@qvac/core 0.7.0 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/CHANGELOG.md +67 -1
  2. package/dist-lib/lib/api/chat.d.ts +1 -1
  3. package/dist-lib/lib/api/chat.js +9 -0
  4. package/dist-lib/lib/api/index.d.ts +2 -1
  5. package/dist-lib/lib/api/index.js +2 -1
  6. package/dist-lib/lib/api/network.d.ts +8 -1
  7. package/dist-lib/lib/api/network.js +6 -0
  8. package/dist-lib/lib/assistant/harness.d.ts +2 -0
  9. package/dist-lib/lib/assistant.js +5 -0
  10. package/dist-lib/lib/core/agent.d.ts +2 -0
  11. package/dist-lib/lib/core/agent.js +16 -5
  12. package/dist-lib/lib/core/blind-peers.d.ts +4 -0
  13. package/dist-lib/lib/core/blind-peers.js +1 -0
  14. package/dist-lib/lib/core/chat-copy.d.ts +4 -0
  15. package/dist-lib/lib/core/chat-copy.js +1 -1
  16. package/dist-lib/lib/core/chat-fork.d.ts +21 -0
  17. package/dist-lib/lib/core/chat-fork.js +245 -0
  18. package/dist-lib/lib/core/chat-move.js +1 -4
  19. package/dist-lib/lib/core/chat.d.ts +1 -0
  20. package/dist-lib/lib/core/chat.js +1 -1
  21. package/dist-lib/lib/core/departures.d.ts +5 -1
  22. package/dist-lib/lib/core/departures.js +15 -1
  23. package/dist-lib/lib/core/fs-drives.d.ts +8 -1
  24. package/dist-lib/lib/core/fs-drives.js +47 -15
  25. package/dist-lib/lib/core/harness-loan.d.ts +4 -20
  26. package/dist-lib/lib/core/harness-loan.js +7 -184
  27. package/dist-lib/lib/core/id.d.ts +1 -0
  28. package/dist-lib/lib/core/id.js +3 -0
  29. package/dist-lib/lib/core/mesh.d.ts +3 -2
  30. package/dist-lib/lib/core/mesh.js +11 -1
  31. package/dist-lib/lib/core/migrations/agent-tools-scope.d.ts +2 -0
  32. package/dist-lib/lib/core/migrations/agent-tools-scope.js +24 -0
  33. package/dist-lib/lib/core/migrations/index.js +3 -1
  34. package/dist-lib/lib/core/peer-duplex.d.ts +32 -0
  35. package/dist-lib/lib/core/peer-duplex.js +207 -0
  36. package/dist-lib/lib/core.d.ts +17 -1
  37. package/dist-lib/lib/core.js +183 -10
  38. package/dist-lib/lib/doctor/checks.js +4 -1
  39. package/dist-lib/lib/mesh-scoped-engine.js +3 -0
  40. package/dist-lib/lib/model-download.d.ts +0 -9
  41. package/dist-lib/lib/model-download.js +1 -13
  42. package/dist-lib/schema/engine.js +45 -1
  43. package/dist-lib/schema/field-order.js +1 -1
  44. package/dist-lib/schema/qvac.js +10 -1
  45. package/dist-lib/spec/bind.js +14 -0
  46. package/dist-lib/spec/calls.js +3 -0
  47. package/dist-lib/spec/capabilities.d.ts +6 -0
  48. package/dist-lib/spec/hrpc/hrpc.json +39 -0
  49. package/dist-lib/spec/hrpc/index.js +41 -4
  50. package/dist-lib/spec/hrpc/messages.js +1420 -1289
  51. package/dist-lib/spec/hyperschema/index.js +1420 -1289
  52. package/dist-lib/spec/hyperschema/schema.json +102 -0
  53. package/dist-lib/spec/hyperschema/types.d.ts +29 -0
  54. package/dist-lib/spec/mesh/hyperdb/messages.js +295 -248
  55. package/dist-lib/spec/mesh/hyperdispatch/messages.js +45 -5
  56. package/dist-lib/spec/mesh/hyperschema/index.js +45 -5
  57. package/dist-lib/spec/mesh/hyperschema/schema.json +31 -1
  58. package/dist-lib/spec/mesh/hyperschema/types.d.ts +9 -1
  59. package/package.json +9 -6
  60. package/tui/app.mjs +121 -67
  61. package/tui/lib/fork-command.mjs +43 -0
@@ -0,0 +1,207 @@
1
+ import b4a from 'b4a';
2
+ import { raw, uint } from 'compact-encoding';
3
+ import { Duplex } from 'streamx';
4
+ import { createProtocolPairing } from './protocol-pairing.js';
5
+ function noop() { }
6
+ const bytes = raw;
7
+ const frame = {
8
+ preencode(state, f) {
9
+ uint.preencode(state, f.id);
10
+ uint.preencode(state, f.mine ? 1 : 0);
11
+ bytes.preencode(state, f.data);
12
+ },
13
+ encode(state, f) {
14
+ uint.encode(state, f.id);
15
+ uint.encode(state, f.mine ? 1 : 0);
16
+ bytes.encode(state, f.data);
17
+ },
18
+ decode(state) {
19
+ const id = uint.decode(state);
20
+ const mine = uint.decode(state) === 1;
21
+ return { id, mine, data: bytes.decode(state) };
22
+ }
23
+ };
24
+ const ends = {
25
+ preencode(state, e) {
26
+ uint.preencode(state, e.id);
27
+ uint.preencode(state, e.mine ? 1 : 0);
28
+ },
29
+ encode(state, e) {
30
+ uint.encode(state, e.id);
31
+ uint.encode(state, e.mine ? 1 : 0);
32
+ },
33
+ decode(state) {
34
+ return { id: uint.decode(state), mine: uint.decode(state) === 1 };
35
+ }
36
+ };
37
+ export class PeerDuplexChannel {
38
+ _opts;
39
+ _peers;
40
+ _nextId;
41
+ _pairing;
42
+ constructor(opts) {
43
+ this._opts = opts;
44
+ this._peers = new Map();
45
+ this._nextId = 1;
46
+ this._pairing = createProtocolPairing(opts.protocol);
47
+ }
48
+ addStream(stream) {
49
+ const deviceId = b4a.toHex(stream.remotePublicKey);
50
+ if (this._peers.has(deviceId)) {
51
+ return;
52
+ }
53
+ const channel = this._pairing
54
+ .mux(stream, () => this.addStream(stream))
55
+ .createChannel({
56
+ protocol: this._opts.protocol,
57
+ onopen: () => this._opened(deviceId),
58
+ onclose: () => this._drop(deviceId)
59
+ });
60
+ if (!channel) {
61
+ return;
62
+ }
63
+ const open = channel.addMessage({
64
+ encoding: uint,
65
+ onmessage: (id) => void this._serve(deviceId, id)
66
+ });
67
+ const data = channel.addMessage({
68
+ encoding: frame,
69
+ onmessage: (f) => this._deliver(deviceId, f)
70
+ });
71
+ const close = channel.addMessage({
72
+ encoding: ends,
73
+ onmessage: (e) => this._end(deviceId, e)
74
+ });
75
+ this._peers.set(deviceId, {
76
+ channel,
77
+ remoteKey: stream.remotePublicKey,
78
+ open,
79
+ data,
80
+ close,
81
+ dialled: new Map(),
82
+ served: new Map(),
83
+ opened: false
84
+ });
85
+ channel.open();
86
+ }
87
+ isOpen(deviceId) {
88
+ return this._peers.get(deviceId)?.opened === true;
89
+ }
90
+ dial(deviceId) {
91
+ const peer = this._peers.get(deviceId);
92
+ if (!peer) {
93
+ throw new Error(this._opts.unreachable);
94
+ }
95
+ const id = this._nextId;
96
+ this._nextId += 1;
97
+ const stream = this._attach(peer, id, { mine: true });
98
+ peer.open.send(id);
99
+ return stream;
100
+ }
101
+ async revalidate(allowed) {
102
+ for (const peer of [...this._peers.values()]) {
103
+ if (peer.served.size === 0)
104
+ continue;
105
+ if (await allowed(peer.remoteKey).catch(() => false))
106
+ continue;
107
+ for (const [id, stream] of [...peer.served]) {
108
+ peer.served.delete(id);
109
+ peer.close.send({ id, mine: false });
110
+ stream.destroy(new Error(this._opts.revoked));
111
+ }
112
+ }
113
+ }
114
+ destroy() {
115
+ this._pairing.destroy();
116
+ for (const [deviceId, peer] of [...this._peers]) {
117
+ try {
118
+ peer.channel.close();
119
+ }
120
+ catch { }
121
+ this._drop(deviceId);
122
+ }
123
+ }
124
+ _opened(deviceId) {
125
+ const peer = this._peers.get(deviceId);
126
+ if (!peer) {
127
+ return;
128
+ }
129
+ peer.opened = true;
130
+ this._opts.onchange?.(deviceId);
131
+ }
132
+ _attach(peer, id, opts) {
133
+ const { mine } = opts;
134
+ const held = mine ? peer.dialled : peer.served;
135
+ const stream = new Duplex({
136
+ write: (chunk, cb) => {
137
+ peer.data.send({ id, mine, data: chunk });
138
+ cb(null);
139
+ },
140
+ final: (cb) => {
141
+ peer.close.send({ id, mine });
142
+ cb(null);
143
+ },
144
+ destroy: (cb) => {
145
+ if (held.delete(id)) {
146
+ peer.close.send({ id, mine });
147
+ }
148
+ cb(null);
149
+ }
150
+ });
151
+ held.set(id, stream);
152
+ return stream;
153
+ }
154
+ async _serve(deviceId, id) {
155
+ const peer = this._peers.get(deviceId);
156
+ if (!peer || peer.served.has(id)) {
157
+ return;
158
+ }
159
+ const stream = this._attach(peer, id, { mine: false });
160
+ const served = await this._opts.onaccept(peer.remoteKey).catch(noop);
161
+ if (!served) {
162
+ stream.destroy();
163
+ return;
164
+ }
165
+ stream.on('error', noop);
166
+ served.on('error', noop);
167
+ stream.pipe(served).pipe(stream);
168
+ }
169
+ _deliver(deviceId, f) {
170
+ const peer = this._peers.get(deviceId);
171
+ if (!peer) {
172
+ return;
173
+ }
174
+ const held = f.mine ? peer.served : peer.dialled;
175
+ held.get(f.id)?.push(f.data);
176
+ }
177
+ _end(deviceId, e) {
178
+ const peer = this._peers.get(deviceId);
179
+ if (!peer) {
180
+ return;
181
+ }
182
+ const held = e.mine ? peer.served : peer.dialled;
183
+ const stream = held.get(e.id);
184
+ if (!stream) {
185
+ return;
186
+ }
187
+ held.delete(e.id);
188
+ stream.push(null);
189
+ stream.destroy();
190
+ }
191
+ _drop(deviceId) {
192
+ const peer = this._peers.get(deviceId);
193
+ if (!peer) {
194
+ return;
195
+ }
196
+ this._peers.delete(deviceId);
197
+ for (const held of [peer.dialled, peer.served]) {
198
+ for (const stream of held.values()) {
199
+ stream.destroy(new Error(this._opts.unreachable));
200
+ }
201
+ held.clear();
202
+ }
203
+ if (peer.opened) {
204
+ this._opts.onchange?.(deviceId);
205
+ }
206
+ }
207
+ }
@@ -6,7 +6,7 @@ import Wakeup from 'protomux-wakeup';
6
6
  import ReadyGuard from 'ready-guard';
7
7
  import ReadyResource from 'ready-resource';
8
8
  import type { Logger } from '../index.ts';
9
- import type { AssistantArchivedMeshList, AssistantCompactChatResponse, AssistantCopySkillResponse, AssistantDeleteModelRequest, AssistantDeleteModelResponse, AssistantDeleteSkillResponse, AssistantKvCacheDeletionResponse, AssistantListModelsRequest, AssistantLoadModelResponse, AssistantManagedSkillsResponse, AssistantMeshList, AssistantAccount, AssistantModelCatalogRevision, AssistantModelInfoList, AssistantOkResponse, AssistantSkillCatalogResponse, AssistantSkillFileResponse, AssistantSpeakRequest, AssistantSpeakResponse, AssistantToolCatalogResponse, AssistantDetectLanguageRequest, AssistantDetectLanguageResponse, AssistantTranscribeRequest, AssistantTranscribeResponse, AssistantUnloadModelResponse, AssistantModelUsers, AssistantCustomModelPreview, AssistantPreviewCustomModelRequest } from '../spec/index.d.ts';
9
+ import type { AssistantArchivedMeshList, AssistantCompactChatResponse, AssistantCopySkillResponse, AssistantDeleteModelRequest, AssistantDeleteModelResponse, AssistantDeleteSkillResponse, AssistantKvCacheDeletionResponse, AssistantListModelsRequest, AssistantLoadModelResponse, AssistantManagedSkillsResponse, AssistantMeshList, AssistantAccount, AssistantModelCatalogRevision, AssistantModelInfoList, AssistantBlindPeering, AssistantOkResponse, AssistantSkillCatalogResponse, AssistantSkillFileResponse, AssistantSpeakRequest, AssistantSpeakResponse, AssistantToolCatalogResponse, AssistantDetectLanguageRequest, AssistantDetectLanguageResponse, AssistantTranscribeRequest, AssistantTranscribeResponse, AssistantUnloadModelResponse, AssistantModelUsers, AssistantCustomModelPreview, AssistantPreviewCustomModelRequest } from '../spec/index.d.ts';
10
10
  import type { InferenceCallSource } from './api/inference-calls.ts';
11
11
  import { type SkillOptions } from './api/index.ts';
12
12
  import type { ModelMemoryUse } from './model-memory-cap.ts';
@@ -144,6 +144,8 @@ export declare class Core extends ReadyResource {
144
144
  private readonly _otaConfigCore;
145
145
  private _otaConfig;
146
146
  private _configPeering;
147
+ private _blindPeeringOn;
148
+ private readonly _blindPeeringListeners;
147
149
  private _recoveryCore;
148
150
  private _recoverySigner;
149
151
  private _recoveryPeering;
@@ -207,6 +209,9 @@ export declare class Core extends ReadyResource {
207
209
  private _identityPhrase;
208
210
  private _identityPublicKey;
209
211
  private _swarm;
212
+ private _siblings;
213
+ private readonly _seenPeers;
214
+ private readonly _siblingLock;
210
215
  private readonly _onSwarmConnection;
211
216
  private _departures;
212
217
  private _fsDrives;
@@ -299,7 +304,15 @@ export declare class Core extends ReadyResource {
299
304
  private _holdIdentityPhrase;
300
305
  private _pushIdentityPhrase;
301
306
  private _offerIdentityPhrase;
307
+ private _startBlindPeering;
308
+ setBlindPeering(enabled: boolean): Promise<AssistantBlindPeering>;
309
+ private _peeredMeshes;
310
+ private _stopBlindPeering;
311
+ private _resumeBlindPeering;
302
312
  private _bringMeshOnline;
313
+ private _dialSiblings;
314
+ private _redialSiblings;
315
+ private _siblingsToDial;
303
316
  private _advertiseDeviceProfile;
304
317
  private _advertiseAppVersion;
305
318
  private _retractInventory;
@@ -415,6 +428,7 @@ export declare class Core extends ReadyResource {
415
428
  private _revokeInMeshes;
416
429
  private _sweepEveryOwnMesh;
417
430
  private _revokeStaleRows;
431
+ private _rehomeRemoved;
418
432
  private _revokeInMeshesOnce;
419
433
  renameLocalDevice(name: string): Promise<void>;
420
434
  account(): Promise<AssistantAccount>;
@@ -426,6 +440,7 @@ export declare class Core extends ReadyResource {
426
440
  getMeshInvite(opts?: MeshInviteOptions): Promise<MeshInvite | null>;
427
441
  private _delegatePairing;
428
442
  previewMeshInvite(invite: string): MeshInvitePreview;
443
+ private _leaveSiblings;
429
444
  private _closeSwarm;
430
445
  pushSkillSecret(request: SecretRequest): Promise<void>;
431
446
  private _storeHandedOffSecret;
@@ -458,6 +473,7 @@ export declare class Core extends ReadyResource {
458
473
  private _onPeerChange;
459
474
  private _meshPool;
460
475
  private _meshStatusSource;
476
+ private _blindPeeringSource;
461
477
  private _networkStatusSource;
462
478
  private _networkSnapshot;
463
479
  private _networkState;
@@ -17,7 +17,7 @@ import { writeAutoDownloadModels } from './api/app-config.js';
17
17
  import { NO_SUCH_MESH, requireMesh } from './api/current-mesh.js';
18
18
  import { ChatMoves } from './core/chat-move.js';
19
19
  import { asEncoded, watchModels } from './models-watch.js';
20
- import { BLIND_PEER_KEYS, BLIND_RELAY_KEYS } from './core/blind-peers.js';
20
+ import { BLIND_PEER_KEYS, BLIND_PEERING_OFF_KEY, BLIND_RELAY_KEYS } from './core/blind-peers.js';
21
21
  import { CoreBlobServer, MemoryVectorIndex } from './core/files/index.js';
22
22
  import { getChat, removeChatAttachments, removeChatKnowledge } from './core/chat.js';
23
23
  import { rebuildChatOrder, refreshChatOrder } from './core/chat-order.js';
@@ -27,7 +27,7 @@ import { decodeMeshInvite, encodeMeshInvite } from './core/invite.js';
27
27
  import { fileKeyOf, knownFile, referencedFile } from './core/files/local-index.js';
28
28
  import { openLocalStore } from './core/local/index.js';
29
29
  import { MAX_MESH_NAME_CHARS, meshNameWithinBounds } from '../schema/qvac.js';
30
- import { getAgent } from './core/agent.js';
30
+ import { getAgent, rehomeAgents } from './core/agent.js';
31
31
  import { countContents, migrateContents, withArchivedMesh } from './core/archived-mesh.js';
32
32
  import { HostedAgents } from './core/hosted-agents.js';
33
33
  import { HarnessLoan } from './core/harness-loan.js';
@@ -109,6 +109,8 @@ export class Core extends ReadyResource {
109
109
  _otaConfigCore;
110
110
  _otaConfig = null;
111
111
  _configPeering = null;
112
+ _blindPeeringOn = true;
113
+ _blindPeeringListeners = new Set();
112
114
  _recoveryCore = null;
113
115
  _recoverySigner = null;
114
116
  _recoveryPeering = null;
@@ -172,6 +174,9 @@ export class Core extends ReadyResource {
172
174
  _identityPhrase = null;
173
175
  _identityPublicKey = null;
174
176
  _swarm;
177
+ _siblings = new Map();
178
+ _seenPeers = new Set();
179
+ _siblingLock = new ScopeLock({ debounce: true });
175
180
  _onSwarmConnection;
176
181
  _departures = null;
177
182
  _fsDrives = null;
@@ -360,6 +365,7 @@ export class Core extends ReadyResource {
360
365
  this._deviceKeyPair =
361
366
  this._swarm?.keyPair ?? (await this.store.createKeyPair(SWARM_KEYPAIR_NAME));
362
367
  this._identityPublicKey = (await this.local.readIdentity())?.publicKey ?? null;
368
+ this._blindPeeringOn = (await this.local.get(BLIND_PEERING_OFF_KEY)) !== true;
363
369
  this._localDevice = await this.local.ensureDevice(this._deviceKeyPair.publicKey, this._device);
364
370
  if (this._autoDownloadModels !== undefined) {
365
371
  await writeAutoDownloadModels(this.local, this._autoDownloadModels);
@@ -372,12 +378,14 @@ export class Core extends ReadyResource {
372
378
  logger: this._log,
373
379
  blindPeerKeys: this._resolveBlindPeerKeys(),
374
380
  blindPeeringOpts: this._blindPeeringOpts,
381
+ blindPeering: this._blindPeeringOn,
375
382
  wakeup: this._wakeup
376
383
  });
377
384
  this._fsDrives = createFsDriveHost(this.store, {
378
385
  swarm,
379
386
  blindPeerKeys: this._resolveBlindPeerKeys(),
380
- blindPeeringOpts: this._blindPeeringOpts
387
+ blindPeeringOpts: this._blindPeeringOpts,
388
+ blindPeering: this._blindPeeringOn
381
389
  });
382
390
  void sweepDriveStaging();
383
391
  this._blobServer = new CoreBlobServer(this.store, (coreKey, blockOffset, meshKey) => this._meshForBlobCore(coreKey, blockOffset, meshKey), { onRead: (mesh, coreKey, blockOffset) => this._servedFile(mesh, coreKey, blockOffset) });
@@ -400,6 +408,7 @@ export class Core extends ReadyResource {
400
408
  mesh: this._meshPool(),
401
409
  meshStatus,
402
410
  network: this._networkStatusSource(),
411
+ blindPeering: this._blindPeeringSource(),
403
412
  diagnostics: this._diagnosticsSource(),
404
413
  inferenceCalls: {
405
414
  list: (meshId, chatId) => this._listInferenceCalls(meshId, chatId),
@@ -695,7 +704,11 @@ export class Core extends ReadyResource {
695
704
  }
696
705
  _peerConfigCore() {
697
706
  const config = this._otaConfig;
698
- if (!config || this._configPeering || this._accountMesh() || !this._swarm) {
707
+ if (!config ||
708
+ !this._blindPeeringOn ||
709
+ this._configPeering ||
710
+ this._accountMesh() ||
711
+ !this._swarm) {
699
712
  return;
700
713
  }
701
714
  const keys = this._resolveBlindPeerKeys();
@@ -704,6 +717,7 @@ export class Core extends ReadyResource {
704
717
  }
705
718
  this._configPeering = new BlindPeering(this._swarm.dht, this.store, {
706
719
  pick: keys.length,
720
+ suspended: this.suspended,
707
721
  ...this._blindPeeringOpts
708
722
  });
709
723
  this._configPeering.addCoreBackground(config.core, { keys });
@@ -832,6 +846,7 @@ export class Core extends ReadyResource {
832
846
  if (this._recoveryPeering) {
833
847
  await this._recoveryPeering.suspend();
834
848
  }
849
+ await this._fsDrives?.suspend();
835
850
  if (this._swarm && this._ownsSwarm) {
836
851
  await this._swarm.suspend();
837
852
  }
@@ -873,6 +888,7 @@ export class Core extends ReadyResource {
873
888
  if (this._recoveryPeering) {
874
889
  await this._recoveryPeering.resume();
875
890
  }
891
+ await this._fsDrives?.resume();
876
892
  for (const { mesh } of this._meshes.values()) {
877
893
  await mesh.resume();
878
894
  }
@@ -886,16 +902,28 @@ export class Core extends ReadyResource {
886
902
  }
887
903
  this._startFileGc();
888
904
  if (this._swarm) {
905
+ for (const [id, key] of this._siblings) {
906
+ if (this._seenPeers.has(id)) {
907
+ this._swarm.joinPeer(key);
908
+ }
909
+ }
889
910
  void this._refreshTopics(this._swarm).catch(noop);
890
911
  }
891
912
  void this._pushIdentityPhrase();
892
913
  this._log?.info('lifecycle', 'core resume');
893
914
  }
894
915
  async _refreshTopics(swarm) {
916
+ let topics = 0;
895
917
  for (const discovery of this._joinedTopics(swarm)) {
896
918
  await discovery.flushed();
897
919
  await discovery.refresh();
920
+ topics++;
898
921
  }
922
+ this._log?.info(['p2p', 'swarm'], 'topics refreshed after resume', {
923
+ topics,
924
+ peers: this.peerDeviceIds.length,
925
+ siblings: this._siblings.size
926
+ });
899
927
  }
900
928
  _joinedTopics(swarm) {
901
929
  if (this._ownsSwarm) {
@@ -1182,7 +1210,7 @@ export class Core extends ReadyResource {
1182
1210
  const core = await this._openOwnRecoveryCore(recoveryKeys(identity));
1183
1211
  const appended = await publishRecovery(core, record);
1184
1212
  const held = await readRecovery(core);
1185
- if (held?.meshKey && b4a.equals(held.meshKey, record.meshKey)) {
1213
+ if (this._blindPeeringOn && held?.meshKey && b4a.equals(held.meshKey, record.meshKey)) {
1186
1214
  await this._markRecoverable(held, core);
1187
1215
  }
1188
1216
  this._peerRecoveryCore(core);
@@ -1313,7 +1341,7 @@ export class Core extends ReadyResource {
1313
1341
  throw new Error(NO_RECOVERY_RECORD);
1314
1342
  }
1315
1343
  _peerRecoveryCore(core) {
1316
- if (!this._swarm || this.closing)
1344
+ if (!this._swarm || this.closing || !this._blindPeeringOn)
1317
1345
  return;
1318
1346
  const keys = this._resolveBlindPeerKeys();
1319
1347
  if (keys.length === 0) {
@@ -1322,6 +1350,7 @@ export class Core extends ReadyResource {
1322
1350
  if (!this._recoveryPeering) {
1323
1351
  this._recoveryPeering = new BlindPeering(this._swarm.dht, this.store, {
1324
1352
  pick: keys.length,
1353
+ suspended: this.suspended,
1325
1354
  ...this._blindPeeringOpts
1326
1355
  });
1327
1356
  }
@@ -1429,16 +1458,63 @@ export class Core extends ReadyResource {
1429
1458
  return false;
1430
1459
  }
1431
1460
  }
1461
+ _startBlindPeering(mesh) {
1462
+ if (this._swarm && this._blindPeeringOn) {
1463
+ mesh.startBlindPeering(this._swarm.dht);
1464
+ }
1465
+ }
1466
+ async setBlindPeering(enabled) {
1467
+ if (enabled !== this._blindPeeringOn) {
1468
+ this._blindPeeringOn = enabled;
1469
+ await (enabled ? this._resumeBlindPeering() : this._stopBlindPeering());
1470
+ this._log?.info(['p2p', 'mesh'], enabled ? 'blind peering on' : 'blind peering off');
1471
+ for (const listener of this._blindPeeringListeners) {
1472
+ listener();
1473
+ }
1474
+ }
1475
+ await this.local.set(BLIND_PEERING_OFF_KEY, !this._blindPeeringOn);
1476
+ return { enabled: this._blindPeeringOn };
1477
+ }
1478
+ _peeredMeshes() {
1479
+ const meshes = [...this._meshes.values()].map((entry) => entry.mesh);
1480
+ return this._candidateMesh ? [...meshes, this._candidateMesh] : meshes;
1481
+ }
1482
+ async _stopBlindPeering() {
1483
+ const recovery = this._recoveryPeering;
1484
+ this._recoveryPeering = null;
1485
+ await Promise.all([
1486
+ ...this._peeredMeshes().map((mesh) => mesh.stopBlindPeering()),
1487
+ this._departures?.stopBlindPeering(),
1488
+ this._fsDrives?.stopBlindPeering(),
1489
+ this._closeConfigPeering(),
1490
+ recovery?.close()
1491
+ ]);
1492
+ }
1493
+ async _resumeBlindPeering() {
1494
+ const suspended = this.suspended;
1495
+ if (this._swarm) {
1496
+ for (const mesh of this._peeredMeshes()) {
1497
+ mesh.startBlindPeering(this._swarm.dht, { suspended });
1498
+ }
1499
+ }
1500
+ this._peerConfigCore();
1501
+ if (this._recoveryCore) {
1502
+ this._peerRecoveryCore(this._recoveryCore);
1503
+ }
1504
+ this._departures?.startBlindPeering({ suspended });
1505
+ await this._fsDrives?.startBlindPeering();
1506
+ }
1432
1507
  _bringMeshOnline(mesh, opts = {}) {
1433
1508
  if (this.closing || !this._isPooled(mesh))
1434
1509
  return;
1435
1510
  if (this._swarm) {
1436
1511
  this._swarm.join(mesh.discoveryKey, { server: true, client: true });
1437
- mesh.startBlindPeering(this._swarm.dht);
1512
+ this._startBlindPeering(mesh);
1438
1513
  if (this.suspended) {
1439
1514
  void mesh.suspend().catch(noop);
1440
1515
  }
1441
1516
  this._log?.info(['p2p', 'swarm'], 'announced and peering the mesh');
1517
+ void this._dialSiblings(mesh).catch(noop);
1442
1518
  }
1443
1519
  if (!opts.new) {
1444
1520
  void this._ensureNotRevoked(mesh);
@@ -1449,6 +1525,57 @@ export class Core extends ReadyResource {
1449
1525
  void this._retractInventory(mesh, this._requireDeviceKeyPair().publicKey);
1450
1526
  void this._settleMembership(mesh, { migrate: true });
1451
1527
  }
1528
+ async _dialSiblings(mesh) {
1529
+ if (!this._isAccount(mesh) || !(await this._siblingLock.lock())) {
1530
+ return;
1531
+ }
1532
+ try {
1533
+ await this._redialSiblings(mesh);
1534
+ }
1535
+ finally {
1536
+ this._siblingLock.unlock();
1537
+ }
1538
+ }
1539
+ async _redialSiblings(mesh) {
1540
+ const swarm = this._swarm;
1541
+ if (!swarm || this.closing) {
1542
+ return;
1543
+ }
1544
+ const live = await this._siblingsToDial(mesh, swarm);
1545
+ if (!this._isAccount(mesh) || this.closing) {
1546
+ return;
1547
+ }
1548
+ let changed = false;
1549
+ for (const [id, key] of this._siblings) {
1550
+ if (live.has(id))
1551
+ continue;
1552
+ swarm.leavePeer(key);
1553
+ changed = true;
1554
+ }
1555
+ for (const [id, key] of live) {
1556
+ if (this._siblings.has(id))
1557
+ continue;
1558
+ swarm.joinPeer(key);
1559
+ changed = true;
1560
+ }
1561
+ this._siblings = live;
1562
+ if (changed) {
1563
+ this._log?.info(['p2p', 'swarm'], 'dialling sibling devices', { devices: live.size });
1564
+ }
1565
+ }
1566
+ async _siblingsToDial(mesh, swarm) {
1567
+ const self = this._requireDeviceKeyPair().publicKey;
1568
+ const hosts = new Set([...swarm.explicitPeers].map((peer) => b4a.toHex(peer.publicKey)));
1569
+ const live = new Map();
1570
+ for await (const device of mesh.view.find(DEVICES)) {
1571
+ const id = b4a.toHex(device.id);
1572
+ const theirs = hosts.has(id) && !this._siblings.has(id);
1573
+ if (!device.revoked && !theirs && !b4a.equals(device.id, self)) {
1574
+ live.set(id, device.id);
1575
+ }
1576
+ }
1577
+ return live;
1578
+ }
1452
1579
  async _advertiseDeviceProfile(mesh, deviceId) {
1453
1580
  const local = this._localDevice;
1454
1581
  if (!local?.platform) {
@@ -2267,9 +2394,7 @@ export class Core extends ReadyResource {
2267
2394
  await candidate.close().catch(noop);
2268
2395
  throw this._joinCancellation?.canceled ? new Error(MESH_JOIN_CANCELED) : error;
2269
2396
  }
2270
- if (this._swarm) {
2271
- candidate.startBlindPeering(this._swarm.dht);
2272
- }
2397
+ this._startBlindPeering(candidate);
2273
2398
  try {
2274
2399
  this._throwIfJoinCanceled();
2275
2400
  if (!b4a.equals(candidate.key, invite.baseKey)) {
@@ -2367,6 +2492,7 @@ export class Core extends ReadyResource {
2367
2492
  mesh.on('members-dirty', () => {
2368
2493
  if (!this._isAccount(mesh))
2369
2494
  return;
2495
+ void this._dialSiblings(mesh).catch(noop);
2370
2496
  void this._followAccountName(mesh);
2371
2497
  void this._revokeInMeshes();
2372
2498
  });
@@ -2395,6 +2521,7 @@ export class Core extends ReadyResource {
2395
2521
  await this.local.setAccountMesh(this._requireDeviceKeyPair().publicKey, mesh.key);
2396
2522
  this._wireMesh(this._poolEntry(mesh));
2397
2523
  this._accountId = this._meshIdOf(mesh);
2524
+ void this._dialSiblings(mesh).catch(noop);
2398
2525
  this._statusError = null;
2399
2526
  this._upgradeRequired = null;
2400
2527
  void this._closeConfigPeering().catch(noop);
@@ -3192,9 +3319,15 @@ export class Core extends ReadyResource {
3192
3319
  throw new Error('this device holds no account mesh');
3193
3320
  }
3194
3321
  const paired = await this._accountMesh()?.view.get(DEVICES, { id: deviceId });
3322
+ const row = await target.view.get(DEVICES, { id: deviceId });
3195
3323
  const revokedAt = Math.max(Date.now(), (paired?.joinedAt ?? 0) + 1);
3196
3324
  await target.dispatch('remove-writer', { writerKey, deviceId, revokedAt });
3197
3325
  await target.autobee.update();
3326
+ const self = this._requireDeviceKeyPair().publicKey;
3327
+ const ours = target === this._accountMesh() || ownedBy(row, target.ourMemberId);
3328
+ if (row && ours && !b4a.equals(deviceId, self)) {
3329
+ await rehomeAgents(target, deviceId, self).catch((err) => this._log?.warn(['p2p', 'mesh'], "handing over a removed device's agents failed", err));
3330
+ }
3198
3331
  if (target === this._accountMesh()) {
3199
3332
  this._sweepUnknownEverywhere = true;
3200
3333
  }
@@ -3255,6 +3388,23 @@ export class Core extends ReadyResource {
3255
3388
  });
3256
3389
  }
3257
3390
  }
3391
+ async _rehomeRemoved(account, mesh, self) {
3392
+ if (!mesh.writable)
3393
+ return;
3394
+ const removed = [];
3395
+ for await (const row of mesh.view.find(DEVICES)) {
3396
+ const ours = mesh === account || ownedBy(row, mesh.ourMemberId);
3397
+ if (row.revoked && ours && !b4a.equals(row.id, self)) {
3398
+ removed.push(row.id);
3399
+ }
3400
+ }
3401
+ for (const id of removed) {
3402
+ const device = await account.view.get(DEVICES, { id });
3403
+ if (device?.revokedBy && b4a.equals(device.revokedBy, account.localWriterKey)) {
3404
+ await rehomeAgents(mesh, id, self).catch((err) => this._log?.warn(['p2p', 'mesh'], "handing over a removed device's agents failed", err));
3405
+ }
3406
+ }
3407
+ }
3258
3408
  async _revokeInMeshesOnce() {
3259
3409
  try {
3260
3410
  const account = this._accountMesh();
@@ -3266,6 +3416,7 @@ export class Core extends ReadyResource {
3266
3416
  await this._sweepEveryOwnMesh(account, account.ourMemberId);
3267
3417
  }
3268
3418
  const self = this._requireDeviceKeyPair().publicKey;
3419
+ await this._rehomeRemoved(account, account, self);
3269
3420
  for (const { mesh } of [...this._meshes.values()]) {
3270
3421
  if (mesh === account || !mesh.writable)
3271
3422
  continue;
@@ -3280,6 +3431,7 @@ export class Core extends ReadyResource {
3280
3431
  const meshId = b4a.toHex(mesh.key);
3281
3432
  const unknownToo = this._sweepUnknownIn.has(meshId);
3282
3433
  await this._revokeStaleRows(account, mesh, { self, ours: mesh.ourMemberId, unknownToo });
3434
+ await this._rehomeRemoved(account, mesh, self);
3283
3435
  this._sweepUnknownIn.delete(meshId);
3284
3436
  }
3285
3437
  }
@@ -3461,6 +3613,12 @@ export class Core extends ReadyResource {
3461
3613
  }
3462
3614
  return { memberInvite: decoded.memberInvite ?? false, inviterName: decoded.inviterName };
3463
3615
  }
3616
+ _leaveSiblings(swarm) {
3617
+ for (const key of this._siblings.values()) {
3618
+ swarm.leavePeer(key);
3619
+ }
3620
+ this._siblings.clear();
3621
+ }
3464
3622
  async _closeSwarm() {
3465
3623
  if (this._ownsWakeup) {
3466
3624
  this._wakeup.destroy();
@@ -3480,6 +3638,7 @@ export class Core extends ReadyResource {
3480
3638
  detach();
3481
3639
  }
3482
3640
  this._peers.clear();
3641
+ this._leaveSiblings(swarm);
3483
3642
  await Promise.all([...this._meshes.values()].map(({ mesh }) => swarm.leave(mesh.discoveryKey)));
3484
3643
  return;
3485
3644
  }
@@ -3928,6 +4087,7 @@ export class Core extends ReadyResource {
3928
4087
  }
3929
4088
  }
3930
4089
  _trackPeerConnection(conn, detach) {
4090
+ this._seenPeers.add(b4a.toHex(conn.remotePublicKey));
3931
4091
  const remove = () => {
3932
4092
  if (this._peers.delete(conn)) {
3933
4093
  this._onPeerChange();
@@ -3992,6 +4152,14 @@ export class Core extends ReadyResource {
3992
4152
  displayName: () => this.displayName()
3993
4153
  };
3994
4154
  }
4155
+ _blindPeeringSource() {
4156
+ return {
4157
+ watch: (listener) => void this._blindPeeringListeners.add(listener),
4158
+ unwatch: (listener) => void this._blindPeeringListeners.delete(listener),
4159
+ snapshot: () => ({ enabled: this._blindPeeringOn }),
4160
+ set: (enabled) => this.setBlindPeering(enabled)
4161
+ };
4162
+ }
3995
4163
  _networkStatusSource() {
3996
4164
  const listeners = new Set();
3997
4165
  let timer = null;
@@ -4415,6 +4583,7 @@ export class Core extends ReadyResource {
4415
4583
  fastForwards: mesh.autobee.stats.fastForwards,
4416
4584
  member: this._memberDiagnostics(mesh),
4417
4585
  orphanedBlobSweep: mesh.orphanedBlobSweepStatus,
4586
+ blindPeering: this._blindPeeringOn,
4418
4587
  blindPeerCount: mesh.blindPeerKeys.length,
4419
4588
  blindPeers: mesh.blindPeerStats()
4420
4589
  }
@@ -4488,6 +4657,10 @@ function unmarkedNotice(losers) {
4488
4657
  return `another device of yours opened a shared mesh at the same moment, so ${ids} is an ordinary mesh now`;
4489
4658
  }
4490
4659
  function noop() { }
4660
+ function ownedBy(device, member) {
4661
+ const owner = device?.memberId ?? device?.claimedMemberId;
4662
+ return !!owner && !!member && b4a.equals(owner, member);
4663
+ }
4491
4664
  function isPeerConnection(conn) {
4492
4665
  return 'remotePublicKey' in conn;
4493
4666
  }