@qvac/core 0.1.4 → 0.2.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 (105) hide show
  1. package/.claude/skills/asana/SKILL.md +86 -0
  2. package/.claude/skills/compaction/SKILL.md +127 -0
  3. package/.claude/skills/device-mesh-debug/SKILL.md +328 -0
  4. package/.claude/skills/gh-stack/SKILL.md +181 -0
  5. package/.claude/skills/gh-stack/references/commands.md +177 -0
  6. package/.claude/skills/gh-stack/references/stack-design.md +97 -0
  7. package/.claude/skills/gh-stack/references/troubleshooting.md +159 -0
  8. package/.claude/skills/js-house-style/SKILL.md +670 -0
  9. package/.claude/skills/pr-create/SKILL.md +180 -0
  10. package/.claude/skills/pr-review/SKILL.md +216 -0
  11. package/CLAUDE.md +45 -0
  12. package/README.md +10 -5
  13. package/dist-lib/lib/api/agents.js +3 -3
  14. package/dist-lib/lib/api/chat-groups.d.ts +1 -1
  15. package/dist-lib/lib/api/chat-groups.js +5 -3
  16. package/dist-lib/lib/api/chat.js +2 -2
  17. package/dist-lib/lib/api/current-mesh.d.ts +3 -2
  18. package/dist-lib/lib/api/current-mesh.js +4 -3
  19. package/dist-lib/lib/api/devices.js +1 -0
  20. package/dist-lib/lib/api/index.js +2 -0
  21. package/dist-lib/lib/api/members.d.ts +3 -0
  22. package/dist-lib/lib/api/members.js +23 -0
  23. package/dist-lib/lib/api/mesh.d.ts +14 -3
  24. package/dist-lib/lib/api/mesh.js +20 -3
  25. package/dist-lib/lib/api/watchable.d.ts +1 -1
  26. package/dist-lib/lib/api/watchable.js +1 -1
  27. package/dist-lib/lib/assistant.js +2 -1
  28. package/dist-lib/lib/core/agent.d.ts +1 -0
  29. package/dist-lib/lib/core/agent.js +29 -1
  30. package/dist-lib/lib/core/chat-group.d.ts +4 -0
  31. package/dist-lib/lib/core/chat-group.js +43 -1
  32. package/dist-lib/lib/core/chat.d.ts +10 -0
  33. package/dist-lib/lib/core/chat.js +75 -9
  34. package/dist-lib/lib/core/contract/index.js +2 -0
  35. package/dist-lib/lib/core/contract/joined-meshes.d.ts +2 -0
  36. package/dist-lib/lib/core/contract/joined-meshes.js +41 -0
  37. package/dist-lib/lib/core/contract/members.js +68 -2
  38. package/dist-lib/lib/core/contract/writers.js +19 -3
  39. package/dist-lib/lib/core/departures.d.ts +3 -1
  40. package/dist-lib/lib/core/departures.js +6 -4
  41. package/dist-lib/lib/core/device-attestation.d.ts +8 -0
  42. package/dist-lib/lib/core/device-attestation.js +25 -0
  43. package/dist-lib/lib/core/device-invites.d.ts +9 -0
  44. package/dist-lib/lib/core/device-invites.js +40 -0
  45. package/dist-lib/lib/core/files/index.d.ts +2 -1
  46. package/dist-lib/lib/core/files/index.js +1 -1
  47. package/dist-lib/lib/core/files/store/index.d.ts +1 -0
  48. package/dist-lib/lib/core/files/store/server.d.ts +5 -4
  49. package/dist-lib/lib/core/files/store/server.js +10 -6
  50. package/dist-lib/lib/core/helpers/index.d.ts +1 -1
  51. package/dist-lib/lib/core/hosted-agents.d.ts +3 -1
  52. package/dist-lib/lib/core/hosted-agents.js +9 -3
  53. package/dist-lib/lib/core/identity.d.ts +10 -0
  54. package/dist-lib/lib/core/identity.js +45 -0
  55. package/dist-lib/lib/core/invite.d.ts +3 -0
  56. package/dist-lib/lib/core/invite.js +8 -2
  57. package/dist-lib/lib/core/local/index.d.ts +22 -7
  58. package/dist-lib/lib/core/local/index.js +202 -66
  59. package/dist-lib/lib/core/membership.d.ts +8 -2
  60. package/dist-lib/lib/core/membership.js +33 -9
  61. package/dist-lib/lib/core/mesh-seed-handoff.d.ts +17 -0
  62. package/dist-lib/lib/core/mesh-seed-handoff.js +34 -0
  63. package/dist-lib/lib/core/mesh.d.ts +13 -1
  64. package/dist-lib/lib/core/mesh.js +61 -8
  65. package/dist-lib/lib/core/migrations/index.d.ts +3 -1
  66. package/dist-lib/lib/core/migrations/index.js +2 -2
  67. package/dist-lib/lib/core/migrations/membership.js +27 -15
  68. package/dist-lib/lib/core/rank.d.ts +14 -0
  69. package/dist-lib/lib/core/rank.js +23 -0
  70. package/dist-lib/lib/core/sync/index.js +6 -0
  71. package/dist-lib/lib/core.d.ts +91 -13
  72. package/dist-lib/lib/core.js +1051 -207
  73. package/dist-lib/schema/build.js +3 -2
  74. package/dist-lib/schema/engine.js +153 -9
  75. package/dist-lib/schema/field-order.js +4 -1
  76. package/dist-lib/schema/local.js +32 -0
  77. package/dist-lib/schema/qvac.js +45 -6
  78. package/dist-lib/spec/bind.js +35 -0
  79. package/dist-lib/spec/calls.js +37 -27
  80. package/dist-lib/spec/capabilities.d.ts +30 -10
  81. package/dist-lib/spec/hrpc/hrpc.json +135 -5
  82. package/dist-lib/spec/hrpc/index.js +130 -9
  83. package/dist-lib/spec/hrpc/messages.js +408 -14
  84. package/dist-lib/spec/hyperschema/index.js +408 -14
  85. package/dist-lib/spec/hyperschema/schema.json +309 -1
  86. package/dist-lib/spec/hyperschema/types.d.ts +90 -1
  87. package/dist-lib/spec/local/hyperdb/db.json +46 -1
  88. package/dist-lib/spec/local/hyperdb/index.js +209 -2
  89. package/dist-lib/spec/local/hyperdb/messages.js +557 -90
  90. package/dist-lib/spec/local/hyperschema/index.js +240 -2
  91. package/dist-lib/spec/local/hyperschema/schema.json +120 -1
  92. package/dist-lib/spec/local/hyperschema/types.d.ts +33 -1
  93. package/dist-lib/spec/mesh/hyperdb/db.json +16 -1
  94. package/dist-lib/spec/mesh/hyperdb/index.js +71 -2
  95. package/dist-lib/spec/mesh/hyperdb/messages.js +571 -192
  96. package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +13 -1
  97. package/dist-lib/spec/mesh/hyperdispatch/index.js +35 -1
  98. package/dist-lib/spec/mesh/hyperdispatch/messages.js +283 -20
  99. package/dist-lib/spec/mesh/hyperschema/index.js +283 -20
  100. package/dist-lib/spec/mesh/hyperschema/schema.json +179 -1
  101. package/dist-lib/spec/mesh/hyperschema/types.d.ts +45 -1
  102. package/package.json +6 -6
  103. package/tui/app.mjs +416 -50
  104. package/tui/boot.mjs +1 -1
  105. package/tui/lib/sidebar.mjs +3 -2
@@ -1,3 +1,4 @@
1
+ import b4a from 'b4a';
1
2
  import BlindPeering from 'blind-peering';
2
3
  import Corestore from 'corestore';
3
4
  import CorestoreStorage from 'hypercore-storage';
@@ -10,26 +11,31 @@ import ReadyGuard from 'ready-guard';
10
11
  import ReadyResource from 'ready-resource';
11
12
  import ScopeLock from 'scope-lock';
12
13
  import { createApi } from "./api/index.js";
14
+ import { NO_SUCH_MESH } from "./api/current-mesh.js";
13
15
  import { BLIND_PEER_KEYS, BLIND_RELAY_KEYS } from "./core/blind-peers.js";
14
- import { CoreBlobServer, MemoryVectorIndex } from "./core/files/index.js";
16
+ import { CoreBlobServer, parseBlobReferenceCoreKey, MemoryVectorIndex } from "./core/files/index.js";
15
17
  import { removeChatAttachments, removeChatKnowledge } from "./core/chat.js";
16
18
  import { createFsDriveHost } from "./core/fs-drives.js";
17
19
  import { sweepDriveStaging } from "./fs-drive-bridge.js";
18
20
  import { closeCollaboraWorker } from "./collabora/worker.js";
19
21
  import { decodeMeshInvite, encodeMeshInvite } from "./core/invite.js";
20
22
  import { openLocalStore } from "./core/local/index.js";
21
- import { getAgent } from "./core/agent.js";
23
+ import { getAgent, ownDefaultAgents } from "./core/agent.js";
22
24
  import { HostedAgents } from "./core/hosted-agents.js";
25
+ import { MeshSeedHandoff } from "./core/mesh-seed-handoff.js";
23
26
  import { SecretHandoff } from "./core/secret-handoff.js";
24
27
  import { SkillOverlayHandoff } from "./core/skill-overlay-handoff.js";
25
28
  import { createSkillOverlayReceiver } from "./core/skill-overlay-receiver.js";
26
29
  import { createSkillSecretReceiver, HOST_REVOKED, NO_SUCH_AGENT, NOT_THE_HOST } from "./core/skill-secrets.js";
27
- import { attestUnattestedDevices } from "./core/membership.js";
30
+ import { attestMemberClaim, claimAttestation } from "./core/identity.js";
31
+ import { attestUnattestedDevices, claimOwnMembership } from "./core/membership.js";
32
+ import { openDeviceInvites } from "./core/device-invites.js";
28
33
  import { runMigrations } from "./core/migrations/index.js";
29
34
  import { advertiseDeviceVersion } from "./core/devices.js";
30
35
  import { OtaConfigReader, configCoreFor, resolveConfigChannel, vendoredMinSupportedVersion } from "./core/ota-config.js";
31
36
  import { updateRequired } from "./core/min-version.js";
32
37
  import { PendingDepartures } from "./core/departures.js";
38
+ import { buildDefaultSeed } from "./core/helpers/index.js";
33
39
  import { Mesh } from "./core/mesh.js";
34
40
  import { raceResource } from "./race-resource.js";
35
41
  import { createSystemInfoSampler } from "./core/system-info.js";
@@ -44,16 +50,27 @@ const top = new Top();
44
50
  const SWARM_KEYPAIR_NAME = 'qvac-swarm-keypair';
45
51
  const QVAC_CORE_NAMESPACE = 'qvac-core';
46
52
  const MESH_JOIN_CANCELED = 'Mesh join canceled';
53
+ const MESH_ID = /^[0-9a-f]{64}$/;
47
54
  const MESH_BELOW_FLOOR = 'Build is below the mesh floor';
55
+ const LEAVE_HOME_REFUSED = 'Your account cannot be left while other meshes remain';
56
+ const ROOM_WITHOUT_ACCOUNT = 'A room needs an account mesh to be recorded in';
57
+ export const MEMBER_INVITE_HOME_REFUSED = 'An account mesh cannot be invited into';
58
+ export const DEVICE_INVITE_ROOM_REFUSED = 'Only an account mesh can take a device invite';
59
+ export const INVITE_ACCOUNT_UNREADABLE = 'This device cannot reach its account to check';
60
+ export const ROOM_HELD_BY_SIBLING = 'Another of your devices holds this room: bring that device online, or invite from it instead';
61
+ const ROOM_NOT_EMPTY = 'this room is not empty: everyone in it will see the newcomer arrive, and the newcomer replays the whole history of the room, including what earlier members wrote';
62
+ const JOINED_MESHES = '@qvac/joined-meshes';
48
63
  const INVITE_DHT_NODES_LIMIT = 3;
49
64
  const INVITE_BLIND_PEERS_LIMIT = 3;
50
65
  const DIAGNOSTICS_SAMPLE_MS = 2000;
66
+ const ACCOUNT_WRITABLE_MS = 15_000;
51
67
  export class Core extends ReadyResource {
52
68
  _storagePath;
53
69
  store;
54
70
  rootCorestore;
55
71
  storage;
56
72
  local;
73
+ _deviceInvites;
57
74
  state;
58
75
  _transport;
59
76
  _apiLifecycle;
@@ -88,42 +105,42 @@ export class Core extends ReadyResource {
88
105
  _indexedEmbedder = null;
89
106
  _indexingLock = new ScopeLock();
90
107
  _meshTransitionLock = new ScopeLock();
91
- _processingFiles;
108
+ _opening = new Map();
92
109
  _skills;
93
110
  _skillCatalog;
94
111
  _skillManagement;
95
112
  _catalogHooks = new ReadyGuard();
96
113
  _tools;
97
114
  _hostsModelSource;
98
- _vectorIndex;
115
+ _spareVectorIndex;
99
116
  _closeSignal;
100
117
  _signalClose;
101
118
  _closeListeners;
102
119
  _closeSignaled;
103
- _mesh;
120
+ _meshes;
121
+ _blobCoreMeshes = new Map();
122
+ _activeMeshKey;
104
123
  _offlineBoot = new ReadyGuard();
105
124
  onlineBoot = new ReadyGuard();
106
125
  _candidateMesh;
107
126
  _cancelCandidateJoin;
108
127
  _joinCancellation;
109
- _meshSecretSeed;
110
128
  _rpcs = new Set();
111
129
  _stream;
112
130
  _api;
113
131
  _clients;
114
132
  _deviceKeyPair;
115
- _memberLock = new ScopeLock({ debounce: true });
116
133
  _identityPhrase = null;
117
134
  _identityPublicKey = null;
118
135
  _swarm;
119
136
  _departures = null;
120
137
  _fsDrives = null;
121
138
  _blobServer = null;
122
- _attachmentUrl = (attachment) => this._blobServer?.link(attachment) ?? null;
123
139
  _wakeup;
140
+ _meshSeedHandoff;
141
+ _following = new Set();
124
142
  _secretHandoff;
125
143
  _skillOverlayHandoff;
126
- _hostedAgents;
127
144
  _minting;
128
145
  _leaving;
129
146
  _kicked;
@@ -132,9 +149,6 @@ export class Core extends ReadyResource {
132
149
  _statusListeners;
133
150
  _diagnosticsProviders;
134
151
  _systemInfo;
135
- _onMeshWritable;
136
- _onMeshDead;
137
- _onMeshStalled;
138
152
  _statusError;
139
153
  _upgradeRequired;
140
154
  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 }) {
@@ -158,6 +172,7 @@ export class Core extends ReadyResource {
158
172
  });
159
173
  this.store = this.rootCorestore.namespace(QVAC_CORE_NAMESPACE);
160
174
  this.local = openLocalStore(this.storage);
175
+ this._deviceInvites = openDeviceInvites(this.local);
161
176
  this.state = QvacState.RESUME;
162
177
  this._spec = spec;
163
178
  this._bootstrapNodes = bootstrapNodes;
@@ -189,7 +204,6 @@ export class Core extends ReadyResource {
189
204
  this._speakStream = speakStream ?? null;
190
205
  this._embedder = embedder ?? null;
191
206
  this._ragExtractors = extractors;
192
- this._processingFiles = null;
193
207
  this._skills = skills ?? null;
194
208
  this._skillCatalog = skillCatalog ?? null;
195
209
  this._skillManagement = null;
@@ -197,7 +211,7 @@ export class Core extends ReadyResource {
197
211
  this._hostsModelSource = hostsModelSource;
198
212
  this._openCatalogHooks();
199
213
  void this._catalogHooks.ready().catch(noop);
200
- this._vectorIndex = vectorIndex ?? (embedder ? new MemoryVectorIndex() : null);
214
+ this._spareVectorIndex = vectorIndex ?? (embedder ? new MemoryVectorIndex() : null);
201
215
  this._closeSignal = new Promise((resolve) => {
202
216
  this._signalClose = resolve;
203
217
  });
@@ -210,11 +224,11 @@ export class Core extends ReadyResource {
210
224
  for (const listener of listeners)
211
225
  listener();
212
226
  });
213
- this._mesh = null;
227
+ this._meshes = new Map();
228
+ this._activeMeshKey = null;
214
229
  this._candidateMesh = null;
215
230
  this._cancelCandidateJoin = null;
216
231
  this._joinCancellation = null;
217
- this._meshSecretSeed = null;
218
232
  this._rpcs.clear();
219
233
  this._stream = null;
220
234
  this._api = null;
@@ -228,7 +242,9 @@ export class Core extends ReadyResource {
228
242
  this._skillOverlayHandoff = new SkillOverlayHandoff({
229
243
  onrequest: (op, from) => this._serveSkillOverlayOp(op, from)
230
244
  });
231
- this._hostedAgents = null;
245
+ this._meshSeedHandoff = new MeshSeedHandoff({
246
+ onrequest: (request, from) => this._serveMeshSeed(request, from)
247
+ });
232
248
  this._minting = false;
233
249
  this._leaving = false;
234
250
  this._kicked = false;
@@ -237,26 +253,6 @@ export class Core extends ReadyResource {
237
253
  this._statusListeners = new Set();
238
254
  this._diagnosticsProviders = new Set();
239
255
  this._systemInfo = null;
240
- this._onMeshWritable = () => {
241
- this._kicked = false;
242
- this._notifyStatus();
243
- };
244
- this._onMeshDead = (err) => {
245
- const mesh = this._mesh;
246
- if (this.closing || !mesh)
247
- return;
248
- this._log?.error(['p2p', 'mesh'], 'mesh autobee died — retiring it', err);
249
- this._mesh = null;
250
- this._statusError = statusErrorMessage('the local database died', err);
251
- this._notifyStatus();
252
- this._retireMesh(mesh).catch((retireErr) => {
253
- this._log?.warn(['p2p', 'mesh'], 'retiring the dead mesh failed', retireErr);
254
- });
255
- };
256
- this._onMeshStalled = (err) => {
257
- if (this._mesh)
258
- this._withdrawFromMesh(this._mesh, err);
259
- };
260
256
  this._statusError = null;
261
257
  this._upgradeRequired = null;
262
258
  }
@@ -270,7 +266,7 @@ export class Core extends ReadyResource {
270
266
  return this.state === QvacState.SUSPEND;
271
267
  }
272
268
  get mesh() {
273
- return this._mesh;
269
+ return this._currentMesh();
274
270
  }
275
271
  async meshReady() {
276
272
  await this._awaitMesh();
@@ -291,11 +287,12 @@ export class Core extends ReadyResource {
291
287
  async _runBootOffline() {
292
288
  try {
293
289
  this._deviceKeyPair = await this.store.createKeyPair(SWARM_KEYPAIR_NAME);
294
- this._identityPublicKey = (await this.local.readIdentity())?.publicKey.toString('hex') ?? null;
290
+ this._identityPublicKey = (await this.local.readIdentity())?.publicKey ?? null;
295
291
  this._localDevice = await this.local.ensureDevice(this.devicePublicKey, this._device);
296
292
  this._openSwarm(this._deviceKeyPair);
297
293
  this._departures = new PendingDepartures(this.store, this.local, {
298
294
  swarm: this._swarm,
295
+ isPooled: (key) => this._currentMesh(key.toString('hex')) !== null,
299
296
  logger: this._log,
300
297
  blindPeerKeys: this._resolveBlindPeerKeys(),
301
298
  blindPeeringOpts: this._blindPeeringOpts,
@@ -307,7 +304,7 @@ export class Core extends ReadyResource {
307
304
  blindPeeringOpts: this._blindPeeringOpts
308
305
  });
309
306
  void sweepDriveStaging();
310
- this._blobServer = new CoreBlobServer(this.store, () => this._currentMesh());
307
+ this._blobServer = new CoreBlobServer(this.store, (coreKey) => this._meshForBlobCore(coreKey));
311
308
  this._blobServer.on('rebound', (port) => {
312
309
  logger.warn('core', 'blob server rebound, links minted before now are stale', { port });
313
310
  });
@@ -318,14 +315,14 @@ export class Core extends ReadyResource {
318
315
  this._api = createApi({
319
316
  state: this.local,
320
317
  notificationLifetime: this,
321
- mesh: () => this._currentMesh(),
318
+ mesh: (meshId) => this._meshFor(meshId),
322
319
  meshStatus: this._meshStatusSource(),
323
320
  network: this._networkStatusSource(),
324
321
  diagnostics: this._diagnosticsSource(),
325
322
  logs: this._logSource(),
326
323
  deviceId: this.devicePublicKey,
327
324
  lifecycle: this._apiLifecycle,
328
- ragIndex: () => this._vectorIndex,
325
+ ragIndex: () => this._activeEntry()?.vectorIndex ?? null,
329
326
  ragEmbed: () => this._indexedEmbedder,
330
327
  platform: () => this._localDevice?.platform ?? null,
331
328
  pushSkillSecret: (request) => this.pushSkillSecret(request),
@@ -450,7 +447,8 @@ export class Core extends ReadyResource {
450
447
  logger.info('core', 'closing local');
451
448
  await this.local?.close();
452
449
  logger.info('core', 'closing mesh');
453
- await this._mesh?.close();
450
+ for (const { mesh } of this._meshes.values())
451
+ await mesh.close();
454
452
  logger.info('core', 'closing storage');
455
453
  await this.store?.close();
456
454
  await this.rootCorestore?.close();
@@ -461,10 +459,10 @@ export class Core extends ReadyResource {
461
459
  }
462
460
  }
463
461
  async _openBootMesh() {
464
- if (this._blockedByMinVersion() || this._mesh || this._minting)
462
+ if (this._blockedByMinVersion() || this._currentMesh() || this._minting)
465
463
  return;
466
464
  try {
467
- await this._openMesh({ seed: this._meshSeed, baseKey: this._meshKey });
465
+ await this._switchToMesh({ seed: this._meshSeed, baseKey: this._meshKey, account: true });
468
466
  }
469
467
  catch (err) {
470
468
  if (this.closing)
@@ -482,8 +480,9 @@ export class Core extends ReadyResource {
482
480
  await this._offlineBoot.ready();
483
481
  if (this.closing)
484
482
  return;
485
- if (this._mesh)
486
- this._bringMeshOnline(this._mesh);
483
+ const mesh = this._currentMesh();
484
+ if (mesh)
485
+ this._bringMeshOnline(mesh);
487
486
  }
488
487
  catch (err) {
489
488
  if (this.closing)
@@ -494,15 +493,15 @@ export class Core extends ReadyResource {
494
493
  if (!this.closing && !this._offlineBoot.destroyed)
495
494
  this._peerConfigCore();
496
495
  if (!this.closing)
497
- this._departures?.flush(this._mesh?.key);
496
+ this._departures?.flush();
498
497
  this.onlineBoot.exit();
499
498
  }
500
499
  }
501
500
  async _reopenMesh() {
502
501
  try {
503
- if (this.closing || this._mesh || this._minting)
502
+ if (this.closing || this._currentMesh() || this._minting)
504
503
  return;
505
- await this._startMesh({ seed: this._meshSeed, baseKey: this._meshKey });
504
+ await this._startMesh({ seed: this._meshSeed, baseKey: this._meshKey, account: true });
506
505
  }
507
506
  catch (err) {
508
507
  if (this.closing)
@@ -566,7 +565,7 @@ export class Core extends ReadyResource {
566
565
  }
567
566
  _peerConfigCore() {
568
567
  const config = this._otaConfig;
569
- if (!config || this._configPeering || this._mesh || !this._swarm)
568
+ if (!config || this._configPeering || this._currentMesh() || !this._swarm)
570
569
  return;
571
570
  const keys = this._resolveBlindPeerKeys();
572
571
  if (keys.length === 0)
@@ -600,7 +599,7 @@ export class Core extends ReadyResource {
600
599
  }
601
600
  if (this._upgradeRequired)
602
601
  return true;
603
- this._log?.error(['p2p', 'mesh'], 'mesh requires a newer build — not opening', {
602
+ this._log?.error(['p2p', 'mesh'], 'mesh requires a newer build - not opening', {
604
603
  range,
605
604
  appVersion
606
605
  });
@@ -626,14 +625,12 @@ export class Core extends ReadyResource {
626
625
  void this._reopenMesh();
627
626
  return;
628
627
  }
629
- const mesh = this._mesh;
630
- if (!mesh)
631
- return;
632
- this._mesh = null;
628
+ for (const { mesh } of [...this._meshes.values()]) {
629
+ this._retirePooledMesh(mesh).catch((err) => {
630
+ this._log?.warn(['p2p', 'mesh'], 'retiring a mesh above our floor failed', err);
631
+ });
632
+ }
633
633
  this._notifyStatus();
634
- this._retireMesh(mesh).catch((err) => {
635
- this._log?.warn(['p2p', 'mesh'], 'retiring a mesh above our floor failed', err);
636
- });
637
634
  }
638
635
  async connect(stream) {
639
636
  if (!this.opened)
@@ -678,8 +675,8 @@ export class Core extends ReadyResource {
678
675
  if (!this.opened)
679
676
  await this.ready();
680
677
  logger.info('core', 'suspending');
681
- if (this._mesh)
682
- await this._mesh.suspend();
678
+ for (const { mesh } of this._meshes.values())
679
+ await mesh.suspend();
683
680
  if (this._departures)
684
681
  await this._departures.suspend();
685
682
  if (this._configPeering)
@@ -712,11 +709,11 @@ export class Core extends ReadyResource {
712
709
  await this._swarm.resume();
713
710
  if (this._configPeering)
714
711
  await this._configPeering.resume();
715
- if (this._mesh)
716
- await this._mesh.resume();
712
+ for (const { mesh } of this._meshes.values())
713
+ await mesh.resume();
717
714
  if (this._departures)
718
715
  await this._departures.resume();
719
- this._departures?.flush(this._mesh?.key);
716
+ this._departures?.flush();
720
717
  if (this._swarm)
721
718
  void this._refreshTopics(this._swarm).catch(noop);
722
719
  void this._pushIdentityPhrase();
@@ -743,17 +740,15 @@ export class Core extends ReadyResource {
743
740
  for (const client of this._clients)
744
741
  client.destroy();
745
742
  this._clients.clear();
746
- await this._stopHostedAgents();
743
+ for (const entry of this._meshes.values())
744
+ await this._stopHostedAgents(entry);
747
745
  await this._stopIndexing();
748
746
  closeCollaboraWorker();
749
747
  await this._blobServer?.close();
750
748
  this._blobServer = null;
751
- if (this._mesh) {
752
- this._mesh.autobee.off('writable', this._onMeshWritable);
753
- this._mesh.autobee.off('error', this._onMeshDead);
754
- this._mesh.autobee.off('close', this._onMeshDead);
755
- this._mesh.autobee.off('interrupt', this._onMeshStalled);
756
- await this._mesh.close();
749
+ for (const entry of this._meshes.values()) {
750
+ this._unwireMesh(entry);
751
+ await entry.mesh.close();
757
752
  }
758
753
  await this._discardCandidateMesh();
759
754
  await this._departures?.close();
@@ -764,8 +759,8 @@ export class Core extends ReadyResource {
764
759
  this._fsDrives = null;
765
760
  logger.info('core', 'closing swarm');
766
761
  await this._closeSwarm();
767
- this._mesh = null;
768
- this._meshSecretSeed = null;
762
+ this._meshes.clear();
763
+ this._activeMeshKey = null;
769
764
  logger.info('core', 'closing local');
770
765
  await this.local?.close();
771
766
  this._systemInfo?.destroy();
@@ -798,52 +793,60 @@ export class Core extends ReadyResource {
798
793
  return [...merged.values()];
799
794
  }
800
795
  async _startMesh(opts = {}) {
801
- const mesh = await this._openMesh(opts);
796
+ const mesh = await this._switchToMesh(opts);
802
797
  this._bringMeshOnline(mesh, opts);
803
798
  return mesh;
804
799
  }
805
- async _openMesh(opts = {}) {
800
+ async _switchToMesh(opts = {}) {
806
801
  if (this.closing)
807
802
  throw new Error('core is closing');
808
803
  if (this._blockedByMinVersion())
809
804
  throw new Error(MESH_BELOW_FLOOR);
810
- if (this._mesh) {
811
- await this._retireMesh(this._mesh);
805
+ const previous = this._currentMesh();
806
+ if (previous)
807
+ await this._retirePooledMesh(previous);
808
+ const mesh = await this._openMesh(opts);
809
+ if (this.closing) {
810
+ await this._retireMesh(mesh);
811
+ throw new Error('core is closing');
812
812
  }
813
+ const halted = mesh.interrupted;
814
+ if (halted) {
815
+ this._withdrawFromMesh(mesh, halted);
816
+ throw halted;
817
+ }
818
+ await this._activateMesh(mesh);
819
+ return mesh;
820
+ }
821
+ async _openMesh(opts = {}) {
822
+ const session = await this.local.resolveSession(this._deviceKeyPair.publicKey, opts);
823
+ return this._openMeshSession(session, opts);
824
+ }
825
+ async _openMeshSession(session, { blindPeerKeys, account, room } = {}) {
813
826
  const deviceId = this._deviceKeyPair.publicKey;
814
- const session = await this.local.resolveSession(deviceId, opts);
815
827
  const mesh = new Mesh(this.store, {
816
828
  seed: session.seed,
817
829
  key: session.baseKey ?? null,
818
830
  deviceId,
819
831
  writerSeed: session.writerSeed,
820
832
  logger: this._log,
821
- blindPeerKeys: this._resolveBlindPeerKeys(opts.blindPeerKeys),
833
+ blindPeerKeys: this._resolveBlindPeerKeys(blindPeerKeys),
822
834
  blindPeeringOpts: this._blindPeeringOpts,
823
835
  wakeup: this._wakeup,
824
- attachmentUrl: this._attachmentUrl
836
+ attachmentUrl: (attachment) => this._linkAttachment(attachment, mesh)
825
837
  });
826
838
  try {
827
839
  await mesh.ready();
828
840
  await raceResource(mesh.autobee, (async () => {
829
- await mesh.appendBirthOp(deviceId, this._localDevice ?? undefined, await this._mintBirthMember(mesh, deviceId, session));
830
- await this.local.recordMesh(deviceId, mesh.key, session.seed, session.creator, session.writerSeed);
841
+ await mesh.appendBirthOp(deviceId, this._localDevice ?? undefined, await this._mintBirthMember(mesh, deviceId, session), { deviceKeyPair: this._deviceKeyPair }, { room });
842
+ await this.local.recordMesh(deviceId, mesh.key, session, { account });
831
843
  })(), 'mesh');
832
844
  }
833
845
  catch (err) {
834
846
  await mesh.close().catch(noop);
835
847
  throw err;
836
848
  }
837
- if (this.closing) {
838
- await mesh.close();
839
- throw new Error('core is closing');
840
- }
841
- const halted = mesh.interrupted;
842
- if (halted) {
843
- this._withdrawFromMesh(mesh, halted);
844
- throw halted;
845
- }
846
- this._activateMesh(mesh, session);
849
+ this._poolEntry(mesh);
847
850
  return mesh;
848
851
  }
849
852
  async _mintBirthMember(mesh, deviceId, session) {
@@ -852,13 +855,28 @@ export class Core extends ReadyResource {
852
855
  return undefined;
853
856
  const now = Date.now();
854
857
  const { identity, mnemonic } = await this.local.ensureIdentity(deviceId, now);
855
- this._identityPublicKey = identity.publicKey.toString('hex');
858
+ this._identityPublicKey = identity.publicKey;
856
859
  this._holdIdentityPhrase(mnemonic);
857
860
  return {
858
861
  member: { id: identity.publicKey, name: local.name, joinedAt: now, updatedAt: now },
859
862
  proof: identity.proof
860
863
  };
861
864
  }
865
+ async _joinBirthMember(mesh, deviceId) {
866
+ const local = this._localDevice;
867
+ if (!local || mesh.localLength > 0)
868
+ return undefined;
869
+ const now = Date.now();
870
+ const { identity, mnemonic } = await this.local.ensureIdentity(deviceId, now);
871
+ this._identityPublicKey = identity.publicKey;
872
+ this._holdIdentityPhrase(mnemonic);
873
+ const attestation = claimAttestation(mesh.publicKey, deviceId, mesh.localWriterKey);
874
+ const proof = await attestMemberClaim(attestation, this._deviceKeyPair, identity.proof);
875
+ return {
876
+ member: { id: identity.publicKey, name: local.name, joinedAt: now, updatedAt: now },
877
+ proof
878
+ };
879
+ }
862
880
  _holdIdentityPhrase(mnemonic) {
863
881
  if (!mnemonic)
864
882
  return;
@@ -886,7 +904,7 @@ export class Core extends ReadyResource {
886
904
  }
887
905
  }
888
906
  _bringMeshOnline(mesh, opts = {}) {
889
- if (this.closing || mesh.closing || this._mesh !== mesh)
907
+ if (this.closing || !this._isPooled(mesh))
890
908
  return;
891
909
  if (this._swarm) {
892
910
  this._swarm.join(mesh.discoveryKey, { server: true, client: true });
@@ -897,7 +915,7 @@ export class Core extends ReadyResource {
897
915
  }
898
916
  if (!opts.new)
899
917
  void this._ensureNotRevoked(mesh);
900
- this._departures?.flush(mesh.key);
918
+ this._departures?.flush();
901
919
  void this._advertiseAppVersion(mesh, this._deviceKeyPair.publicKey);
902
920
  void this._advertiseDeviceProfile(mesh, this._deviceKeyPair.publicKey);
903
921
  void this._settleMembership(mesh, { migrate: true });
@@ -908,7 +926,7 @@ export class Core extends ReadyResource {
908
926
  return;
909
927
  try {
910
928
  await mesh.waitForWritable();
911
- if (mesh.closing || this._mesh !== mesh)
929
+ if (!this._isPooled(mesh))
912
930
  return;
913
931
  const snap = mesh.view.snapshot();
914
932
  let existing;
@@ -940,7 +958,7 @@ export class Core extends ReadyResource {
940
958
  return;
941
959
  try {
942
960
  await mesh.waitForWritable();
943
- if (mesh.closing || this._mesh !== mesh)
961
+ if (!this._isPooled(mesh))
944
962
  return;
945
963
  const snap = mesh.view.snapshot();
946
964
  let existing;
@@ -964,27 +982,54 @@ export class Core extends ReadyResource {
964
982
  async _settleMembership(mesh, { migrate = false } = {}) {
965
983
  if (!this._localDevice)
966
984
  return;
967
- if (!(await this._memberLock.lock()))
985
+ const entry = this._meshes.get(this._poolKey(mesh));
986
+ if (entry?.mesh !== mesh)
987
+ return;
988
+ if (!(await entry.memberLock.lock()))
968
989
  return;
969
990
  try {
970
991
  await mesh.waitForWritable();
971
- if (mesh.closing || this._mesh !== mesh)
992
+ if (!this._isPooled(mesh))
972
993
  return;
973
994
  await mesh.autobee.update();
974
- if (mesh.closing || this._mesh !== mesh)
995
+ if (!this._isPooled(mesh))
975
996
  return;
976
997
  const context = { deviceKeyPair: this._deviceKeyPair, log: this._log };
977
- if (migrate)
978
- await runMigrations(mesh, { ...context, ...this._migrationContext() });
979
- await attestUnattestedDevices(mesh, context);
998
+ if (migrate) {
999
+ await runMigrations(mesh, { ...context, ...this._migrationContext() }, {
1000
+ catalog: await this._isHomeMesh(mesh)
1001
+ });
1002
+ }
1003
+ await this._claimOwnMembership(mesh, context);
1004
+ await attestUnattestedDevices(mesh, context, this._deviceInvites);
980
1005
  }
981
1006
  catch (err) {
982
1007
  this._log?.warn(['p2p', 'mesh'], 'settling membership failed', err);
983
1008
  }
984
1009
  finally {
985
- this._memberLock.unlock();
1010
+ entry.memberLock.unlock();
986
1011
  }
987
1012
  }
1013
+ async _claimOwnMembership(mesh, context) {
1014
+ if (mesh.ourMemberId)
1015
+ return;
1016
+ const known = await this._knownMember(mesh);
1017
+ if (known)
1018
+ await claimOwnMembership(mesh, context, known);
1019
+ }
1020
+ async _knownMember(exclude) {
1021
+ const deviceId = this._deviceKeyPair.publicKey;
1022
+ for (const entry of this._meshes.values()) {
1023
+ if (entry.mesh === exclude || !entry.mesh.ourMemberId)
1024
+ continue;
1025
+ const own = await entry.mesh.view
1026
+ .get('@qvac/devices', { id: deviceId })
1027
+ .catch(() => null);
1028
+ if (own?.memberId && own.memberProof)
1029
+ return { id: own.memberId, proof: own.memberProof };
1030
+ }
1031
+ return null;
1032
+ }
988
1033
  _migrationContext() {
989
1034
  return {
990
1035
  device: {
@@ -994,15 +1039,16 @@ export class Core extends ReadyResource {
994
1039
  deviceName: this._localDevice.name,
995
1040
  mintRoot: async (deviceId, at) => {
996
1041
  const { identity, mnemonic } = await this.local.ensureIdentity(deviceId, at);
997
- this._identityPublicKey = identity.publicKey.toString('hex');
1042
+ this._identityPublicKey = identity.publicKey;
998
1043
  this._holdIdentityPhrase(mnemonic);
999
1044
  return { publicKey: identity.publicKey, proof: identity.proof };
1000
1045
  }
1001
1046
  };
1002
1047
  }
1003
- async _recordMembership(own) {
1048
+ async _recordMemberProof(mesh, own) {
1004
1049
  try {
1005
- await this.local.recordMembership({
1050
+ await this.local.recordMemberProof({
1051
+ meshKey: mesh.key,
1006
1052
  memberId: own.memberId,
1007
1053
  proof: own.memberProof,
1008
1054
  attestedAt: own.attestedAt || 0
@@ -1013,58 +1059,121 @@ export class Core extends ReadyResource {
1013
1059
  }
1014
1060
  }
1015
1061
  async _ensureNotRevoked(mesh) {
1062
+ const wasActive = this._isActive(mesh);
1016
1063
  try {
1017
1064
  const revocation = await mesh.checkResumedRevocation();
1018
- if (!revocation || this._mesh !== mesh)
1065
+ if (!revocation || !this._isPooled(mesh))
1066
+ return;
1067
+ if (!wasActive || !this._isActive(mesh)) {
1068
+ if (revocation !== 'kicked')
1069
+ return;
1070
+ this._log?.info(['p2p', 'mesh'], 'a sibling mesh kicked this device, closing it');
1071
+ await this._departRevokedMesh(mesh);
1019
1072
  return;
1020
- this._log?.info(['p2p', 'mesh'], `resumed already revoked (${revocation} while offline) — minting a fresh mesh`);
1073
+ }
1074
+ this._log?.info(['p2p', 'mesh'], `resumed already revoked (${revocation} while offline), replacing the active mesh`);
1021
1075
  if (revocation === 'kicked')
1022
1076
  this._kickedPending = true;
1023
- await this._requestFreshMesh();
1077
+ await this._replaceRevokedActiveMesh(mesh);
1024
1078
  }
1025
1079
  catch (err) {
1026
1080
  this._log?.warn(['p2p', 'mesh'], 'resumed revocation check failed', err);
1027
1081
  }
1028
1082
  }
1029
1083
  async _retireMesh(mesh) {
1030
- if (this._mesh === mesh) {
1031
- this._mesh = null;
1032
- this._notifyStatus();
1033
- }
1034
- if (this._candidateMesh === mesh)
1035
- this._candidateMesh = null;
1036
- mesh.autobee.off('writable', this._onMeshWritable);
1037
- mesh.autobee.off('error', this._onMeshDead);
1038
- mesh.autobee.off('close', this._onMeshDead);
1039
- mesh.autobee.off('interrupt', this._onMeshStalled);
1040
- await this._stopHostedAgents();
1084
+ this._unpoolMesh(mesh);
1085
+ if (this._swarm)
1086
+ await this._swarm.leave(mesh.discoveryKey).catch(() => { });
1087
+ await mesh.close();
1088
+ }
1089
+ async _retirePooledMesh(mesh) {
1090
+ const entry = this._meshes.get(this._poolKey(mesh));
1091
+ this._unpoolMesh(mesh);
1041
1092
  try {
1042
- await this._stopIndexing();
1093
+ await this._stopMeshServices(entry?.mesh === mesh ? entry : null);
1043
1094
  }
1044
1095
  finally {
1045
- if (this._swarm)
1046
- await this._swarm.leave(mesh.discoveryKey).catch(() => { });
1047
- await mesh.close();
1096
+ await this._retireMesh(mesh);
1048
1097
  }
1049
1098
  }
1099
+ async _stopMeshServices(entry) {
1100
+ if (!entry)
1101
+ return;
1102
+ await this._stopHostedAgents(entry);
1103
+ await this._stopEntryIndexing(entry);
1104
+ this._releaseVectorIndex(entry);
1105
+ }
1106
+ async _stopEntryIndexing(entry) {
1107
+ const files = entry.processingFiles;
1108
+ if (!files)
1109
+ return;
1110
+ entry.processingFiles = null;
1111
+ if (!(await files.stopProcessing()))
1112
+ entry.vectorIndex = null;
1113
+ }
1114
+ _releaseVectorIndex(entry) {
1115
+ this._spareVectorIndex ??= entry.vectorIndex;
1116
+ entry.vectorIndex = null;
1117
+ }
1050
1118
  _withdrawFromMesh(mesh, err) {
1051
- if (this.closing || this._upgradeRequired)
1119
+ if (this.closing)
1052
1120
  return;
1053
- this._log?.error(['p2p', 'mesh'], 'mesh needs a newer build — withdrawing from it', err);
1054
- this._setUpgradeRequired('halt');
1055
- this._retireMesh(mesh).catch((retireErr) => {
1121
+ if (!this._upgradeRequired) {
1122
+ this._log?.error(['p2p', 'mesh'], 'mesh needs a newer build - withdrawing from it', err);
1123
+ this._setUpgradeRequired('halt');
1124
+ }
1125
+ this._retirePooledMesh(mesh).catch((retireErr) => {
1056
1126
  this._log?.warn(['p2p', 'mesh'], 'retiring the stalled mesh failed', retireErr);
1057
1127
  });
1058
1128
  }
1059
- _onKicked() {
1129
+ _onKicked(mesh) {
1060
1130
  if (this.closing || this._leaving)
1061
1131
  return;
1062
- this._log?.info(['p2p', 'mesh'], 'our writer was revoked (kicked) — minting a fresh mesh');
1132
+ if (!this._isActive(mesh)) {
1133
+ this._log?.info(['p2p', 'mesh'], 'our writer was revoked on a sibling mesh, closing it');
1134
+ this._departRevokedMesh(mesh).catch((err) => this._log?.warn(['p2p', 'mesh'], 'closing the revoked sibling mesh failed', err));
1135
+ return;
1136
+ }
1137
+ this._log?.info(['p2p', 'mesh'], 'our writer was revoked (kicked), replacing the active mesh');
1063
1138
  this._kicked = true;
1064
1139
  this._kickedPending = true;
1065
- this._requestFreshMesh().catch((err) => {
1066
- this._log?.error(['p2p', 'mesh'], 'kick-driven re-mint failed', err);
1067
- });
1140
+ this._replaceRevokedActiveMesh(mesh).catch((err) => this._log?.error(['p2p', 'mesh'], 'kick-driven replacement failed', err));
1141
+ }
1142
+ async _replaceRevokedActiveMesh(mesh) {
1143
+ this._minting = true;
1144
+ this._notifyStatus();
1145
+ let fallback = null;
1146
+ try {
1147
+ const others = (await this.local.listMeshes()).filter((row) => !row.key.equals(mesh.key));
1148
+ await this._archiveMembership(mesh);
1149
+ fallback = await this._writableFallback(others);
1150
+ if (fallback) {
1151
+ await this._handOffActive(fallback, this._activeEntry());
1152
+ await this._retirePooledMesh(mesh);
1153
+ this.emit('mesh-changed', fallback);
1154
+ }
1155
+ }
1156
+ finally {
1157
+ this._minting = false;
1158
+ this._notifyStatus();
1159
+ }
1160
+ if (!fallback)
1161
+ await this._requestFreshMesh();
1162
+ }
1163
+ async _departRevokedMesh(mesh) {
1164
+ await this._archiveMembership(mesh);
1165
+ await this._retirePooledMesh(mesh);
1166
+ this._notifyStatus();
1167
+ }
1168
+ async _archiveMembership(mesh) {
1169
+ if (!mesh)
1170
+ return;
1171
+ try {
1172
+ await this.local.archiveMesh(mesh.key);
1173
+ }
1174
+ catch (error) {
1175
+ this._log?.warn(['p2p', 'mesh'], 'archiving the departed mesh row failed', error);
1176
+ }
1068
1177
  }
1069
1178
  async _requestFreshMesh() {
1070
1179
  if (this.closing || this._minting)
@@ -1072,7 +1181,7 @@ export class Core extends ReadyResource {
1072
1181
  this._minting = true;
1073
1182
  this._notifyStatus();
1074
1183
  try {
1075
- this.emit('mesh-changed', await this._startMesh({ new: true }));
1184
+ this.emit('mesh-changed', await this._startMesh({ new: true, account: true }));
1076
1185
  }
1077
1186
  catch (err) {
1078
1187
  if (this.closing)
@@ -1124,8 +1233,15 @@ export class Core extends ReadyResource {
1124
1233
  throw new Error('Core is closing');
1125
1234
  if (this._leaving || this._minting)
1126
1235
  throw new Error('Mesh transition already in progress');
1127
- if (this._mesh?.key.equals(invite.baseKey) && this._mesh.writable)
1128
- return;
1236
+ const pooled = this._currentMesh(invite.baseKey.toString('hex'));
1237
+ if (pooled) {
1238
+ if (pooled.writable) {
1239
+ if (!this._isActive(pooled))
1240
+ await this._handOffActive(pooled, this._activeEntry());
1241
+ return;
1242
+ }
1243
+ await this._retirePooledMesh(pooled);
1244
+ }
1129
1245
  ownsTransition = true;
1130
1246
  this._minting = true;
1131
1247
  this._notifyStatus();
@@ -1136,7 +1252,7 @@ export class Core extends ReadyResource {
1136
1252
  this._minting = false;
1137
1253
  this._meshTransitionLock.unlock();
1138
1254
  this._notifyStatus();
1139
- this._departures?.flush(this._mesh?.key);
1255
+ this._departures?.flush();
1140
1256
  }
1141
1257
  }
1142
1258
  cancelMeshJoin() {
@@ -1161,7 +1277,7 @@ export class Core extends ReadyResource {
1161
1277
  }
1162
1278
  async _runJoinMesh(invite) {
1163
1279
  const deviceId = this._deviceKeyPair.publicKey;
1164
- const source = this._mesh;
1280
+ const source = this._currentMesh();
1165
1281
  for (const node of invite.additionalNodes ?? [])
1166
1282
  this._swarm?.dht.addNode(node);
1167
1283
  await this._departures?.park(invite.baseKey);
@@ -1186,29 +1302,149 @@ export class Core extends ReadyResource {
1186
1302
  if (this._joinCancellation)
1187
1303
  this._joinCancellation.accepting = false;
1188
1304
  try {
1189
- if (source && (await this._departureHasAudience(source))) {
1190
- await this.local.recordDeparture(source.key);
1305
+ if (source && !invite.memberInvite && (await this._departureHasAudience(source))) {
1306
+ await this.local.recordDeparture(source.key, { creator: source.minted });
1191
1307
  }
1192
- await this.local.recordMesh(deviceId, candidate.key, session.seed, session.creator, session.writerSeed);
1308
+ await this.local.recordMesh(deviceId, candidate.key, session);
1193
1309
  }
1194
1310
  catch (error) {
1195
1311
  await this._discardCandidateMesh();
1196
1312
  throw error;
1197
1313
  }
1198
- if (source) {
1199
- await this._announceDeparture(source);
1200
- await this._retireMesh(source).catch((error) => this._log?.warn(['p2p', 'mesh'], 'source mesh retirement failed', error));
1201
- }
1314
+ const outgoing = invite.memberInvite ? this._activeEntry() : null;
1315
+ if (!invite.memberInvite)
1316
+ await this._departOldAccount(candidate.key, source);
1202
1317
  if (this._blockedByMinVersion()) {
1318
+ await this.local.setActiveMesh(deviceId, candidate.key);
1203
1319
  await this._retireMesh(candidate).catch((err) => this._log?.warn(['p2p', 'mesh'], 'withholding the joined mesh failed', err));
1204
1320
  return;
1205
1321
  }
1206
- this._activateMesh(candidate, session);
1322
+ await this._handOffActive(candidate, outgoing);
1207
1323
  void this._departures?.drop(candidate.key).catch(noop);
1208
1324
  void this._advertiseDeviceProfile(candidate, deviceId);
1209
1325
  void this._settleMembership(candidate, { migrate: true });
1326
+ if (invite.memberInvite) {
1327
+ void this._recordJoinedRoom(candidate.key);
1328
+ void this._seedMemberDefaults(candidate);
1329
+ }
1210
1330
  this.emit('mesh-changed', candidate);
1211
1331
  }
1332
+ async _departOldAccount(joined, source) {
1333
+ const account = (await this._homeMeshKey()) ?? source?.key;
1334
+ const rows = (await this.local.listMeshes()).filter((row) => !row.key.equals(joined));
1335
+ const rooms = rows.filter((row) => !account?.equals(row.key));
1336
+ const registry = account ? await this._accountMesh() : null;
1337
+ if (registry)
1338
+ for (const room of rooms)
1339
+ await this._releaseJoinedRoom(room.key, registry);
1340
+ for (const room of rooms)
1341
+ await this._departIfPooled(room.key, 'room');
1342
+ await this.local.adoptHomeMesh(this._deviceKeyPair.publicKey, joined);
1343
+ if (account)
1344
+ await this._departIfPooled(account, 'account');
1345
+ }
1346
+ async _departIfPooled(key, kind) {
1347
+ const meshId = key.toString('hex');
1348
+ const mesh = this._currentMesh(meshId);
1349
+ if (!mesh) {
1350
+ this._log?.warn(['p2p', 'mesh'], `the old ${kind} was closed, so its writer was not revoked`, {
1351
+ meshId
1352
+ });
1353
+ return;
1354
+ }
1355
+ try {
1356
+ await Promise.race([mesh.waitForWritable(), this._closeSignal, after(ACCOUNT_WRITABLE_MS)]);
1357
+ if (!mesh.writable) {
1358
+ this._log?.warn(['p2p', 'mesh'], `the old ${kind} never went writable, so its writer was not revoked`, { meshId });
1359
+ return;
1360
+ }
1361
+ await this._announceDeparture(mesh);
1362
+ await this._retirePooledMesh(mesh);
1363
+ }
1364
+ catch (error) {
1365
+ this._log?.warn(['p2p', 'mesh'], `departing the old ${kind} failed`, error);
1366
+ }
1367
+ }
1368
+ async _seedMemberDefaults(mesh) {
1369
+ const local = this._localDevice;
1370
+ if (!local)
1371
+ return;
1372
+ try {
1373
+ await Promise.race([mesh.waitForWritable(), this._closeSignal]);
1374
+ if (!this._isPooled(mesh))
1375
+ return;
1376
+ await mesh.autobee.update();
1377
+ if (!this._isPooled(mesh) || (await this._hasOwnDefaultAgent(mesh)))
1378
+ return;
1379
+ const deviceId = this._deviceKeyPair.publicKey;
1380
+ await mesh.dispatch('seed-defaults', buildDefaultSeed({ id: deviceId, recommendedModelName: local.recommendedModelName }, []));
1381
+ }
1382
+ catch (error) {
1383
+ this._log?.warn(['p2p', 'mesh'], 'seeding this member defaults in the room failed', error);
1384
+ }
1385
+ }
1386
+ async _hasOwnDefaultAgent(mesh) {
1387
+ return (await ownDefaultAgents(mesh)).length > 0;
1388
+ }
1389
+ async _recordJoinedRoom(meshKey) {
1390
+ try {
1391
+ await this._registerRoom(meshKey, false);
1392
+ }
1393
+ catch (error) {
1394
+ this._log?.warn(['p2p', 'mesh'], 'recording the joined room failed', error);
1395
+ }
1396
+ }
1397
+ async _registerRoom(meshKey, shared, held) {
1398
+ const registry = held ?? (await this._accountMesh());
1399
+ if (!registry)
1400
+ throw new Error(ROOM_WITHOUT_ACCOUNT);
1401
+ const ours = registry.ourMemberId;
1402
+ if (!ours)
1403
+ throw new Error('our member is not known yet');
1404
+ const now = Date.now();
1405
+ await registry.dispatch('put-joined-mesh', {
1406
+ room: { meshKey, memberId: ours, joinedAt: now, shared, sharedAt: shared ? now : 0 }
1407
+ });
1408
+ }
1409
+ async _releaseJoinedRoom(meshKey, registry) {
1410
+ try {
1411
+ const held = registry ? [registry] : [...this._meshes.values()].map((entry) => entry.mesh);
1412
+ for (const mesh of held) {
1413
+ const ours = mesh.ourMemberId;
1414
+ if (!mesh.writable || !ours || mesh.key.equals(meshKey))
1415
+ continue;
1416
+ const room = await mesh.view.get(JOINED_MESHES, { meshKey });
1417
+ if (!room?.memberId.equals(ours))
1418
+ continue;
1419
+ await mesh.dispatch('put-joined-mesh', {
1420
+ room: { meshKey, memberId: ours, joinedAt: room.joinedAt, leftAt: Date.now() }
1421
+ });
1422
+ return;
1423
+ }
1424
+ }
1425
+ catch (error) {
1426
+ this._log?.warn(['p2p', 'mesh'], 'releasing the joined room failed', error);
1427
+ }
1428
+ }
1429
+ async _accountMesh() {
1430
+ if (this.closing)
1431
+ return null;
1432
+ const home = await this._homeMeshKey();
1433
+ const held = home ? this._currentMesh(home.toString('hex')) : null;
1434
+ const mesh = home ? await this._openPooledMesh(home.toString('hex')).catch(() => null) : null;
1435
+ if (mesh && !mesh.writable) {
1436
+ await Promise.race([mesh.waitForWritable(), this._closeSignal, after(ACCOUNT_WRITABLE_MS)]);
1437
+ }
1438
+ if (mesh && !mesh.ourMemberId)
1439
+ await mesh.refreshOurMember();
1440
+ if (!mesh?.writable) {
1441
+ if (mesh && !held)
1442
+ await this._retirePooledMesh(mesh).catch(noop);
1443
+ this._log?.warn(['p2p', 'mesh'], 'no writable account mesh');
1444
+ return null;
1445
+ }
1446
+ return mesh;
1447
+ }
1212
1448
  async _openCandidateMesh(invite, session) {
1213
1449
  this._throwIfJoinCanceled();
1214
1450
  const candidate = new Mesh(this.store, {
@@ -1221,7 +1457,7 @@ export class Core extends ReadyResource {
1221
1457
  blindPeeringOpts: this._blindPeeringOpts,
1222
1458
  bootFrom: invite.fastForward,
1223
1459
  wakeup: this._wakeup,
1224
- attachmentUrl: this._attachmentUrl
1460
+ attachmentUrl: (attachment) => this._linkAttachment(attachment, candidate)
1225
1461
  });
1226
1462
  this._candidateMesh = candidate;
1227
1463
  if (this._swarm)
@@ -1247,7 +1483,16 @@ export class Core extends ReadyResource {
1247
1483
  candidate.startBlindPeering(this._swarm.dht);
1248
1484
  try {
1249
1485
  this._throwIfJoinCanceled();
1250
- await candidate.appendBirthOp(this._deviceKeyPair.publicKey, this._localDevice ?? undefined);
1486
+ const member = invite.memberInvite
1487
+ ? await this._joinBirthMember(candidate, this._deviceKeyPair.publicKey)
1488
+ : undefined;
1489
+ const claim = {
1490
+ deviceKeyPair: this._deviceKeyPair,
1491
+ ...(member || !invite.memberId
1492
+ ? {}
1493
+ : { memberId: invite.memberId, inviteNonce: invite.inviteNonce })
1494
+ };
1495
+ await candidate.appendBirthOp(this._deviceKeyPair.publicKey, this._localDevice ?? undefined, member, claim);
1251
1496
  this._throwIfJoinCanceled();
1252
1497
  if (!candidate.key.equals(invite.baseKey)) {
1253
1498
  throw new Error('Device is not permitted to join the invited mesh');
@@ -1307,28 +1552,165 @@ export class Core extends ReadyResource {
1307
1552
  offClose = this._onClose(onCanceled);
1308
1553
  });
1309
1554
  }
1310
- _activateMesh(mesh, session) {
1555
+ _onMeshWritable(mesh) {
1556
+ if (!this._isPooled(mesh))
1557
+ return;
1558
+ if (this._isActive(mesh))
1559
+ this._kicked = false;
1560
+ this._notifyStatus();
1561
+ }
1562
+ _onMeshDead(mesh, err) {
1563
+ if (this.closing || !this._isPooled(mesh))
1564
+ return;
1565
+ this._log?.error(['p2p', 'mesh'], 'mesh autobee died - retiring it', err);
1566
+ if (this._isActive(mesh)) {
1567
+ this._statusError = statusErrorMessage('the local database died', err);
1568
+ }
1569
+ this._retirePooledMesh(mesh).catch((retireErr) => {
1570
+ this._log?.warn(['p2p', 'mesh'], 'retiring the dead mesh failed', retireErr);
1571
+ });
1572
+ this._notifyStatus();
1573
+ }
1574
+ _onMeshStalled(mesh, err) {
1575
+ if (this._isPooled(mesh))
1576
+ this._withdrawFromMesh(mesh, err);
1577
+ }
1578
+ _wireMesh(entry) {
1579
+ if (entry.handlers)
1580
+ return;
1581
+ const { mesh } = entry;
1582
+ const handlers = {
1583
+ writable: () => this._onMeshWritable(mesh),
1584
+ dead: (err) => this._onMeshDead(mesh, err),
1585
+ stalled: (err) => this._onMeshStalled(mesh, err)
1586
+ };
1587
+ entry.handlers = handlers;
1588
+ mesh.on('kicked', () => this._onKicked(mesh));
1589
+ mesh.on('unattested-device', () => void this._settleMembership(mesh));
1590
+ mesh.on('rooms-dirty', () => void this._followJoinedRooms(mesh));
1591
+ mesh.on('membership', (own) => {
1592
+ void this._recordMemberProof(mesh, own);
1593
+ void this._followJoinedRooms(mesh);
1594
+ });
1595
+ mesh.autobee.on('writable', handlers.writable);
1596
+ mesh.autobee.on('error', handlers.dead);
1597
+ mesh.autobee.on('close', handlers.dead);
1598
+ mesh.autobee.on('interrupt', handlers.stalled);
1599
+ this._startMeshServices(entry);
1600
+ }
1601
+ async _handOffActive(incoming, outgoing) {
1602
+ const stopped = outgoing && outgoing.mesh !== incoming ? outgoing : null;
1603
+ if (stopped) {
1604
+ await this._stopEntryIndexing(stopped);
1605
+ this._releaseVectorIndex(stopped);
1606
+ }
1607
+ try {
1608
+ await this._activateMesh(incoming);
1609
+ }
1610
+ catch (err) {
1611
+ if (stopped)
1612
+ this._startEntryIndexing(stopped);
1613
+ throw err;
1614
+ }
1615
+ }
1616
+ async _activateMesh(mesh) {
1311
1617
  if (this._candidateMesh === mesh)
1312
1618
  this._candidateMesh = null;
1313
- this._mesh = mesh;
1314
- this._meshSecretSeed = session.seed;
1619
+ await this.local.setActiveMesh(this._deviceKeyPair.publicKey, mesh.key);
1620
+ this._wireMesh(this._poolEntry(mesh));
1621
+ this._activeMeshKey = this._poolKey(mesh);
1315
1622
  this._statusError = null;
1316
1623
  this._upgradeRequired = null;
1317
1624
  void this._closeConfigPeering().catch(noop);
1318
1625
  if (mesh.writable)
1319
1626
  this._kicked = false;
1320
- mesh.on('kicked', () => this._onKicked());
1321
- mesh.on('unattested-device', () => void this._settleMembership(mesh));
1322
- mesh.on('membership', (own) => void this._recordMembership(own));
1323
- mesh.autobee.on('writable', this._onMeshWritable);
1324
- mesh.autobee.on('error', this._onMeshDead);
1325
- mesh.autobee.on('close', this._onMeshDead);
1326
- mesh.autobee.on('interrupt', this._onMeshStalled);
1327
- this._startMeshServices(mesh);
1627
+ this._startIndexing();
1328
1628
  this._notifyStatus();
1329
1629
  }
1330
- _currentMesh() {
1331
- return this._mesh;
1630
+ _currentMesh(meshKey = this._activeMeshKey) {
1631
+ if (meshKey === null)
1632
+ return null;
1633
+ return this._meshes.get(meshKey)?.mesh ?? null;
1634
+ }
1635
+ _poolKey(mesh) {
1636
+ return mesh.key.toString('hex');
1637
+ }
1638
+ _poolEntry(mesh) {
1639
+ const key = this._poolKey(mesh);
1640
+ const existing = this._meshes.get(key);
1641
+ if (existing?.mesh === mesh)
1642
+ return existing;
1643
+ const entry = {
1644
+ mesh,
1645
+ handlers: null,
1646
+ hostedAgents: null,
1647
+ processingFiles: null,
1648
+ vectorIndex: null,
1649
+ memberLock: new ScopeLock({ debounce: true })
1650
+ };
1651
+ this._meshes.set(key, entry);
1652
+ return entry;
1653
+ }
1654
+ _meshFor(meshId) {
1655
+ return meshId ? this._currentMesh(meshId) : this._currentMesh();
1656
+ }
1657
+ _activeEntry() {
1658
+ if (this._activeMeshKey === null)
1659
+ return null;
1660
+ return this._meshes.get(this._activeMeshKey) ?? null;
1661
+ }
1662
+ _meshForBlobCore(coreKey) {
1663
+ if (!coreKey)
1664
+ return this._currentMesh();
1665
+ for (const { mesh } of this._meshes.values()) {
1666
+ if (mesh.blobs.core.key.toString('hex') === coreKey)
1667
+ return mesh;
1668
+ }
1669
+ return this._currentMesh(this._blobCoreMeshes.get(coreKey) ?? null);
1670
+ }
1671
+ _linkAttachment(attachment, mesh) {
1672
+ const url = this._blobServer?.link(attachment, mesh) ?? null;
1673
+ if (url) {
1674
+ const coreKey = parseBlobReferenceCoreKey(attachment.blobId);
1675
+ if (coreKey)
1676
+ this._blobCoreMeshes.set(coreKey, this._poolKey(mesh));
1677
+ }
1678
+ return url;
1679
+ }
1680
+ _isPooled(mesh) {
1681
+ return !mesh.closing && this._meshes.get(this._poolKey(mesh))?.mesh === mesh;
1682
+ }
1683
+ _isActive(mesh) {
1684
+ return this._currentMesh() === mesh;
1685
+ }
1686
+ _unpoolMesh(mesh) {
1687
+ const key = this._poolKey(mesh);
1688
+ const entry = this._meshes.get(key);
1689
+ let wasActive = false;
1690
+ if (entry?.mesh === mesh) {
1691
+ this._unwireMesh(entry);
1692
+ this._meshes.delete(key);
1693
+ if (this._activeMeshKey === key) {
1694
+ this._activeMeshKey = null;
1695
+ wasActive = true;
1696
+ }
1697
+ for (const [coreKey, poolKey] of this._blobCoreMeshes) {
1698
+ if (poolKey === key)
1699
+ this._blobCoreMeshes.delete(coreKey);
1700
+ }
1701
+ }
1702
+ if (this._candidateMesh === mesh)
1703
+ this._candidateMesh = null;
1704
+ if (wasActive)
1705
+ this._notifyStatus();
1706
+ }
1707
+ _unwireMesh(entry) {
1708
+ if (!entry.handlers)
1709
+ return;
1710
+ entry.mesh.autobee.off('writable', entry.handlers.writable);
1711
+ entry.mesh.autobee.off('error', entry.handlers.dead);
1712
+ entry.mesh.autobee.off('close', entry.handlers.dead);
1713
+ entry.mesh.autobee.off('interrupt', entry.handlers.stalled);
1332
1714
  }
1333
1715
  async _awaitMesh() {
1334
1716
  if (this.closing)
@@ -1337,7 +1719,8 @@ export class Core extends ReadyResource {
1337
1719
  await this.ready();
1338
1720
  return this._currentMesh();
1339
1721
  }
1340
- _startMeshServices(mesh) {
1722
+ _startMeshServices(entry) {
1723
+ const { mesh } = entry;
1341
1724
  const deviceId = this._deviceKeyPair.publicKey;
1342
1725
  mesh.on('notification', (event) => this.emit('notification', event));
1343
1726
  void mesh.refreshMembership().catch((error) => {
@@ -1367,8 +1750,7 @@ export class Core extends ReadyResource {
1367
1750
  };
1368
1751
  void sweep();
1369
1752
  mesh.autobee.on('move-to', () => void sweep());
1370
- this._startIndexing();
1371
- this._hostedAgents = new HostedAgents({
1753
+ entry.hostedAgents = new HostedAgents({
1372
1754
  mesh,
1373
1755
  local: this.local,
1374
1756
  deviceId,
@@ -1377,11 +1759,11 @@ export class Core extends ReadyResource {
1377
1759
  onmoved: () => this._skillCatalog?.(null, null, { refresh: true }) ?? Promise.resolve(),
1378
1760
  log: this._log
1379
1761
  });
1380
- this._hostedAgents.start();
1762
+ entry.hostedAgents.start();
1381
1763
  }
1382
- async _stopHostedAgents() {
1383
- const hosted = this._hostedAgents;
1384
- this._hostedAgents = null;
1764
+ async _stopHostedAgents(entry) {
1765
+ const hosted = entry.hostedAgents;
1766
+ entry.hostedAgents = null;
1385
1767
  await hosted?.close();
1386
1768
  }
1387
1769
  async _discardCandidateMesh() {
@@ -1409,7 +1791,7 @@ export class Core extends ReadyResource {
1409
1791
  await snap.close();
1410
1792
  }
1411
1793
  }
1412
- async _announceDeparture(mesh = this._mesh, strict = false) {
1794
+ async _announceDeparture(mesh = this._currentMesh(), strict = false) {
1413
1795
  if (!mesh?.writable) {
1414
1796
  if (strict)
1415
1797
  throw new Error('Mesh departure requires a writable source');
@@ -1439,13 +1821,30 @@ export class Core extends ReadyResource {
1439
1821
  return;
1440
1822
  if (this._minting)
1441
1823
  throw new Error('Mesh transition already in progress');
1824
+ const departing = this._currentMesh();
1825
+ if (!departing)
1826
+ return;
1827
+ const others = (await this.local.listMeshes()).filter((row) => !row.key.equals(departing.key));
1828
+ if (others.length > 0 && (await this._isHomeMesh(departing))) {
1829
+ throw new Error(LEAVE_HOME_REFUSED);
1830
+ }
1442
1831
  this._leaving = true;
1443
1832
  this._notifyStatus();
1444
- const landed = this._waitForMeshChange();
1445
1833
  try {
1446
- if (await this._departureHasAudience(mesh))
1447
- await this.local.recordDeparture(mesh.key);
1448
- await this._announceDeparture();
1834
+ if (await this._departureHasAudience(departing)) {
1835
+ await this.local.recordDeparture(departing.key, { creator: departing.minted });
1836
+ }
1837
+ await this._announceDeparture(departing);
1838
+ await this._releaseJoinedRoom(departing.key);
1839
+ await this._archiveMembership(departing);
1840
+ const fallback = await this._writableFallback(others);
1841
+ if (fallback) {
1842
+ await this._handOffActive(fallback, this._activeEntry());
1843
+ await this._retirePooledMesh(departing);
1844
+ this._departures?.flush();
1845
+ return;
1846
+ }
1847
+ const landed = this._waitForMeshChange();
1449
1848
  this._requestFreshMesh().catch(() => { });
1450
1849
  const error = await landed;
1451
1850
  if (error)
@@ -1456,6 +1855,280 @@ export class Core extends ReadyResource {
1456
1855
  this._notifyStatus();
1457
1856
  }
1458
1857
  }
1858
+ async _isHomeMesh(mesh) {
1859
+ const home = await this._homeMeshKey();
1860
+ return home !== undefined && home.equals(mesh.key);
1861
+ }
1862
+ async _homeMeshKey() {
1863
+ return (await this.local.homeMesh(this._deviceKeyPair.publicKey))?.key;
1864
+ }
1865
+ async _writableFallback(rows) {
1866
+ const home = await this._homeMeshKey();
1867
+ const preferred = (row) => Number(!!home?.equals(row.key));
1868
+ for (const row of [...rows].sort((a, b) => preferred(b) - preferred(a))) {
1869
+ const meshId = row.key.toString('hex');
1870
+ const held = this._currentMesh(meshId);
1871
+ const mesh = await this._openPooledMesh(meshId).catch(() => null);
1872
+ if (mesh?.writable)
1873
+ return mesh;
1874
+ if (mesh && !held)
1875
+ await this._retirePooledMesh(mesh).catch(noop);
1876
+ }
1877
+ return null;
1878
+ }
1879
+ async listMeshes() {
1880
+ if (!this.opened)
1881
+ await this.ready();
1882
+ const home = await this._homeMeshKey();
1883
+ const rows = await this.local.listMeshes();
1884
+ const labels = await this._meshLabels(home);
1885
+ return { meshes: rows.map((row) => this._meshEntry(row, home, labels)) };
1886
+ }
1887
+ async _meshLabels(home) {
1888
+ const labels = new Map();
1889
+ const registry = home ? this._currentMesh(home.toString('hex')) : null;
1890
+ const ours = registry?.ourMemberId;
1891
+ if (!registry || !ours)
1892
+ return labels;
1893
+ for await (const room of registry.view.find(JOINED_MESHES)) {
1894
+ if (room.label && room.memberId.equals(ours)) {
1895
+ labels.set(room.meshKey.toString('hex'), room.label);
1896
+ }
1897
+ }
1898
+ return labels;
1899
+ }
1900
+ async _isOwnRoomIn(registry, meshKey) {
1901
+ const ours = registry.ourMemberId;
1902
+ if (!ours)
1903
+ return false;
1904
+ const room = await registry.view.get(JOINED_MESHES, { meshKey });
1905
+ return Boolean(room) && !room.leftAt && room.memberId.equals(ours);
1906
+ }
1907
+ async setMeshLabel(meshId, label) {
1908
+ if (!this.opened)
1909
+ await this.ready();
1910
+ const meshKey = this._meshKeyOf(meshId);
1911
+ const registry = await this._accountMesh();
1912
+ if (!registry)
1913
+ throw new Error(ROOM_WITHOUT_ACCOUNT);
1914
+ const ours = registry.ourMemberId;
1915
+ const room = await registry.view.get(JOINED_MESHES, { meshKey });
1916
+ if (!room?.memberId.equals(ours))
1917
+ throw new Error(`${NO_SUCH_MESH}: ${meshId}`);
1918
+ await registry.dispatch('put-joined-mesh', {
1919
+ room: {
1920
+ meshKey,
1921
+ memberId: ours,
1922
+ joinedAt: room.joinedAt,
1923
+ leftAt: room.leftAt,
1924
+ label,
1925
+ labeledAt: Math.max(Date.now(), (room.labeledAt ?? 0) + 1),
1926
+ shared: room.shared,
1927
+ sharedAt: room.sharedAt
1928
+ }
1929
+ });
1930
+ this._notifyStatus();
1931
+ }
1932
+ async createRoom({ shared = false } = {}) {
1933
+ if (!this.opened)
1934
+ await this.ready();
1935
+ await this._meshTransitionLock.lock();
1936
+ try {
1937
+ return await this._mintRoom(shared);
1938
+ }
1939
+ finally {
1940
+ this._meshTransitionLock.unlock();
1941
+ }
1942
+ }
1943
+ async sharedRoom() {
1944
+ if (!this.opened)
1945
+ await this.ready();
1946
+ await this._meshTransitionLock.lock();
1947
+ try {
1948
+ const registry = await this._accountMesh();
1949
+ if (!registry)
1950
+ throw new Error(ROOM_WITHOUT_ACCOUNT);
1951
+ const resolved = await this._resolveSharedRoom(registry);
1952
+ return resolved ?? { meshId: await this._mintRoom(true, registry) };
1953
+ }
1954
+ finally {
1955
+ this._meshTransitionLock.unlock();
1956
+ }
1957
+ }
1958
+ async _occupancyNotice(meshId) {
1959
+ const mesh = this._currentMesh(meshId);
1960
+ const ours = mesh?.ourMemberId;
1961
+ if (!mesh)
1962
+ return undefined;
1963
+ const snap = mesh.view.snapshot();
1964
+ try {
1965
+ for await (const member of snap.find('@qvac/members')) {
1966
+ if (!ours || !member.id.equals(ours))
1967
+ return ROOM_NOT_EMPTY;
1968
+ }
1969
+ for await (const _chunk of snap.find('@qvac/chunks', { limit: 1 })) {
1970
+ return ROOM_NOT_EMPTY;
1971
+ }
1972
+ return undefined;
1973
+ }
1974
+ finally {
1975
+ await snap.close();
1976
+ }
1977
+ }
1978
+ async _resolveSharedRoom(registry) {
1979
+ const ours = registry.ourMemberId;
1980
+ if (!ours)
1981
+ return null;
1982
+ const marked = [];
1983
+ for await (const room of registry.view.find(JOINED_MESHES)) {
1984
+ if (room.shared && !room.leftAt && room.memberId.equals(ours))
1985
+ marked.push(room);
1986
+ }
1987
+ if (marked.length === 0)
1988
+ return null;
1989
+ marked.sort((a, b) => a.joinedAt - b.joinedAt || Buffer.compare(a.meshKey, b.meshKey));
1990
+ const [winner, ...losers] = marked;
1991
+ for (const loser of losers)
1992
+ await this._unmarkSharedRoom(registry, loser);
1993
+ return { meshId: winner.meshKey.toString('hex'), notice: unmarkedNotice(losers) };
1994
+ }
1995
+ async _unmarkSharedRoom(registry, room) {
1996
+ await registry.dispatch('put-joined-mesh', {
1997
+ room: {
1998
+ meshKey: room.meshKey,
1999
+ memberId: room.memberId,
2000
+ joinedAt: room.joinedAt,
2001
+ label: room.label,
2002
+ labeledAt: room.labeledAt,
2003
+ shared: false,
2004
+ sharedAt: Math.max(Date.now(), (room.sharedAt ?? 0) + 1)
2005
+ }
2006
+ });
2007
+ }
2008
+ async _mintRoom(shared, resolved) {
2009
+ if (this.closing)
2010
+ throw new Error('Core is closing');
2011
+ if (this._blockedByMinVersion())
2012
+ throw new Error(MESH_BELOW_FLOOR);
2013
+ if (this._leaving || this._minting)
2014
+ throw new Error('Mesh transition already in progress');
2015
+ const registry = resolved ?? (await this._accountMesh());
2016
+ if (!registry)
2017
+ throw new Error(ROOM_WITHOUT_ACCOUNT);
2018
+ const mesh = await this._openMesh({ new: true, room: true });
2019
+ this._wireMesh(this._poolEntry(mesh));
2020
+ this._bringMeshOnline(mesh, { new: true });
2021
+ try {
2022
+ await this._registerRoom(mesh.key, shared, registry);
2023
+ }
2024
+ catch (error) {
2025
+ await this._discardRoom(mesh);
2026
+ throw error;
2027
+ }
2028
+ this._notifyStatus();
2029
+ return this._poolKey(mesh);
2030
+ }
2031
+ async _discardRoom(mesh) {
2032
+ try {
2033
+ await this.local.archiveMesh(mesh.key);
2034
+ await this._retirePooledMesh(mesh);
2035
+ }
2036
+ catch (error) {
2037
+ this._log?.error(['p2p', 'mesh'], 'an unregistered room was left behind', error);
2038
+ }
2039
+ }
2040
+ async openMesh(meshId) {
2041
+ await this._openPooledMesh(meshId);
2042
+ this._notifyStatus();
2043
+ }
2044
+ async selectMesh(meshId) {
2045
+ if (!this.opened)
2046
+ await this.ready();
2047
+ await this._meshTransitionLock.lock();
2048
+ try {
2049
+ if (this.closing)
2050
+ throw new Error('Core is closing');
2051
+ if (this._leaving || this._minting)
2052
+ throw new Error('Mesh transition already in progress');
2053
+ if (meshId === this._activeMeshKey)
2054
+ return;
2055
+ const outgoing = this._activeEntry();
2056
+ await this._handOffActive(await this._openPooledMesh(meshId), outgoing);
2057
+ }
2058
+ finally {
2059
+ this._meshTransitionLock.unlock();
2060
+ }
2061
+ }
2062
+ async closeMesh(meshId) {
2063
+ if (!this.opened)
2064
+ await this.ready();
2065
+ await this._meshTransitionLock.lock();
2066
+ try {
2067
+ if (meshId === this._activeMeshKey)
2068
+ throw new Error('The active mesh cannot be closed');
2069
+ const mesh = this._currentMesh(meshId);
2070
+ if (!mesh)
2071
+ return;
2072
+ await this._retirePooledMesh(mesh);
2073
+ }
2074
+ finally {
2075
+ this._meshTransitionLock.unlock();
2076
+ }
2077
+ this._notifyStatus();
2078
+ }
2079
+ async _openPooledMesh(meshId) {
2080
+ if (!this.opened)
2081
+ await this.ready();
2082
+ if (this.closing)
2083
+ throw new Error('Core is closing');
2084
+ if (this._blockedByMinVersion())
2085
+ throw new Error(MESH_BELOW_FLOOR);
2086
+ const pooled = this._currentMesh(meshId);
2087
+ if (pooled)
2088
+ return pooled;
2089
+ const inflight = this._opening.get(meshId);
2090
+ if (inflight)
2091
+ return inflight;
2092
+ const opening = this._openRecordedMesh(meshId).finally(() => this._opening.delete(meshId));
2093
+ this._opening.set(meshId, opening);
2094
+ return opening;
2095
+ }
2096
+ async _openRecordedMesh(meshId) {
2097
+ const session = await this.local.readMeshSession(this._meshKeyOf(meshId));
2098
+ if (!session)
2099
+ throw new Error(`${NO_SUCH_MESH}: ${meshId}`);
2100
+ const mesh = await this._openMeshSession(session);
2101
+ this._wireMesh(this._poolEntry(mesh));
2102
+ this._bringMeshOnline(mesh);
2103
+ return mesh;
2104
+ }
2105
+ _meshEntry(row, home, labels) {
2106
+ const meshId = row.key.toString('hex');
2107
+ const mesh = this._currentMesh(meshId);
2108
+ const active = meshId === this._activeMeshKey;
2109
+ return {
2110
+ meshId,
2111
+ state: this._meshEntryState(mesh, active),
2112
+ writable: mesh?.writable ?? false,
2113
+ open: mesh !== null,
2114
+ active,
2115
+ home: !!home?.equals(row.key),
2116
+ label: labels.get(meshId),
2117
+ members: mesh?.membership.members ?? 0
2118
+ };
2119
+ }
2120
+ _meshEntryState(mesh, active) {
2121
+ if (active)
2122
+ return this._statusState();
2123
+ if (!mesh)
2124
+ return 'idle';
2125
+ return mesh.writable ? 'joined' : 'joining';
2126
+ }
2127
+ _meshKeyOf(meshId) {
2128
+ if (!MESH_ID.test(meshId))
2129
+ throw new Error(`${NO_SUCH_MESH}: ${meshId}`);
2130
+ return Buffer.from(meshId, 'hex');
2131
+ }
1459
2132
  async removeDeviceWriter(writerKey, deviceId) {
1460
2133
  const mesh = await this._awaitMesh();
1461
2134
  if (!mesh)
@@ -1492,16 +2165,45 @@ export class Core extends ReadyResource {
1492
2165
  return;
1493
2166
  await Promise.race([mesh.waitForWritable(), this._closeSignal]);
1494
2167
  }
1495
- async getMeshInvite() {
1496
- const mesh = await this._awaitMesh();
1497
- if (!mesh || !this._meshSecretSeed)
2168
+ async _assertRoomIsHere(meshId) {
2169
+ const meshKey = this._meshKeyOf(meshId);
2170
+ if (await this.local.readMeshSession(meshKey))
2171
+ return;
2172
+ const registry = await this._accountMesh();
2173
+ if (registry && (await this._isOwnRoomIn(registry, meshKey))) {
2174
+ throw new Error(ROOM_HELD_BY_SIBLING);
2175
+ }
2176
+ }
2177
+ async getMeshInvite(opts = {}) {
2178
+ if (opts.memberInvite && opts.meshId)
2179
+ await this._assertRoomIsHere(opts.meshId);
2180
+ const mesh = opts.meshId ? await this._openPooledMesh(opts.meshId) : await this._awaitMesh();
2181
+ if (!mesh)
1498
2182
  return null;
2183
+ const registry = await this._accountMesh();
2184
+ if (!registry?.ourMemberId)
2185
+ throw new Error(INVITE_ACCOUNT_UNREADABLE);
2186
+ const room = await this._isOwnRoomIn(registry, mesh.key);
2187
+ if (opts.memberInvite) {
2188
+ if (!room)
2189
+ throw new Error(MEMBER_INVITE_HOME_REFUSED);
2190
+ }
2191
+ else {
2192
+ if (room)
2193
+ throw new Error(DEVICE_INVITE_ROOM_REFUSED);
2194
+ const home = await this._homeMeshKey();
2195
+ if (!home)
2196
+ throw new Error(INVITE_ACCOUNT_UNREADABLE);
2197
+ if (!home.equals(mesh.key))
2198
+ throw new Error(DEVICE_INVITE_ROOM_REFUSED);
2199
+ }
1499
2200
  const auto = mesh.autobee;
1500
2201
  const info = await auto.system.getInfo();
1501
2202
  const head = info && info.view.length > 0 ? await auto.trusted.mostRecentTrusted(auto.view, null) : null;
1502
2203
  const additionalNodes = this._swarm
1503
2204
  ? this._swarm.dht.toArray({ limit: INVITE_DHT_NODES_LIMIT })
1504
2205
  : [];
2206
+ const memberId = opts.memberInvite ? undefined : (mesh.ourMemberId ?? undefined);
1505
2207
  const snap = mesh.view.snapshot();
1506
2208
  let blindPeerRows;
1507
2209
  try {
@@ -1514,14 +2216,18 @@ export class Core extends ReadyResource {
1514
2216
  }
1515
2217
  return {
1516
2218
  baseKey: mesh.key,
1517
- seed: this._meshSecretSeed,
2219
+ seed: mesh.seed,
1518
2220
  fastForward: head ? { key: head.key, length: head.length } : null,
1519
2221
  additionalNodes,
1520
- blindPeers: blindPeerRows.map((row) => row.key)
2222
+ blindPeers: blindPeerRows.map((row) => row.key),
2223
+ memberInvite: opts.memberInvite ?? false,
2224
+ memberId,
2225
+ inviteNonce: memberId ? await this._deviceInvites.issue() : undefined
1521
2226
  };
1522
2227
  }
1523
2228
  async _closeSwarm() {
1524
2229
  this._wakeup.destroy();
2230
+ this._meshSeedHandoff.destroy();
1525
2231
  this._secretHandoff.destroy();
1526
2232
  this._skillOverlayHandoff.destroy();
1527
2233
  if (!this._swarm)
@@ -1535,7 +2241,7 @@ export class Core extends ReadyResource {
1535
2241
  async pushSkillSecret(request) {
1536
2242
  if (!this.opened)
1537
2243
  await this.ready();
1538
- const mesh = this._mesh;
2244
+ const mesh = this._currentMesh();
1539
2245
  const deviceId = this.devicePublicKey;
1540
2246
  if (!mesh || !deviceId)
1541
2247
  throw new Error(NOT_THE_HOST);
@@ -1553,7 +2259,7 @@ export class Core extends ReadyResource {
1553
2259
  return this._secretHandoff.send(agent.providerDeviceId, request);
1554
2260
  }
1555
2261
  async _storeHandedOffSecret(request, fromDeviceId) {
1556
- const mesh = this._mesh;
2262
+ const mesh = this._currentMesh();
1557
2263
  const deviceId = this.devicePublicKey;
1558
2264
  if (!mesh || !deviceId)
1559
2265
  throw new Error(NOT_THE_HOST);
@@ -1566,6 +2272,104 @@ export class Core extends ReadyResource {
1566
2272
  });
1567
2273
  return receive(request, fromDeviceId);
1568
2274
  }
2275
+ async _followJoinedRooms(memberMesh) {
2276
+ if (this.closing || !this._isPooled(memberMesh))
2277
+ return;
2278
+ const ours = memberMesh.ourMemberId;
2279
+ if (!ours)
2280
+ return;
2281
+ for await (const room of memberMesh.view.find(JOINED_MESHES)) {
2282
+ if (this.closing || !this._isPooled(memberMesh))
2283
+ return;
2284
+ if (!room.memberId.equals(ours) || room.leftAt)
2285
+ continue;
2286
+ const meshId = room.meshKey.toString('hex');
2287
+ if (this._currentMesh(meshId) || this._following.has(meshId))
2288
+ continue;
2289
+ this._following.add(meshId);
2290
+ try {
2291
+ if (await this.local.readMeshSession(room.meshKey))
2292
+ continue;
2293
+ await this._openFollowedRoom(memberMesh, room.meshKey, ours);
2294
+ }
2295
+ catch (error) {
2296
+ this._log?.warn(['p2p', 'mesh'], 'following a joined room failed', error);
2297
+ }
2298
+ finally {
2299
+ this._following.delete(meshId);
2300
+ }
2301
+ }
2302
+ }
2303
+ async _openFollowedRoom(memberMesh, meshKey, ours) {
2304
+ const seed = await this._requestRoomSeed(memberMesh, meshKey, ours);
2305
+ if (!seed)
2306
+ return;
2307
+ if (this.closing || this._blockedByMinVersion())
2308
+ return;
2309
+ const session = this.local.createCandidateSession(seed, meshKey);
2310
+ await this.local.recordMesh(this._deviceKeyPair.publicKey, meshKey, session);
2311
+ await this._openPooledMesh(meshKey.toString('hex'));
2312
+ this._notifyStatus();
2313
+ }
2314
+ async _requestRoomSeed(memberMesh, meshKey, ours) {
2315
+ const self = this._deviceKeyPair?.publicKey;
2316
+ for await (const device of memberMesh.view.find('@qvac/devices')) {
2317
+ if (this.closing)
2318
+ return null;
2319
+ if (device.revoked || !device.memberId?.equals(ours))
2320
+ continue;
2321
+ if (self && device.id.equals(self))
2322
+ continue;
2323
+ const seed = await this._meshSeedHandoff.request(device.id, meshKey).catch(() => null);
2324
+ if (seed)
2325
+ return seed;
2326
+ }
2327
+ return null;
2328
+ }
2329
+ async _serveMeshSeed(request, fromDeviceId) {
2330
+ const asker = Buffer.from(fromDeviceId);
2331
+ if (!(await this._isOwnMemberDevice(asker))) {
2332
+ this._log?.warn(['p2p', 'mesh'], 'seed request refused: not a device of this member');
2333
+ return null;
2334
+ }
2335
+ if (!(await this._isJoinedRoomKey(request.meshKey))) {
2336
+ this._log?.warn(['p2p', 'mesh'], 'seed request refused: not a room this member joined');
2337
+ return null;
2338
+ }
2339
+ const session = await this.local.readMeshSession(request.meshKey);
2340
+ return session?.seed ?? null;
2341
+ }
2342
+ async _isJoinedRoomKey(meshKey) {
2343
+ for (const entry of this._meshes.values()) {
2344
+ const ours = entry.mesh.ourMemberId;
2345
+ if (!ours)
2346
+ continue;
2347
+ const row = await entry.mesh.view
2348
+ .get(JOINED_MESHES, { meshKey })
2349
+ .catch(() => null);
2350
+ if (row?.memberId.equals(ours) && !row.leftAt)
2351
+ return true;
2352
+ }
2353
+ return false;
2354
+ }
2355
+ async _isOwnMemberDevice(deviceId) {
2356
+ let ourDevice = false;
2357
+ for (const entry of this._meshes.values()) {
2358
+ const ours = entry.mesh.ourMemberId;
2359
+ if (!ours)
2360
+ continue;
2361
+ const device = await entry.mesh.view
2362
+ .get('@qvac/devices', { id: deviceId })
2363
+ .catch(() => null);
2364
+ if (!device)
2365
+ continue;
2366
+ if (device.revoked)
2367
+ return false;
2368
+ if (device.memberId?.equals(ours))
2369
+ ourDevice = true;
2370
+ }
2371
+ return ourDevice;
2372
+ }
1569
2373
  async _skillOverlayOp(agentId, op) {
1570
2374
  const host = await this._resolveSkillOverlayHost(agentId);
1571
2375
  if (!host)
@@ -1577,7 +2381,7 @@ export class Core extends ReadyResource {
1577
2381
  return null;
1578
2382
  if (!this.opened)
1579
2383
  await this.ready();
1580
- const mesh = this._mesh;
2384
+ const mesh = this._currentMesh();
1581
2385
  const deviceId = this.devicePublicKey;
1582
2386
  if (!mesh || !deviceId)
1583
2387
  throw new Error(NOT_THE_HOST);
@@ -1594,7 +2398,7 @@ export class Core extends ReadyResource {
1594
2398
  return agent.providerDeviceId;
1595
2399
  }
1596
2400
  async _serveSkillOverlayOp(op, fromDeviceId) {
1597
- const mesh = this._mesh;
2401
+ const mesh = this._currentMesh();
1598
2402
  const deviceId = this.devicePublicKey;
1599
2403
  if (!mesh || !deviceId)
1600
2404
  throw new Error(NOT_THE_HOST);
@@ -1682,9 +2486,15 @@ export class Core extends ReadyResource {
1682
2486
  this._wakeup.addStream(conn);
1683
2487
  this._secretHandoff.addStream(conn);
1684
2488
  this._skillOverlayHandoff.addStream(conn);
1685
- this._hostedAgents?.kick();
2489
+ this._meshSeedHandoff.addStream(conn);
2490
+ for (const entry of this._meshes.values()) {
2491
+ entry.hostedAgents?.kick();
2492
+ if (entry.mesh.ourMemberId)
2493
+ void this._followJoinedRooms(entry.mesh);
2494
+ }
1686
2495
  this.rootCorestore.replicate(conn);
1687
- this._mesh?.files.reconnect();
2496
+ for (const entry of this._meshes.values())
2497
+ entry.mesh.files.reconnect();
1688
2498
  }
1689
2499
  _trackPeerConnection(conn) {
1690
2500
  this._peers.add(conn);
@@ -1716,10 +2526,26 @@ export class Core extends ReadyResource {
1716
2526
  acknowledgeKicked: () => this.acknowledgeKicked(),
1717
2527
  join: (invite) => this.joinMesh(invite),
1718
2528
  cancelJoin: () => this.cancelMeshJoin(),
1719
- invite: async () => {
1720
- const invite = await this.getMeshInvite();
1721
- return invite ? encodeMeshInvite(invite) : null;
1722
- }
2529
+ invite: async (member, meshId) => {
2530
+ if (!member) {
2531
+ const device = await this.getMeshInvite({ meshId });
2532
+ return { invite: device ? encodeMeshInvite(device) : undefined };
2533
+ }
2534
+ const room = meshId ? { meshId } : await this.sharedRoom();
2535
+ const invite = await this.getMeshInvite({ memberInvite: true, meshId: room.meshId });
2536
+ const notices = [room.notice, await this._occupancyNotice(room.meshId)];
2537
+ return {
2538
+ invite: invite ? encodeMeshInvite(invite) : undefined,
2539
+ meshId: room.meshId,
2540
+ notice: notices.filter(Boolean).join('\n') || undefined
2541
+ };
2542
+ },
2543
+ list: () => this.listMeshes(),
2544
+ open: (meshId) => this.openMesh(meshId),
2545
+ select: (meshId) => this.selectMesh(meshId),
2546
+ close: (meshId) => this.closeMesh(meshId),
2547
+ label: (meshId, label) => this.setMeshLabel(meshId, label),
2548
+ createRoom: () => this.createRoom()
1723
2549
  };
1724
2550
  }
1725
2551
  _networkStatusSource() {
@@ -1911,7 +2737,6 @@ export class Core extends ReadyResource {
1911
2737
  registerEmbedder(embedder, extractors) {
1912
2738
  this._embedder = embedder;
1913
2739
  this._ragExtractors = extractors ?? this._ragExtractors;
1914
- this._vectorIndex = this._vectorIndex ?? new MemoryVectorIndex();
1915
2740
  const settled = this._reconcileIndexing();
1916
2741
  settled.catch((err) => this._log?.warn(['indexing'], 'reconcile failed', err));
1917
2742
  return () => {
@@ -1936,24 +2761,32 @@ export class Core extends ReadyResource {
1936
2761
  }
1937
2762
  }
1938
2763
  _startIndexing() {
1939
- if (!this._mesh || !this._embedder || this._processingFiles)
2764
+ const entry = this._activeEntry();
2765
+ if (entry)
2766
+ this._startEntryIndexing(entry);
2767
+ }
2768
+ _startEntryIndexing(entry) {
2769
+ if (!this._embedder || entry.processingFiles)
1940
2770
  return;
1941
- this._vectorIndex ??= new MemoryVectorIndex();
1942
- this._processingFiles = this._mesh.files;
1943
- this._processingFiles.startProcessing({
2771
+ entry.vectorIndex ??= this._takeVectorIndex();
2772
+ entry.processingFiles = entry.mesh.files;
2773
+ entry.processingFiles.startProcessing({
1944
2774
  embedder: this._embedder,
1945
- index: this._vectorIndex,
2775
+ index: entry.vectorIndex,
1946
2776
  deviceId: this.devicePublicKey,
1947
2777
  extractors: this._ragExtractors
1948
2778
  });
1949
2779
  this._indexedEmbedder = this._embedder;
1950
2780
  }
2781
+ _takeVectorIndex() {
2782
+ const index = this._spareVectorIndex ?? new MemoryVectorIndex();
2783
+ this._spareVectorIndex = null;
2784
+ return index;
2785
+ }
1951
2786
  async _stopIndexing() {
1952
2787
  this._indexedEmbedder = null;
1953
- if (this._processingFiles && !(await this._processingFiles.stopProcessing())) {
1954
- this._vectorIndex = null;
1955
- }
1956
- this._processingFiles = null;
2788
+ for (const entry of this._meshes.values())
2789
+ await this._stopEntryIndexing(entry);
1957
2790
  }
1958
2791
  registerTranscribe(hook) {
1959
2792
  this._transcribe = hook;
@@ -2006,15 +2839,16 @@ export class Core extends ReadyResource {
2006
2839
  };
2007
2840
  }
2008
2841
  _memberDiagnostics(mesh) {
2009
- const memberId = mesh.ourMemberId?.toString('hex') ?? null;
2842
+ const memberId = mesh.ourMemberId;
2843
+ const root = this._identityPublicKey;
2010
2844
  return {
2011
2845
  ...mesh.membership,
2012
- memberId,
2013
- mintedOurRoot: memberId !== null && memberId === this._identityPublicKey
2846
+ memberId: memberId?.toString('hex') ?? null,
2847
+ mintedOurRoot: !!memberId && !!root && b4a.equals(memberId, root)
2014
2848
  };
2015
2849
  }
2016
2850
  _diagnostics() {
2017
- const mesh = this._mesh;
2851
+ const mesh = this._currentMesh();
2018
2852
  const swarm = this._swarm;
2019
2853
  const dht = swarm ? swarm.dht : null;
2020
2854
  const system = this._systemInfo?.snapshot() ?? { cpu: null, gpu: null };
@@ -2068,7 +2902,7 @@ export class Core extends ReadyResource {
2068
2902
  }
2069
2903
  _statusSnapshot() {
2070
2904
  const peerDeviceIds = [...this.peerDeviceIds];
2071
- const mesh = this._mesh;
2905
+ const mesh = this._currentMesh();
2072
2906
  return {
2073
2907
  state: this._statusState(),
2074
2908
  writable: mesh?.writable ?? false,
@@ -2097,8 +2931,9 @@ export class Core extends ReadyResource {
2097
2931
  return 'needs-upgrade';
2098
2932
  if (this._minting)
2099
2933
  return 'joining';
2100
- if (this._mesh)
2101
- return this._mesh.writable ? 'joined' : 'joining';
2934
+ const mesh = this._currentMesh();
2935
+ if (mesh)
2936
+ return mesh.writable ? 'joined' : 'joining';
2102
2937
  return 'idle';
2103
2938
  }
2104
2939
  _notifyStatus() {
@@ -2106,6 +2941,15 @@ export class Core extends ReadyResource {
2106
2941
  listener();
2107
2942
  }
2108
2943
  }
2944
+ function after(ms) {
2945
+ return new Promise((resolve) => unref(setTimeout(resolve, ms)));
2946
+ }
2947
+ function unmarkedNotice(losers) {
2948
+ if (losers.length === 0)
2949
+ return undefined;
2950
+ const ids = losers.map((room) => room.meshKey.toString('hex').slice(0, 8)).join(', ');
2951
+ return `another device of yours opened a shared room at the same moment, so ${ids} is an ordinary room now`;
2952
+ }
2109
2953
  function noop() { }
2110
2954
  async function closeConfigReader(reader) {
2111
2955
  if (!reader)