@qvac/core 0.6.6 → 0.7.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 (123) hide show
  1. package/CHANGELOG.md +185 -1
  2. package/README.md +2 -0
  3. package/dist-lib/lib/api/app-config.d.ts +3 -1
  4. package/dist-lib/lib/api/app-config.js +15 -4
  5. package/dist-lib/lib/assistant/fake-models.d.ts +22 -0
  6. package/dist-lib/lib/assistant/fake-models.js +112 -0
  7. package/dist-lib/lib/assistant/harness.d.ts +10 -0
  8. package/dist-lib/lib/assistant/harness.js +26 -0
  9. package/dist-lib/lib/assistant/history.d.ts +5 -1
  10. package/dist-lib/lib/assistant/history.js +14 -2
  11. package/dist-lib/lib/assistant-tree.d.ts +4 -0
  12. package/dist-lib/lib/assistant-tree.js +45 -7
  13. package/dist-lib/lib/assistant.d.ts +16 -2
  14. package/dist-lib/lib/assistant.js +184 -44
  15. package/dist-lib/lib/constants.d.ts +6 -0
  16. package/dist-lib/lib/constants.js +6 -0
  17. package/dist-lib/lib/core/chat-activity.d.ts +1 -1
  18. package/dist-lib/lib/core/chat-activity.js +3 -0
  19. package/dist-lib/lib/core/collections.d.ts +1 -0
  20. package/dist-lib/lib/core/collections.js +1 -0
  21. package/dist-lib/lib/core/contract/compute.d.ts +1 -1
  22. package/dist-lib/lib/core/contract/compute.js +2 -1
  23. package/dist-lib/lib/core/contract/index.js +6 -1
  24. package/dist-lib/lib/core/contract/invites.d.ts +1 -11
  25. package/dist-lib/lib/core/contract/invites.js +3 -3
  26. package/dist-lib/lib/core/contract/mesh-seeds.d.ts +2 -0
  27. package/dist-lib/lib/core/contract/mesh-seeds.js +26 -0
  28. package/dist-lib/lib/core/contract/open-join.d.ts +2 -0
  29. package/dist-lib/lib/core/contract/open-join.js +57 -0
  30. package/dist-lib/lib/core/departures.d.ts +3 -0
  31. package/dist-lib/lib/core/departures.js +44 -1
  32. package/dist-lib/lib/core/fs-drives.js +9 -6
  33. package/dist-lib/lib/core/handoff-channel.d.ts +1 -0
  34. package/dist-lib/lib/core/handoff-channel.js +7 -2
  35. package/dist-lib/lib/core/harness-loan.d.ts +1 -0
  36. package/dist-lib/lib/core/harness-loan.js +7 -2
  37. package/dist-lib/lib/core/identity.d.ts +1 -1
  38. package/dist-lib/lib/core/identity.js +6 -4
  39. package/dist-lib/lib/core/invite.d.ts +2 -0
  40. package/dist-lib/lib/core/invite.js +6 -2
  41. package/dist-lib/lib/core/membership.d.ts +1 -12
  42. package/dist-lib/lib/core/membership.js +1 -94
  43. package/dist-lib/lib/core/mesh.d.ts +8 -13
  44. package/dist-lib/lib/core/mesh.js +41 -87
  45. package/dist-lib/lib/core/protocol-pairing.d.ts +11 -0
  46. package/dist-lib/lib/core/protocol-pairing.js +22 -0
  47. package/dist-lib/lib/core/sync/index.js +2 -16
  48. package/dist-lib/lib/core.d.ts +43 -49
  49. package/dist-lib/lib/core.js +451 -909
  50. package/dist-lib/lib/diagnostics.d.ts +7 -0
  51. package/dist-lib/lib/diagnostics.js +22 -0
  52. package/dist-lib/lib/doctor/checks.d.ts +15 -0
  53. package/dist-lib/lib/doctor/checks.js +269 -0
  54. package/dist-lib/lib/doctor/redact.d.ts +4 -0
  55. package/dist-lib/lib/doctor/redact.js +31 -0
  56. package/dist-lib/lib/doctor/render.d.ts +8 -0
  57. package/dist-lib/lib/doctor/render.js +83 -0
  58. package/dist-lib/lib/doctor/tree.d.ts +5 -0
  59. package/dist-lib/lib/doctor/tree.js +167 -0
  60. package/dist-lib/lib/downloads-allowed-pusher.d.ts +10 -0
  61. package/dist-lib/lib/downloads-allowed-pusher.js +37 -0
  62. package/dist-lib/lib/guards.d.ts +1 -0
  63. package/dist-lib/lib/guards.js +4 -0
  64. package/dist-lib/lib/inference-calls.js +1 -5
  65. package/dist-lib/lib/kv-cache-reclaim.d.ts +2 -1
  66. package/dist-lib/lib/kv-cache-reclaim.js +24 -0
  67. package/dist-lib/lib/mesh-scoped-engine.js +1 -0
  68. package/dist-lib/lib/model-download-status.d.ts +1 -1
  69. package/dist-lib/lib/model-download.d.ts +9 -1
  70. package/dist-lib/lib/model-download.js +48 -3
  71. package/dist-lib/lib/model-memory-cap.d.ts +29 -0
  72. package/dist-lib/lib/model-memory-cap.js +69 -0
  73. package/dist-lib/lib/models-watch.d.ts +1 -1
  74. package/dist-lib/lib/models-watch.js +4 -4
  75. package/dist-lib/lib/oauth/flow.js +1 -1
  76. package/dist-lib/lib/oauth/loopback.js +16 -2
  77. package/dist-lib/lib/oauth/redirect-types.d.ts +1 -0
  78. package/dist-lib/lib/run-replay.d.ts +26 -0
  79. package/dist-lib/lib/run-replay.js +107 -0
  80. package/dist-lib/schema/config.d.ts +1 -1
  81. package/dist-lib/schema/config.js +1 -1
  82. package/dist-lib/schema/content.js +9 -1
  83. package/dist-lib/schema/engine.js +30 -5
  84. package/dist-lib/schema/local.js +3 -2
  85. package/dist-lib/schema/qvac.js +18 -1
  86. package/dist-lib/spec/bind.js +3 -0
  87. package/dist-lib/spec/calls.js +1 -0
  88. package/dist-lib/spec/capabilities.d.ts +2 -0
  89. package/dist-lib/spec/config/hyperconf/index.js +1 -1
  90. package/dist-lib/spec/hrpc/hrpc.json +13 -0
  91. package/dist-lib/spec/hrpc/index.js +15 -3
  92. package/dist-lib/spec/hrpc/messages.js +646 -557
  93. package/dist-lib/spec/hyperschema/index.js +646 -557
  94. package/dist-lib/spec/hyperschema/schema.json +80 -0
  95. package/dist-lib/spec/hyperschema/types.d.ts +20 -3
  96. package/dist-lib/spec/local/hyperdb/messages.js +22 -10
  97. package/dist-lib/spec/local/hyperschema/index.js +16 -7
  98. package/dist-lib/spec/local/hyperschema/schema.json +10 -1
  99. package/dist-lib/spec/local/hyperschema/types.d.ts +3 -2
  100. package/dist-lib/spec/mesh/hyperdb/db.json +16 -1
  101. package/dist-lib/spec/mesh/hyperdb/index.js +71 -2
  102. package/dist-lib/spec/mesh/hyperdb/messages.js +477 -240
  103. package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +13 -1
  104. package/dist-lib/spec/mesh/hyperdispatch/index.js +35 -1
  105. package/dist-lib/spec/mesh/hyperdispatch/messages.js +180 -7
  106. package/dist-lib/spec/mesh/hyperschema/index.js +180 -7
  107. package/dist-lib/spec/mesh/hyperschema/schema.json +98 -1
  108. package/dist-lib/spec/mesh/hyperschema/types.d.ts +26 -2
  109. package/package.json +9 -17
  110. package/tui/README.md +7 -1
  111. package/tui/app.mjs +245 -11
  112. package/tui/boot.mjs +1 -0
  113. package/tui/index.mjs +1 -0
  114. package/tui/lib/render.mjs +25 -1
  115. package/worker/assistant-sidecar.mjs +5 -2
  116. package/dist-lib/lib/core/device-invites.d.ts +0 -11
  117. package/dist-lib/lib/core/device-invites.js +0 -63
  118. package/dist-lib/lib/core/grant-expiry.d.ts +0 -11
  119. package/dist-lib/lib/core/grant-expiry.js +0 -66
  120. package/dist-lib/lib/core/mesh-seed-handoff.d.ts +0 -27
  121. package/dist-lib/lib/core/mesh-seed-handoff.js +0 -53
  122. package/dist-lib/lib/oauth/loopback.stub.d.ts +0 -3
  123. package/dist-lib/lib/oauth/loopback.stub.js +0 -6
@@ -11,7 +11,9 @@ import Top from 'process-top';
11
11
  import ReadyGuard from 'ready-guard';
12
12
  import ReadyResource from 'ready-resource';
13
13
  import ScopeLock from 'scope-lock';
14
+ import { isAndroid } from 'which-runtime';
14
15
  import { createApi } from './api/index.js';
16
+ import { writeAutoDownloadModels } from './api/app-config.js';
15
17
  import { NO_SUCH_MESH, requireMesh } from './api/current-mesh.js';
16
18
  import { ChatMoves } from './core/chat-move.js';
17
19
  import { asEncoded, watchModels } from './models-watch.js';
@@ -23,9 +25,6 @@ import { createFsDriveHost } from './core/fs-drives.js';
23
25
  import { sweepDriveStaging } from './fs-drive-bridge.js';
24
26
  import { decodeMeshInvite, encodeMeshInvite } from './core/invite.js';
25
27
  import { fileKeyOf, knownFile, referencedFile } from './core/files/local-index.js';
26
- import { grantKeyPair, inviteGrantTermsRefusal } from './core/invite-grants.js';
27
- import { belongsToAnotherMember, inviteStanding, memberStanding, removedBy } from './core/contract/invites.js';
28
- import { syncStatusSnapshot } from './core/sync-status.js';
29
28
  import { openLocalStore } from './core/local/index.js';
30
29
  import { MAX_MESH_NAME_CHARS, meshNameWithinBounds } from '../schema/qvac.js';
31
30
  import { getAgent } from './core/agent.js';
@@ -33,27 +32,22 @@ import { countContents, migrateContents, withArchivedMesh } from './core/archive
33
32
  import { HostedAgents } from './core/hosted-agents.js';
34
33
  import { HarnessLoan } from './core/harness-loan.js';
35
34
  import { duplexPair } from '@qvac/harness/transport';
36
- import { signMemberWriterClaim, signWriterClaim, verifyMemberWriterClaim, verifyWriterClaim } from './core/device-attestation.js';
37
- import { HOST_SILENT, HOST_UNREACHABLE } from './core/handoff-channel.js';
38
- import { MeshSeedHandoff, SEED_REFUSED, ADMISSION_DEFERRED, SEED_ASKER_UNSEEN } from './core/mesh-seed-handoff.js';
39
35
  import { SecretHandoff } from './core/secret-handoff.js';
40
36
  import { InferenceCallsHandoff } from './core/inference-calls-handoff.js';
41
37
  import { createInferenceCallsReceiver, NO_RECORD_STORE } from './core/inference-calls-receiver.js';
42
38
  import { SkillOverlayHandoff } from './core/skill-overlay-handoff.js';
43
39
  import { createSkillOverlayReceiver } from './core/skill-overlay-receiver.js';
44
40
  import { createSkillSecretReceiver, HOST_REVOKED, NO_SUCH_AGENT, NOT_THE_HOST } from './core/skill-secrets.js';
45
- import { attestMemberClaim, claimAttestation, deviceInviteToken, openIdentity } from './core/identity.js';
41
+ import { attestDeviceProof, attestMemberClaim, claimAttestation, delegationFits, openIdentity } from './core/identity.js';
46
42
  import { openRecoveryCore, publishRecovery, readRecovery, recoveryKeys } from './core/account-recovery.js';
47
- import { admitInvitedDevice, attestUnattestedDevices, claimOwnMembership, legacyNameStamp, namedAt, renameFoundedMember, sealAccount } from './core/membership.js';
48
- import { DEVICE_INVITE_SPENT, openDeviceInvites } from './core/device-invites.js';
43
+ import { claimOwnMembership, legacyNameStamp, namedAt, renameFoundedMember } from './core/membership.js';
49
44
  import { runMigrations } from './core/migrations/index.js';
50
45
  import { backfillFileIndex } from './core/migrations/file-index.js';
51
46
  import { advertiseDeviceVersion, retractInventory } from './core/devices.js';
52
47
  import { OtaConfigReader, configCoreFor, resolveConfigChannel, vendoredMinSupportedVersion } from './core/ota-config.js';
53
48
  import { updateRequired } from './core/min-version.js';
54
49
  import { PendingDepartures } from './core/departures.js';
55
- import { isKickedRow } from './core/helpers/index.js';
56
- import { Mesh, claimableDevice, meshPublicKeyFromSeed } from './core/mesh.js';
50
+ import { Mesh } from './core/mesh.js';
57
51
  import { raceResource } from './race-resource.js';
58
52
  import { createSystemInfoSampler } from './core/system-info.js';
59
53
  import { setLogger } from './logger.js';
@@ -62,19 +56,19 @@ import { openLocalSpec } from './spec-loader.js';
62
56
  import { unref } from './unref.js';
63
57
  import { local } from '../spec/index.js';
64
58
  import { isRecord } from './guards.js';
65
- import { CHUNKS, DEVICES, JOINED_MESHES, MEMBERS, MESH_BLIND_PEERS, MESH_INVITE_GRANTS, MESH_INVITE_SPENDS, MESH_KINDS } from './core/collections.js';
59
+ import { CHUNKS, DEVICES, JOINED_MESHES, MEMBERS, MESH_BLIND_PEERS, MESH_SEEDS } from './core/collections.js';
60
+ import { isOwnRevokedRow } from './core/helpers/index.js';
66
61
  const QvacState = local.getEnum('@local/qvac-state');
67
62
  const top = new Top();
68
63
  const FILE_GC_INTERVAL_MS = 24 * 60 * 60 * 1000;
69
64
  const SWARM_KEYPAIR_NAME = 'qvac-swarm-keypair';
70
65
  const QVAC_CORE_NAMESPACE = 'qvac-core';
71
66
  const MESH_JOIN_CANCELED = 'Mesh join canceled';
72
- const GRANT_JOIN_TIMED_OUT = 'Join did not complete in time - the invite may already be spent, expired or revoked';
73
67
  const MESH_ID = /^[0-9a-f]{64}$/;
74
68
  const MESH_BELOW_FLOOR = 'Build is below the mesh floor';
75
69
  const MESH_WITHOUT_ACCOUNT = 'Recording a mesh needs the account mesh';
76
70
  export const ACCOUNT_MEMBER_UNKNOWN = 'This device is not part of its account yet';
77
- export const AWAITING_ADMISSION = 'awaiting-admission';
71
+ export const DEVICE_UNKNOWN = 'This device has no device details: pass `device` when creating Core';
78
72
  const MESH_WITHOUT_MEMBER = 'A mesh needs this device to belong to its account member';
79
73
  const NO_WRITABLE_ACCOUNT = 'The account mesh is not writable';
80
74
  const NOT_AN_ARCHIVED_ACCOUNT = 'Not an account this device left';
@@ -82,42 +76,27 @@ const NO_ACCOUNT_TO_RECOVER = 'This device has no account to make recoverable';
82
76
  const PHRASE_NOT_OURS = 'The phrase does not derive the identity this device holds';
83
77
  const PHRASE_NOT_A_MEMBER = "The phrase names no member of this account, so it could not recover it: publish from a device holding the account's phrase";
84
78
  const NO_RECOVERY_RECORD = 'No recovery record was found for this phrase';
85
- const RECOVERY_DEVICE_REMOVED = 'This device was removed from the account, so it cannot recover it: wipe it and recover as a new device';
86
79
  const RECOVERY_WAIT_MS = 30_000;
87
80
  export const MEMBER_INVITE_HOME_REFUSED = 'An account mesh cannot be invited into';
88
81
  export const DEVICE_INVITE_MESH_REFUSED = 'Only an account mesh can take a device invite';
89
82
  export const MESH_NAME_ACCOUNT_REFUSED = 'An account mesh has no shared name';
90
83
  export const INVITE_ACCOUNT_UNREADABLE = 'This device cannot reach its account to check';
84
+ export const DEVICE_INVITE_CHAIN_TOO_LONG = 'This device cannot invite another one: invite from another of your devices, or recover from your phrase';
91
85
  export const MESH_HELD_BY_SIBLING = 'Another of your devices holds this mesh: bring that device online, or invite from it instead';
92
- export const INVITE_GRANT_REVOKED = 'This invite was revoked';
93
- export const MESH_ALREADY_JOINED = 'This device is already in this mesh';
94
- export const MESH_ADMISSION_PENDING = 'This mesh has not admitted this device yet: try again in a moment';
95
- export const MEMBER_ALREADY_IN_MESH = 'Another of your devices is already in this mesh: join through it once it is online';
96
- export const DEVICE_OF_ANOTHER_MEMBER = 'This device belongs to another member in this mesh';
97
- export const MEMBER_REMOVED_SINCE_INVITE = 'You were removed from this mesh after this invite was made: ask for a new one';
98
- export const INVITE_GRANT_EXPIRED = 'This invite has expired';
99
- export const INVITE_GRANT_EXHAUSTED = 'This invite has already been used';
100
- export const SEALED_ACCOUNT_READER = 'This account admits no device by its seed alone: pair this device with a device invite';
101
- export const INVITER_OFFLINE = 'The device that made this invite is not online: bring it online and try again';
86
+ const MESH_DIED = 'the local database died';
102
87
  const MESH_NOT_EMPTY = 'this mesh is not empty: everyone in it will see the newcomer arrive, and the newcomer replays the whole history of the mesh, including what earlier members wrote';
103
- const UNANSWERED_SEED_REQUESTS = new Set([HOST_SILENT, SEED_ASKER_UNSEEN]);
104
88
  const INVITE_DHT_NODES_LIMIT = 3;
105
89
  const INVITE_BLIND_PEERS_LIMIT = 3;
106
- const GRANT_DEFAULT_MAX_USES = 1;
107
- const GRANT_DEFAULT_TTL_MS = 48 * 60 * 60 * 1000;
108
- const GRANT_VISIBLE_MS = 10_000;
109
90
  const DIAGNOSTICS_SAMPLE_MS = 2000;
110
- const ACCOUNT_WRITABLE_MS = 15_000;
111
- const GRANT_WRITABLE_MS = 60_000;
112
- const INVITER_RETRY_MS = 1_000;
113
- const ADMISSION_RETRIES = 60;
114
91
  export class Core extends ReadyResource {
115
92
  _storagePath;
116
93
  store;
117
94
  rootCorestore;
118
95
  storage;
96
+ _ownsStore;
97
+ _ownsSwarm;
98
+ _ownsWakeup;
119
99
  local;
120
- _deviceInvites;
121
100
  state;
122
101
  _transport;
123
102
  _apiLifecycle;
@@ -137,15 +116,14 @@ export class Core extends ReadyResource {
137
116
  _blindPeerKeys;
138
117
  _blindPeeringOpts;
139
118
  _fileGc;
119
+ _autoDownloadModels;
140
120
  _fileGcTimer = null;
141
121
  _fileGcRun = null;
142
122
  _meshSeed;
143
123
  _meshKey;
144
124
  _additionalNodes;
145
- _grantWritableMs;
146
- _accountWritableMs;
147
- _grantVisibleMs;
148
125
  _listModels;
126
+ _modelMemory = null;
149
127
  _deleteModel;
150
128
  _clearKvCache;
151
129
  _deleteChatKvCache;
@@ -194,16 +172,14 @@ export class Core extends ReadyResource {
194
172
  _identityPhrase = null;
195
173
  _identityPublicKey = null;
196
174
  _swarm;
175
+ _onSwarmConnection;
197
176
  _departures = null;
198
177
  _fsDrives = null;
199
178
  _blobServer = null;
200
179
  _wakeup;
201
- _meshSeedHandoff;
202
180
  _harnessLoan;
203
181
  _serveLoanedHarness = null;
204
182
  _following = new Map();
205
- _admissionRetries = new Map();
206
- _admissionRetryPending = new Set();
207
183
  _revokingInMeshes = null;
208
184
  _revokeInMeshesAgain = false;
209
185
  _sweepUnknownEverywhere = false;
@@ -212,10 +188,12 @@ export class Core extends ReadyResource {
212
188
  _skillOverlayHandoff;
213
189
  _inferenceCallsHandoff;
214
190
  _minting;
191
+ _backfill = Promise.resolve();
215
192
  _leaving;
216
193
  _kicked;
217
194
  _kickedPending;
218
195
  _peers;
196
+ _peerArrivedSuspended = false;
219
197
  _statusListeners;
220
198
  _listing;
221
199
  _diagnosticsProviders;
@@ -225,7 +203,7 @@ export class Core extends ReadyResource {
225
203
  _systemInfo;
226
204
  _statusError;
227
205
  _upgradeRequired;
228
- constructor(transport, { storagePath, logger = null, spec = openLocalSpec, bootstrapNodes = undefined, device = undefined, app = undefined, minSupportedVersion = undefined, otaConfigCore = undefined, blindPeerKeys = undefined, blindPeeringOpts = undefined, fileGc = null, meshSeed = undefined, meshKey = undefined, additionalNodes = undefined, listModels = undefined, deleteModel = undefined, clearKvCache = undefined, deleteChatKvCache = undefined, compactChat = undefined, modelCatalogWatch = undefined, retryModelDownload = undefined, transcribe = undefined, detectLanguage = undefined, previewModelSource = undefined, speak = undefined, speakStream = undefined, embedder = undefined, extractors = undefined, skills = undefined, skillCatalog = undefined, tools = undefined, hostsModelSource = false, vectorIndex = undefined, apiLifecycle = undefined, grantWritableMs = GRANT_WRITABLE_MS, accountWritableMs = ACCOUNT_WRITABLE_MS, grantVisibleMs = GRANT_VISIBLE_MS }) {
206
+ constructor(transport, { storagePath, store = undefined, swarm = undefined, wakeup = undefined, logger = null, spec = openLocalSpec, bootstrapNodes = undefined, device = undefined, app = undefined, minSupportedVersion = undefined, otaConfigCore = undefined, blindPeerKeys = undefined, blindPeeringOpts = undefined, fileGc = null, autoDownloadModels = undefined, meshSeed = undefined, meshKey = undefined, additionalNodes = undefined, listModels = undefined, deleteModel = undefined, clearKvCache = undefined, deleteChatKvCache = undefined, compactChat = undefined, modelCatalogWatch = undefined, retryModelDownload = undefined, transcribe = undefined, detectLanguage = undefined, previewModelSource = undefined, speak = undefined, speakStream = undefined, embedder = undefined, extractors = undefined, skills = undefined, skillCatalog = undefined, tools = undefined, hostsModelSource = false, vectorIndex = undefined, apiLifecycle = undefined }) {
229
207
  super();
230
208
  this._transport = transport;
231
209
  this._apiLifecycle = apiLifecycle ?? {
@@ -236,18 +214,21 @@ export class Core extends ReadyResource {
236
214
  if (logger) {
237
215
  setLogger(logger);
238
216
  }
217
+ if (swarm && (!store || !wakeup)) {
218
+ throw new Error('swarm may only be passed alongside store and wakeup');
219
+ }
220
+ this._ownsStore = !store;
221
+ this._ownsSwarm = !swarm;
222
+ this._ownsWakeup = !wakeup;
239
223
  this._storagePath = storagePath;
240
224
  this.storage = new CorestoreStorage(this._storagePath, {
241
225
  blobGarbageCollectionAgeCutOff: 1.0,
242
226
  blobGarbageCollectionForceThreshold: 0.5,
243
227
  blobFileSize: 64 * 1024 * 1024
244
228
  });
245
- this.rootCorestore = new Corestore(this.storage, {
246
- globalCache: new Rache()
247
- });
229
+ this.rootCorestore = store ?? new Corestore(this.storage, { globalCache: new Rache() });
248
230
  this.store = this.rootCorestore.namespace(QVAC_CORE_NAMESPACE);
249
231
  this.local = openLocalStore(this.storage);
250
- this._deviceInvites = openDeviceInvites(this.local);
251
232
  this.state = QvacState.RESUME;
252
233
  this._spec = spec;
253
234
  this._bootstrapNodes = bootstrapNodes;
@@ -264,12 +245,10 @@ export class Core extends ReadyResource {
264
245
  this._blindPeerKeys = blindPeerKeys;
265
246
  this._blindPeeringOpts = blindPeeringOpts;
266
247
  this._fileGc = fileGc;
248
+ this._autoDownloadModels = autoDownloadModels;
267
249
  this._meshSeed = meshSeed;
268
250
  this._meshKey = meshKey;
269
251
  this._additionalNodes = additionalNodes;
270
- this._grantWritableMs = grantWritableMs;
271
- this._accountWritableMs = accountWritableMs;
272
- this._grantVisibleMs = grantVisibleMs;
273
252
  this._listModels = listModels ?? null;
274
253
  this._deleteModel = deleteModel ?? null;
275
254
  this._clearKvCache = clearKvCache ?? null;
@@ -317,17 +296,15 @@ export class Core extends ReadyResource {
317
296
  this._api = null;
318
297
  this._clients = new Set();
319
298
  this._deviceKeyPair = null;
320
- this._swarm = null;
321
- this._wakeup = new Wakeup();
299
+ this._swarm = swarm ?? null;
300
+ this._onSwarmConnection = (conn) => this._onConnection(conn);
301
+ this._wakeup = wakeup ?? new Wakeup();
322
302
  this._secretHandoff = new SecretHandoff({
323
303
  onrequest: (request, from) => this._storeHandedOffSecret(request, from)
324
304
  });
325
305
  this._skillOverlayHandoff = new SkillOverlayHandoff({
326
306
  onrequest: (op, from) => this._serveSkillOverlayOp(op, from)
327
307
  });
328
- this._meshSeedHandoff = new MeshSeedHandoff({
329
- onrequest: (request, from) => this._serveMeshSeed(request, from)
330
- });
331
308
  this._harnessLoan = new HarnessLoan({ onborrow: (from) => this._lendHarness(from) });
332
309
  this._inferenceCallsHandoff = new InferenceCallsHandoff({
333
310
  onrequest: (request, from) => this._serveInferenceCalls(request, from)
@@ -336,7 +313,7 @@ export class Core extends ReadyResource {
336
313
  this._leaving = null;
337
314
  this._kicked = false;
338
315
  this._kickedPending = false;
339
- this._peers = new Set();
316
+ this._peers = new Map();
340
317
  this._statusListeners = new Set();
341
318
  this._listing = null;
342
319
  this._diagnosticsProviders = new Set();
@@ -369,7 +346,7 @@ export class Core extends ReadyResource {
369
346
  return this._peers.size;
370
347
  }
371
348
  get peerDeviceIds() {
372
- const ids = [...this._peers].map((conn) => b4a.toHex(conn.remotePublicKey));
349
+ const ids = [...this._peers.keys()].map((conn) => b4a.toHex(conn.remotePublicKey));
373
350
  return [...new Set(ids)].sort();
374
351
  }
375
352
  async _open() {
@@ -380,13 +357,18 @@ export class Core extends ReadyResource {
380
357
  }
381
358
  async _runBootOffline() {
382
359
  try {
383
- this._deviceKeyPair = await this.store.createKeyPair(SWARM_KEYPAIR_NAME);
360
+ this._deviceKeyPair =
361
+ this._swarm?.keyPair ?? (await this.store.createKeyPair(SWARM_KEYPAIR_NAME));
384
362
  this._identityPublicKey = (await this.local.readIdentity())?.publicKey ?? null;
385
363
  this._localDevice = await this.local.ensureDevice(this._deviceKeyPair.publicKey, this._device);
364
+ if (this._autoDownloadModels !== undefined) {
365
+ await writeAutoDownloadModels(this.local, this._autoDownloadModels);
366
+ }
386
367
  const swarm = this._openSwarm(this._deviceKeyPair);
387
368
  this._departures = new PendingDepartures(this.store, this.local, {
388
369
  swarm,
389
370
  isPooled: (key) => this._pooled(b4a.toHex(key)) !== null,
371
+ deviceId: () => this._deviceKeyPair?.publicKey ?? null,
390
372
  logger: this._log,
391
373
  blindPeerKeys: this._resolveBlindPeerKeys(),
392
374
  blindPeeringOpts: this._blindPeeringOpts,
@@ -433,7 +415,7 @@ export class Core extends ReadyResource {
433
415
  computeLender: () => this._computeLender(),
434
416
  pushSkillSecret: (request) => this.pushSkillSecret(request),
435
417
  listModels: this._hostsModelSource || this._listModels
436
- ? async (request) => ({ models: (await this._readModels(request)) ?? [] })
418
+ ? async (request) => (await this._readModelList(request)) ?? { models: [] }
437
419
  : null,
438
420
  deleteModel: this._hostsModelSource || this._deleteModel
439
421
  ? (options) => this._deleteModel?.(options) ??
@@ -466,7 +448,7 @@ export class Core extends ReadyResource {
466
448
  ? () => this._watchModelCatalog()
467
449
  : null,
468
450
  modelsWatch: this._hostsModelSource || this._modelCatalogWatch
469
- ? (request) => watchModels(() => this._modelCatalogRevisions(), () => this._readModels(request))
451
+ ? (request) => watchModels(() => this._modelCatalogRevisions(), () => this._readModelList(request))
470
452
  : null,
471
453
  retryModelDownload: this._hostsModelSource || this._retryModelDownload
472
454
  ? ({ name }) => this._retryModelDownload?.(name) ??
@@ -581,8 +563,7 @@ export class Core extends ReadyResource {
581
563
  await mesh.close();
582
564
  }
583
565
  logger.info('core', 'closing storage');
584
- await this.store?.close();
585
- await this.rootCorestore?.close();
566
+ await this._closeStore();
586
567
  }
587
568
  finally {
588
569
  this._offlineBoot.destroy(err instanceof Error ? err : new Error(String(err)));
@@ -851,13 +832,15 @@ export class Core extends ReadyResource {
851
832
  if (this._recoveryPeering) {
852
833
  await this._recoveryPeering.suspend();
853
834
  }
854
- if (this._swarm) {
835
+ if (this._swarm && this._ownsSwarm) {
855
836
  await this._swarm.suspend();
856
837
  }
857
838
  if (this._blobServer) {
858
839
  await this._blobServer.suspend();
859
840
  }
860
- await this.store.suspend();
841
+ if (!isAndroid) {
842
+ await this.storage.suspend();
843
+ }
861
844
  this._log?.info('lifecycle', 'core suspend');
862
845
  }
863
846
  async resume() {
@@ -877,11 +860,11 @@ export class Core extends ReadyResource {
877
860
  await this.ready();
878
861
  }
879
862
  logger.info('core', 'resuming');
880
- await this.store.resume();
863
+ await this.storage.resume();
881
864
  if (this._blobServer) {
882
865
  await this._blobServer.resume();
883
866
  }
884
- if (this._swarm) {
867
+ if (this._swarm && this._ownsSwarm) {
885
868
  await this._swarm.resume();
886
869
  }
887
870
  if (this._configPeering) {
@@ -897,6 +880,10 @@ export class Core extends ReadyResource {
897
880
  await this._departures.resume();
898
881
  }
899
882
  this._departures?.flush();
883
+ if (this._peerArrivedSuspended) {
884
+ this._peerArrivedSuspended = false;
885
+ this._retryOnPeer();
886
+ }
900
887
  this._startFileGc();
901
888
  if (this._swarm) {
902
889
  void this._refreshTopics(this._swarm).catch(noop);
@@ -905,15 +892,27 @@ export class Core extends ReadyResource {
905
892
  this._log?.info('lifecycle', 'core resume');
906
893
  }
907
894
  async _refreshTopics(swarm) {
908
- for (const discovery of swarm.topics()) {
895
+ for (const discovery of this._joinedTopics(swarm)) {
909
896
  await discovery.flushed();
910
897
  await discovery.refresh();
911
898
  }
912
899
  }
900
+ _joinedTopics(swarm) {
901
+ if (this._ownsSwarm) {
902
+ return swarm.topics();
903
+ }
904
+ const meshes = [...this._meshes.values()].map(({ mesh }) => mesh);
905
+ if (this._candidateMesh) {
906
+ meshes.push(this._candidateMesh);
907
+ }
908
+ return meshes
909
+ .map((mesh) => swarm.status(mesh.discoveryKey))
910
+ .filter((discovery) => discovery !== null);
911
+ }
913
912
  async _close() {
914
913
  logger.info('core', 'closing');
915
914
  if (this.state === QvacState.SUSPEND) {
916
- await this.store.resume();
915
+ await this.storage.resume();
917
916
  }
918
917
  this._catalogHooks.destroy(new Error('core is closing'));
919
918
  this._modelCatalogWatchSource.destroy(new Error('core is closing'));
@@ -958,18 +957,29 @@ export class Core extends ReadyResource {
958
957
  this._systemInfo?.destroy();
959
958
  this._systemInfo = null;
960
959
  logger.info('core', 'closing storage');
961
- await this.store?.close();
962
- await this.rootCorestore?.close();
960
+ await this._closeStore();
963
961
  this._log?.info('lifecycle', 'core closed');
964
962
  }
963
+ async _closeStore() {
964
+ await this.store?.close();
965
+ if (this._ownsStore) {
966
+ await this.rootCorestore?.close();
967
+ }
968
+ await this.storage.close();
969
+ }
965
970
  _openSwarm(keyPair) {
966
- this._swarm = new Hyperswarm({
971
+ this._swarm ??= new Hyperswarm({
967
972
  keyPair,
968
973
  bootstrap: this._bootstrapNodes,
969
974
  relayThrough: this._bootstrapNodes ? undefined : () => BLIND_RELAY_KEYS
970
975
  });
971
- this._swarm.on('connection', (conn) => this._onConnection(conn));
972
- this._log?.info(['p2p', 'swarm'], 'swarm created', {
976
+ this._swarm.on('connection', this._onSwarmConnection);
977
+ for (const conn of this._swarm.connections) {
978
+ if (isPeerConnection(conn)) {
979
+ this._onConnection(conn);
980
+ }
981
+ }
982
+ this._log?.info(['p2p', 'swarm'], 'swarm opened', {
973
983
  deviceKey: b4a.toHex(keyPair.publicKey).slice(0, 8)
974
984
  });
975
985
  for (const node of this._additionalNodes ?? []) {
@@ -1036,31 +1046,61 @@ export class Core extends ReadyResource {
1036
1046
  });
1037
1047
  try {
1038
1048
  await mesh.ready();
1039
- await raceResource(mesh.autobee, (async () => {
1040
- const birth = sharing
1041
- ? await this._accountBirthMember(mesh, session)
1042
- : await this._mintBirthMember(mesh, deviceId, session);
1043
- await mesh.appendBirthOp(this._requireDeviceKeyPair(), this._localDevice ?? undefined, {
1044
- member: birth?.member,
1045
- sharing
1046
- });
1049
+ if (session.baseKey && mesh.localLength === 0) {
1050
+ this._requireLocalDevice();
1047
1051
  await this.local.recordMesh(mesh.key, session, { deviceId, account });
1048
- if (account && birth?.mnemonic) {
1049
- await this._publishRecovery(birth.mnemonic, {
1050
- meshKey: mesh.key,
1051
- seed: session.seed,
1052
- createdAt: Date.now()
1053
- });
1054
- }
1055
- })(), 'mesh');
1052
+ void this._appendJoinLater(mesh, () => this._knownBirthMember(mesh, deviceId), sharing);
1053
+ }
1054
+ else {
1055
+ await this._appendOwnBirth(mesh, session, { deviceId, account, sharing });
1056
+ }
1056
1057
  }
1057
1058
  catch (err) {
1059
+ if (mesh.opened && mesh.interrupted && !this._upgradeRequired) {
1060
+ this._setUpgradeRequired('halt');
1061
+ }
1058
1062
  await mesh.close().catch(noop);
1059
1063
  throw err;
1060
1064
  }
1061
1065
  this._poolEntry(mesh);
1062
1066
  return mesh;
1063
1067
  }
1068
+ async _appendOwnBirth(mesh, session, { deviceId, account, sharing }) {
1069
+ await raceResource(mesh.autobee, (async () => {
1070
+ const birth = sharing
1071
+ ? await this._accountBirthMember(mesh, session)
1072
+ : await this._mintBirthMember(mesh, deviceId, session);
1073
+ await mesh.appendBirthOp(this._requireDeviceKeyPair(), this._localDevice ?? undefined, {
1074
+ member: birth?.member,
1075
+ sharing
1076
+ });
1077
+ await this.local.recordMesh(mesh.key, session, { deviceId, account });
1078
+ if (account && birth?.mnemonic) {
1079
+ await this._publishRecovery(birth.mnemonic, {
1080
+ meshKey: mesh.key,
1081
+ seed: session.seed,
1082
+ createdAt: Date.now()
1083
+ });
1084
+ }
1085
+ })(), 'mesh');
1086
+ }
1087
+ _requireLocalDevice() {
1088
+ if (!this._localDevice)
1089
+ throw new Error(DEVICE_UNKNOWN);
1090
+ return this._localDevice;
1091
+ }
1092
+ _appendJoinLater(mesh, member, sharing = false) {
1093
+ const birth = async () => ({ member: (await member()) ?? undefined, sharing });
1094
+ const appended = mesh.appendBirthOp(this._requireDeviceKeyPair(), this._localDevice ?? undefined, birth);
1095
+ return appended.catch((err) => {
1096
+ if (this.closing || mesh.closing)
1097
+ return;
1098
+ if (mesh.interrupted && !this._upgradeRequired) {
1099
+ this._setUpgradeRequired('halt');
1100
+ }
1101
+ this._log?.warn(['p2p', 'mesh'], 'appending the join failed', err);
1102
+ });
1103
+ }
1064
1104
  async _accountBirthMember(mesh, session) {
1065
1105
  if (!this._localDevice || session.baseKey || mesh.localLength > 0) {
1066
1106
  return undefined;
@@ -1173,6 +1213,7 @@ export class Core extends ReadyResource {
1173
1213
  }
1174
1214
  async recoverAccount(mnemonic, { timeout = RECOVERY_WAIT_MS } = {}) {
1175
1215
  await this._awaitMesh();
1216
+ this._requireLocalDevice();
1176
1217
  const identity = await openIdentity(mnemonic);
1177
1218
  const keys = recoveryKeys(identity);
1178
1219
  const core = openRecoveryCore(this.store, keys);
@@ -1196,7 +1237,8 @@ export class Core extends ReadyResource {
1196
1237
  ownsTransition = true;
1197
1238
  this._minting = true;
1198
1239
  this._notifyStatus();
1199
- await this._recoverAccount(identity, record);
1240
+ const joined = await this._joinRecoveredAccount(identity, record);
1241
+ await this._rerootRecovered(identity, record, joined);
1200
1242
  await this._markRecoverable(record, core);
1201
1243
  await this._adoptRecoveryCore(core, keys.coreKeyPair.publicKey);
1202
1244
  }
@@ -1213,21 +1255,24 @@ export class Core extends ReadyResource {
1213
1255
  this._departures?.flush();
1214
1256
  }
1215
1257
  }
1216
- async _recoverAccount(identity, record) {
1258
+ async _joinRecoveredAccount(identity, record) {
1217
1259
  const deviceId = this._requireDeviceKeyPair().publicKey;
1218
1260
  const proof = await identity.bootstrap(deviceId);
1219
1261
  const meshId = b4a.toHex(record.meshKey);
1220
1262
  await this._opening.get(meshId)?.catch(noop);
1221
1263
  const pooled = this._pooled(meshId);
1222
- const recovery = pooled?.writable ? undefined : { memberId: identity.identityPublicKey, proof };
1223
1264
  if (pooled?.writable) {
1224
1265
  if (!this._isAccount(pooled)) {
1225
1266
  await this._adoptAccount(pooled);
1226
1267
  }
1268
+ return { proof, recovered: false };
1227
1269
  }
1228
- else {
1229
- await this._joinMesh({ seed: record.seed, baseKey: record.meshKey, fastForward: null, recovery }, pooled);
1230
- }
1270
+ const recovery = { memberId: identity.identityPublicKey, proof };
1271
+ await this._joinMesh({ seed: record.seed, baseKey: record.meshKey, fastForward: null, recovery }, pooled);
1272
+ return { proof, recovered: true };
1273
+ }
1274
+ async _rerootRecovered(identity, record, { proof, recovered }) {
1275
+ const deviceId = this._requireDeviceKeyPair().publicKey;
1231
1276
  const mesh = this._accountMesh();
1232
1277
  if (!mesh?.key || !b4a.equals(mesh.key, record.meshKey)) {
1233
1278
  throw new Error('the recovered account did not adopt');
@@ -1243,7 +1288,7 @@ export class Core extends ReadyResource {
1243
1288
  createdAt: now
1244
1289
  });
1245
1290
  this._identityPublicKey = identity.identityPublicKey;
1246
- if (recovery) {
1291
+ if (recovered) {
1247
1292
  return;
1248
1293
  }
1249
1294
  await mesh.dispatch('attest-member-device', {
@@ -1297,23 +1342,12 @@ export class Core extends ReadyResource {
1297
1342
  if (!local || mesh.localLength > 0) {
1298
1343
  return undefined;
1299
1344
  }
1300
- const now = Date.now();
1301
- const attestation = claimAttestation(mesh.publicKey, deviceId, mesh.localWriterKey);
1302
- const known = await this._knownMember(mesh);
1345
+ const known = await this._knownBirthMember(mesh, deviceId);
1303
1346
  if (known) {
1304
- const identity = await this.local.readIdentity();
1305
- const name = identity?.displayName;
1306
- const ours = identity && name && namedAt(identity) >= known.updatedAt;
1307
- return {
1308
- member: {
1309
- id: known.id,
1310
- name: ours ? name : known.name,
1311
- joinedAt: now,
1312
- updatedAt: ours ? namedAt(identity) : known.updatedAt
1313
- },
1314
- proof: await attestMemberClaim(attestation, this._requireDeviceKeyPair(), known.proof)
1315
- };
1347
+ return known;
1316
1348
  }
1349
+ const now = Date.now();
1350
+ const attestation = claimAttestation(mesh.publicKey, deviceId, mesh.localWriterKey);
1317
1351
  const { identity, mnemonic } = await this.local.ensureIdentity(deviceId, now);
1318
1352
  this._identityPublicKey = identity.publicKey;
1319
1353
  this._holdIdentityPhrase(mnemonic);
@@ -1328,6 +1362,46 @@ export class Core extends ReadyResource {
1328
1362
  proof
1329
1363
  };
1330
1364
  }
1365
+ async _knownBirthMember(mesh, deviceId) {
1366
+ if (!this._localDevice || mesh.localLength > 0)
1367
+ return null;
1368
+ const known = await this._knownMember(mesh);
1369
+ if (!known) {
1370
+ return null;
1371
+ }
1372
+ const identity = await this.local.readIdentity();
1373
+ const name = identity?.displayName;
1374
+ const ours = identity && name && namedAt(identity) >= known.updatedAt;
1375
+ const attestation = claimAttestation(mesh.publicKey, deviceId, mesh.localWriterKey);
1376
+ return {
1377
+ member: {
1378
+ id: known.id,
1379
+ name: ours ? name : known.name,
1380
+ joinedAt: Date.now(),
1381
+ updatedAt: ours ? namedAt(identity) : known.updatedAt
1382
+ },
1383
+ proof: await attestMemberClaim(attestation, this._requireDeviceKeyPair(), known.proof)
1384
+ };
1385
+ }
1386
+ async _recoveryBirthMember(mesh, recovery) {
1387
+ const deviceKeyPair = this._requireDeviceKeyPair();
1388
+ const attestation = claimAttestation(mesh.publicKey, deviceKeyPair.publicKey, mesh.localWriterKey);
1389
+ await this.local.recordMemberProof({
1390
+ meshKey: mesh.key,
1391
+ memberId: recovery.memberId,
1392
+ proof: recovery.proof,
1393
+ attestedAt: 0
1394
+ });
1395
+ return {
1396
+ member: {
1397
+ id: recovery.memberId,
1398
+ name: this._localDevice?.name ?? '',
1399
+ joinedAt: Date.now(),
1400
+ updatedAt: 0
1401
+ },
1402
+ proof: await attestMemberClaim(attestation, deviceKeyPair, recovery.proof)
1403
+ };
1404
+ }
1331
1405
  _holdIdentityPhrase(mnemonic) {
1332
1406
  if (!mnemonic)
1333
1407
  return;
@@ -1476,8 +1550,9 @@ export class Core extends ReadyResource {
1476
1550
  });
1477
1551
  }
1478
1552
  await this._claimOwnMembership(mesh, context);
1479
- await attestUnattestedDevices(mesh, context, this._deviceInvites);
1480
- await this._sealAccount(mesh);
1553
+ await this._backfillMeshSeeds(mesh).catch((err) => {
1554
+ this._log?.warn(['p2p', 'mesh'], 'backfilling mesh seeds failed', err);
1555
+ });
1481
1556
  await this._renameFoundedMember(mesh, context);
1482
1557
  await this._revokeInMeshes();
1483
1558
  }
@@ -1488,12 +1563,6 @@ export class Core extends ReadyResource {
1488
1563
  entry.memberLock.unlock();
1489
1564
  }
1490
1565
  }
1491
- async _sealAccount(mesh) {
1492
- if ((this._meshSeed && b4a.equals(this._meshSeed, mesh.seed)) || !this._isAccount(mesh)) {
1493
- return;
1494
- }
1495
- await sealAccount(mesh, { deviceKeyPair: this._requireDeviceKeyPair(), log: this._log });
1496
- }
1497
1566
  async _renameFoundedMember(mesh, context) {
1498
1567
  let identity = await this.local.readIdentity();
1499
1568
  if (!identity) {
@@ -1563,7 +1632,16 @@ export class Core extends ReadyResource {
1563
1632
  };
1564
1633
  }
1565
1634
  }
1566
- return null;
1635
+ return this._recordedMember();
1636
+ }
1637
+ async _recordedMember() {
1638
+ const account = await this._storedAccountKey();
1639
+ const link = account ? await this.local.readMemberProof(account) : null;
1640
+ if (!link) {
1641
+ return null;
1642
+ }
1643
+ const name = (await this.local.readIdentity())?.displayName ?? '';
1644
+ return { id: link.memberId, name, updatedAt: 0, proof: link.proof };
1567
1645
  }
1568
1646
  _migrationContext() {
1569
1647
  const local = this._localDevice;
@@ -1774,6 +1852,7 @@ export class Core extends ReadyResource {
1774
1852
  throw new Error('Invalid mesh invite');
1775
1853
  }
1776
1854
  await this._awaitMesh();
1855
+ this._requireLocalDevice();
1777
1856
  await this._meshTransitionLock.lock();
1778
1857
  let ownsTransition = false;
1779
1858
  try {
@@ -1816,15 +1895,10 @@ export class Core extends ReadyResource {
1816
1895
  throw new Error(MESH_JOIN_CANCELED);
1817
1896
  }
1818
1897
  async _joinMesh(invite, retire = null) {
1819
- const cancellation = {
1820
- canceled: false,
1821
- accepting: true,
1822
- deadline: Date.now() + this._grantVisibleMs,
1823
- account: !invite.memberInvite
1824
- };
1898
+ const cancellation = { canceled: false, accepting: true, account: !invite.memberInvite };
1825
1899
  this._joinCancellation = cancellation;
1826
1900
  try {
1827
- if (invite.memberInvite && invite.inviteNonce) {
1901
+ if (invite.memberInvite) {
1828
1902
  await this._joinMeshInvite(invite);
1829
1903
  }
1830
1904
  else if (await this._reopenOwnAccount(invite)) {
@@ -1841,7 +1915,7 @@ export class Core extends ReadyResource {
1841
1915
  }
1842
1916
  }
1843
1917
  async _reopenOwnAccount(invite) {
1844
- if (invite.recovery || !invite.memberId || !invite.inviteNonce)
1918
+ if (invite.recovery || !invite.memberId)
1845
1919
  return false;
1846
1920
  const stored = await this._storedAccountKey();
1847
1921
  if (!stored || !b4a.equals(stored, invite.baseKey)) {
@@ -1851,12 +1925,7 @@ export class Core extends ReadyResource {
1851
1925
  return false;
1852
1926
  }
1853
1927
  const account = await this._openPooledMesh(b4a.toHex(invite.baseKey));
1854
- const own = await account.view.get(DEVICES, {
1855
- id: this._requireDeviceKeyPair().publicKey
1856
- });
1857
- if (!own ||
1858
- own.revoked ||
1859
- !(own.writerKey && b4a.equals(own.writerKey, account.localWriterKey))) {
1928
+ if (!(await this._keepsWriter(account))) {
1860
1929
  return false;
1861
1930
  }
1862
1931
  if (!this._isAccount(account)) {
@@ -1889,7 +1958,7 @@ export class Core extends ReadyResource {
1889
1958
  const meshId = b4a.toHex(invite.baseKey);
1890
1959
  await this._following.get(meshId);
1891
1960
  const joining = (async () => {
1892
- if (!(await this._openOwnMesh(invite))) {
1961
+ if (!(await this._reopenRecordedMesh(invite))) {
1893
1962
  await this._claimJoin(invite, this._pooled(meshId));
1894
1963
  }
1895
1964
  })();
@@ -1904,6 +1973,22 @@ export class Core extends ReadyResource {
1904
1973
  }
1905
1974
  }
1906
1975
  }
1976
+ async _reopenRecordedMesh(invite) {
1977
+ if (!(await this.local.readMeshSession(invite.baseKey)))
1978
+ return false;
1979
+ return this._keepsWriter(await this._openPooledMesh(b4a.toHex(invite.baseKey)));
1980
+ }
1981
+ async _keepsWriter(mesh) {
1982
+ if (mesh.writable)
1983
+ return true;
1984
+ if (await mesh.ownWriterRefused()) {
1985
+ return false;
1986
+ }
1987
+ const own = await mesh.view.get(DEVICES, {
1988
+ id: this._requireDeviceKeyPair().publicKey
1989
+ });
1990
+ return !(own && isOwnRevokedRow(mesh, own));
1991
+ }
1907
1992
  async _runJoinMesh(invite, claim) {
1908
1993
  const deviceId = this._requireDeviceKeyPair().publicKey;
1909
1994
  const source = this._accountMesh();
@@ -1942,32 +2027,44 @@ export class Core extends ReadyResource {
1942
2027
  this._joinCancellation.accepting = false;
1943
2028
  }
1944
2029
  try {
1945
- if (source && !invite.memberInvite && (await this._departureHasAudience(source))) {
1946
- await this.local.recordDeparture(source.key, { creator: source.minted });
1947
- }
1948
2030
  await this.local.recordMesh(candidate.key, session, { deviceId });
1949
2031
  }
1950
2032
  catch (error) {
1951
2033
  await this._discardCandidateMesh();
1952
2034
  throw error;
1953
2035
  }
1954
- return this._landJoinedMesh(invite, candidate, { source, deviceId });
2036
+ const member = await this._candidateBirthMember(candidate, invite);
2037
+ const appended = this._appendJoinLater(candidate, async () => member);
2038
+ return this._landJoinedMesh(candidate, {
2039
+ source,
2040
+ deviceId,
2041
+ memberInvite: !!invite.memberInvite,
2042
+ appended
2043
+ });
1955
2044
  }
1956
- async _landJoinedMesh(invite, candidate, { source, deviceId }) {
1957
- if (!invite.memberInvite) {
2045
+ async _landJoinedMesh(candidate, { source, deviceId, memberInvite, appended }) {
2046
+ if (!memberInvite) {
1958
2047
  await this._departOldAccount(candidate.key, source);
1959
2048
  }
1960
2049
  if (this._blockedByMinVersion()) {
1961
- if (!invite.memberInvite) {
2050
+ if (!memberInvite) {
1962
2051
  await this.local.setAccountMesh(deviceId, candidate.key);
1963
2052
  }
1964
- await this._retireMesh(candidate).catch((err) => this._log?.warn(['p2p', 'mesh'], 'withholding the joined mesh failed', err));
2053
+ void appended
2054
+ .then(() => this._retireMesh(candidate))
2055
+ .catch((err) => this._log?.warn(['p2p', 'mesh'], 'withholding the joined mesh failed', err));
1965
2056
  return null;
1966
2057
  }
1967
- if (invite.memberInvite || candidate.compute) {
2058
+ if (memberInvite || candidate.compute) {
1968
2059
  this._candidateMesh = null;
2060
+ if (!candidate.writable) {
2061
+ await this.local.markAwaitingAdmission(candidate.key, { awaitingAdmission: true });
2062
+ }
1969
2063
  this._wireMesh(this._poolEntry(candidate));
1970
2064
  this._bringMeshOnline(candidate);
2065
+ if (candidate.writable) {
2066
+ await this.local.markAwaitingAdmission(candidate.key, { awaitingAdmission: false });
2067
+ }
1971
2068
  this._notifyStatus();
1972
2069
  }
1973
2070
  else {
@@ -1977,7 +2074,7 @@ export class Core extends ReadyResource {
1977
2074
  void this._advertiseDeviceProfile(candidate, deviceId);
1978
2075
  void candidate.refreshOurMember().catch(noop);
1979
2076
  void this._settleMembership(candidate, { migrate: true });
1980
- if (invite.memberInvite && !candidate.compute) {
2077
+ if (memberInvite && !candidate.compute) {
1981
2078
  void this._recordJoinedMesh(candidate.key);
1982
2079
  }
1983
2080
  this.emit('mesh-changed', candidate);
@@ -1990,9 +2087,6 @@ export class Core extends ReadyResource {
1990
2087
  }
1991
2088
  const rows = (await this.local.listMeshes()).filter((row) => !b4a.equals(row.key, joined));
1992
2089
  const meshes = rows.filter((row) => !(account && b4a.equals(account, row.key)));
1993
- if (account) {
1994
- await this._writableAccount();
1995
- }
1996
2090
  for (const mesh of meshes) {
1997
2091
  await this._departIfPooled(mesh.key, 'mesh');
1998
2092
  }
@@ -2011,16 +2105,7 @@ export class Core extends ReadyResource {
2011
2105
  return;
2012
2106
  }
2013
2107
  try {
2014
- await Promise.race([
2015
- mesh.waitForWritable(),
2016
- this._closeSignal,
2017
- after(this._accountWritableMs)
2018
- ]);
2019
- if (!mesh.writable) {
2020
- this._log?.warn(['p2p', 'mesh'], `the old ${kind} never went writable, so its writer was not revoked`, { meshId });
2021
- return;
2022
- }
2023
- await this._announceDeparture(mesh);
2108
+ await this._recordDeparture(mesh);
2024
2109
  await this._retirePooledMesh(mesh);
2025
2110
  }
2026
2111
  catch (error) {
@@ -2038,17 +2123,70 @@ export class Core extends ReadyResource {
2038
2123
  async _registerMesh(meshKey, opts) {
2039
2124
  const { shared } = opts;
2040
2125
  const registry = opts.registry ?? (await this._writableAccount());
2041
- if (!registry) {
2042
- throw new Error(MESH_WITHOUT_ACCOUNT);
2043
- }
2044
- const ours = registry.ourMemberId;
2045
- if (!ours) {
2046
- throw new Error(ACCOUNT_MEMBER_UNKNOWN);
2126
+ const ours = registry?.ourMemberId;
2127
+ if (!registry || !ours) {
2128
+ return;
2047
2129
  }
2048
2130
  const now = Date.now();
2049
2131
  await registry.dispatch('put-joined-mesh', {
2050
2132
  room: { meshKey, memberId: ours, joinedAt: now, shared, sharedAt: shared ? now : 0 }
2051
2133
  });
2134
+ await this._backfillMeshSeeds(registry).catch((err) => {
2135
+ this._log?.warn(['p2p', 'mesh'], 'writing the mesh seed failed; writable or settle retries', err);
2136
+ });
2137
+ }
2138
+ async _backfillMeshSeeds(account, { register = true } = {}) {
2139
+ if (!this._isAccount(account))
2140
+ return;
2141
+ const pass = this._backfill.then(() => this._writeMeshSeeds(account, register));
2142
+ this._backfill = pass.catch(noop);
2143
+ await pass;
2144
+ }
2145
+ async _writeMeshSeeds(account, register) {
2146
+ const ours = account.ourMemberId;
2147
+ if (!ours || !this._isAccount(account) || !account.writable) {
2148
+ return;
2149
+ }
2150
+ const batch = account.batch();
2151
+ const registered = register
2152
+ ? await this._registerRecordedMeshes(account, ours, batch)
2153
+ : new Set();
2154
+ for (const { mesh } of this._meshes.values()) {
2155
+ if (await this._seedRowDue(account, mesh, { ours, registered })) {
2156
+ batch.dispatch('put-mesh-seed', { meshKey: mesh.key, seed: mesh.seed });
2157
+ }
2158
+ }
2159
+ await batch.flush();
2160
+ }
2161
+ async _registerRecordedMeshes(account, ours, batch) {
2162
+ const registered = new Set();
2163
+ const now = Date.now();
2164
+ for (const row of await this.local.listMeshes()) {
2165
+ if (b4a.equals(row.key, account.key) || row.compute)
2166
+ continue;
2167
+ const held = await account.view.get(JOINED_MESHES, { meshKey: row.key });
2168
+ if (held) {
2169
+ continue;
2170
+ }
2171
+ batch.dispatch('put-joined-mesh', {
2172
+ room: { meshKey: row.key, memberId: ours, joinedAt: now, shared: false, sharedAt: 0 }
2173
+ });
2174
+ registered.add(b4a.toHex(row.key));
2175
+ }
2176
+ return registered;
2177
+ }
2178
+ async _seedRowDue(account, mesh, { ours, registered }) {
2179
+ if (!mesh.writable || mesh === account || mesh.compute)
2180
+ return false;
2181
+ if (registered.has(this._meshIdOf(mesh))) {
2182
+ return true;
2183
+ }
2184
+ const joined = await account.view.get(JOINED_MESHES, { meshKey: mesh.key });
2185
+ if (!joined?.memberId || !b4a.equals(joined.memberId, ours) || joined.leftAt) {
2186
+ return false;
2187
+ }
2188
+ const held = await account.view.get(MESH_SEEDS, { meshKey: mesh.key });
2189
+ return !held || !b4a.equals(held.seed, mesh.seed);
2052
2190
  }
2053
2191
  async _releaseJoinedMesh(meshKey, registry) {
2054
2192
  try {
@@ -2078,13 +2216,6 @@ export class Core extends ReadyResource {
2078
2216
  const key = await this._storedAccountKey();
2079
2217
  const held = key ? this._pooled(b4a.toHex(key)) : null;
2080
2218
  const mesh = key ? await this._openPooledMesh(b4a.toHex(key)).catch(noop) : null;
2081
- if (mesh && !mesh.writable) {
2082
- await Promise.race([
2083
- mesh.waitForWritable(),
2084
- this._closeSignal,
2085
- after(this._accountWritableMs)
2086
- ]);
2087
- }
2088
2219
  if (mesh && !mesh.ourMemberId) {
2089
2220
  await mesh.refreshOurMember();
2090
2221
  }
@@ -2097,114 +2228,8 @@ export class Core extends ReadyResource {
2097
2228
  }
2098
2229
  return mesh;
2099
2230
  }
2100
- async _awaitGrantRow(mesh, grantKey) {
2101
- await this._untilDeadline(async () => Boolean(await mesh.view.get(MESH_INVITE_GRANTS, { grantKey })));
2102
- }
2103
- async _untilDeadline(done) {
2104
- const deadline = this._joinCancellation?.deadline ?? Date.now();
2105
- for (;;) {
2106
- if (await done())
2107
- return true;
2108
- if (Date.now() >= deadline)
2109
- return false;
2110
- this._throwIfJoinCanceled();
2111
- await after(250);
2112
- }
2113
- }
2114
- async _refuseIfGrantUnusable(candidate, grantSecret, memberId) {
2115
- const grantKey = grantKeyPair(grantSecret).publicKey;
2116
- await this._awaitGrantRow(candidate, grantKey);
2117
- await this._untilDeadline(async () => {
2118
- await candidate.autobee.update();
2119
- const status = await syncStatusSnapshot(candidate);
2120
- return !!status.devices?.length && status.state === 'synced';
2121
- });
2122
- await this._refuseUnredeemable(candidate, grantKey, memberId);
2123
- }
2124
- async _refuseUnredeemable(mesh, grantKey, memberId) {
2125
- const deviceId = this._requireDeviceKeyPair().publicKey;
2126
- const snap = mesh.view.snapshot();
2127
- try {
2128
- const grant = await snap.get(MESH_INVITE_GRANTS, { grantKey });
2129
- if (!grant) {
2130
- return;
2131
- }
2132
- const issuer = grant.issuerDeviceId && (await snap.get(DEVICES, { id: grant.issuerDeviceId }));
2133
- const spent = await snap.get(MESH_INVITE_SPENDS, {
2134
- grantKey,
2135
- deviceId
2136
- });
2137
- const own = await snap.get(DEVICES, { id: deviceId });
2138
- const live = own && !own.revoked;
2139
- const expired = !!grant.expiresAt && Date.now() > grant.expiresAt;
2140
- if (expired && (!spent || grant.revokedAt)) {
2141
- throw new Error(INVITE_GRANT_EXPIRED);
2142
- }
2143
- if (grant.revokedAt || !issuer || issuer.revoked) {
2144
- throw new Error(INVITE_GRANT_REVOKED);
2145
- }
2146
- if (spent && !live) {
2147
- throw new Error(INVITE_GRANT_EXHAUSTED);
2148
- }
2149
- if (spent) {
2150
- return;
2151
- }
2152
- if (grant.maxUses && (grant.uses ?? 0) >= grant.maxUses) {
2153
- throw new Error(INVITE_GRANT_EXHAUSTED);
2154
- }
2155
- if (memberId && (await belongsToAnotherMember(snap, own ?? null, memberId))) {
2156
- throw new Error(DEVICE_OF_ANOTHER_MEMBER);
2157
- }
2158
- if (live) {
2159
- throw new Error(MESH_ALREADY_JOINED);
2160
- }
2161
- const standing = memberId &&
2162
- (await inviteStanding(snap, {
2163
- device: own ?? null,
2164
- memberId,
2165
- grant,
2166
- compute: mesh.compute
2167
- }));
2168
- if (standing === 'present') {
2169
- throw new Error(MEMBER_ALREADY_IN_MESH);
2170
- }
2171
- if (standing === 'removed') {
2172
- throw new Error(MEMBER_REMOVED_SINCE_INVITE);
2173
- }
2174
- }
2175
- finally {
2176
- await snap.close();
2177
- }
2178
- }
2179
- async _refuseFromOwnCopy(invite, grantKey) {
2180
- if (this._pooled(b4a.toHex(invite.baseKey)))
2181
- return;
2182
- const recorded = await this.local.readMeshSession(invite.baseKey);
2183
- const archived = (await this.local.listArchivedMeshes()).find((row) => b4a.equals(row.key, invite.baseKey));
2184
- const writerSeed = recorded?.writerSeed ?? archived?.writerSeed;
2185
- if (!writerSeed) {
2186
- return;
2187
- }
2188
- const copy = new Mesh(this.store, {
2189
- seed: invite.seed,
2190
- key: invite.baseKey,
2191
- deviceId: this._requireDeviceKeyPair().publicKey,
2192
- writerSeed,
2193
- logger: this._log
2194
- });
2195
- try {
2196
- await copy.ready();
2197
- await this._refuseUnredeemable(copy, grantKey, null);
2198
- }
2199
- finally {
2200
- await copy.close();
2201
- }
2202
- }
2203
2231
  async _openCandidateMesh(invite, session) {
2204
2232
  this._throwIfJoinCanceled();
2205
- if (invite.memberInvite && invite.inviteNonce) {
2206
- await this._refuseFromOwnCopy(invite, grantKeyPair(invite.inviteNonce).publicKey);
2207
- }
2208
2233
  const candidate = new Mesh(this.store, {
2209
2234
  seed: session.seed,
2210
2235
  key: session.baseKey ?? null,
@@ -2246,21 +2271,6 @@ export class Core extends ReadyResource {
2246
2271
  candidate.startBlindPeering(this._swarm.dht);
2247
2272
  }
2248
2273
  try {
2249
- this._throwIfJoinCanceled();
2250
- const member = invite.memberInvite
2251
- ? await this._joinBirthMember(candidate, this._requireDeviceKeyPair().publicKey)
2252
- : undefined;
2253
- const invited = !member && invite.memberId && invite.inviteNonce;
2254
- const grantSecret = invite.memberInvite ? invite.inviteNonce : undefined;
2255
- if (grantSecret) {
2256
- await this._refuseIfGrantUnusable(candidate, grantSecret, member?.member.id ?? null);
2257
- }
2258
- await candidate.appendBirthOp(this._requireDeviceKeyPair(), this._localDevice ?? undefined, {
2259
- member,
2260
- grantSecret,
2261
- recovery: invite.recovery,
2262
- vouched: Boolean(invited)
2263
- });
2264
2274
  this._throwIfJoinCanceled();
2265
2275
  if (!b4a.equals(candidate.key, invite.baseKey)) {
2266
2276
  throw new Error('Device is not permitted to join the invited mesh');
@@ -2268,22 +2278,6 @@ export class Core extends ReadyResource {
2268
2278
  if ((await candidate.checkResumedRevocation()) === 'kicked') {
2269
2279
  throw new Error('Device is not permitted to join the invited mesh');
2270
2280
  }
2271
- if (invite.recovery && (await this._isRemovedFrom(candidate))) {
2272
- throw new Error(RECOVERY_DEVICE_REMOVED);
2273
- }
2274
- this._throwIfJoinCanceled();
2275
- disarm();
2276
- const inviter = invited
2277
- ? this._requestInvitedAdmission(candidate, invite.memberId, invite.inviteNonce)
2278
- : null;
2279
- await this._waitForCandidateWritable(candidate, !invite.recovery).catch((error) => {
2280
- throw inviter && !inviter.answered && error.message === GRANT_JOIN_TIMED_OUT
2281
- ? new Error(INVITER_OFFLINE)
2282
- : error;
2283
- });
2284
- if (!candidate.writable) {
2285
- throw new Error('Device is not permitted to join the invited mesh');
2286
- }
2287
2281
  this._throwIfJoinCanceled();
2288
2282
  return candidate;
2289
2283
  }
@@ -2295,104 +2289,43 @@ export class Core extends ReadyResource {
2295
2289
  disarm();
2296
2290
  }
2297
2291
  }
2298
- _requestInvitedAdmission(candidate, memberId, nonce) {
2299
- const { publicKey: deviceId, secretKey } = this._requireDeviceKeyPair();
2300
- const local = this._localDevice;
2301
- if (!local) {
2302
- throw new Error('local device read before the offline boot');
2292
+ async _candidateBirthMember(candidate, invite) {
2293
+ const deviceId = this._requireDeviceKeyPair().publicKey;
2294
+ if (invite.recovery) {
2295
+ return this._recoveryBirthMember(candidate, invite.recovery);
2296
+ }
2297
+ const { deviceSeed, deviceProof, memberId } = invite;
2298
+ if (deviceSeed && deviceProof && memberId) {
2299
+ return this._delegatedBirthMember(candidate, {
2300
+ deviceSeed,
2301
+ deviceProof,
2302
+ memberId,
2303
+ name: invite.inviterName ?? ''
2304
+ });
2303
2305
  }
2304
- const joinedAt = Date.now();
2305
- const request = {
2306
- writerKey: candidate.localWriterKey,
2307
- joinedAt,
2308
- deviceSignature: candidate.signDeviceClaim(secretKey, deviceId, joinedAt),
2309
- memberSignature: signMemberWriterClaim(secretKey, {
2310
- meshPublicKey: candidate.publicKey,
2311
- deviceId,
2312
- writerKey: candidate.localWriterKey,
2313
- joinedAt,
2314
- memberId
2315
- }),
2316
- inviteToken: deviceInviteToken(nonce, deviceId),
2317
- name: local.name,
2318
- capabilityProfile: local.capabilityProfile,
2319
- recommendedModelName: local.recommendedModelName
2320
- };
2321
- const state = { answered: false };
2322
- const asking = () => this._candidateMesh === candidate && !candidate.writable && !this.closing;
2323
- const ask = (inviter) => this._meshSeedHandoff.admit(inviter, candidate.key, request).then((admitted) => {
2324
- state.answered = true;
2325
- return admitted;
2326
- }, (error) => {
2327
- if (![HOST_UNREACHABLE, HOST_SILENT, SEED_REFUSED].includes(error.message)) {
2328
- state.answered = true;
2329
- }
2330
- return false;
2331
- });
2332
- void (async () => {
2333
- while (asking()) {
2334
- for (const inviter of await this._liveSiblings(candidate, memberId)) {
2335
- if (!asking() || (await ask(inviter)))
2336
- return;
2337
- }
2338
- await Promise.race([after(INVITER_RETRY_MS), this._closeSignal]);
2339
- }
2340
- })().catch(noop);
2341
- return state;
2306
+ return invite.memberInvite ? this._joinBirthMember(candidate, deviceId) : undefined;
2342
2307
  }
2343
- async _isRemovedFrom(mesh) {
2344
- const device = await mesh.view
2345
- .get(DEVICES, { id: this._requireDeviceKeyPair().publicKey })
2346
- .catch(noop);
2347
- return device ? isKickedRow(device) : false;
2308
+ async _delegatedBirthMember(mesh, { deviceSeed, deviceProof, memberId, name }) {
2309
+ const keyPair = this._requireDeviceKeyPair();
2310
+ const proof = await attestDeviceProof(keyPair.publicKey, crypto.keyPair(deviceSeed), deviceProof);
2311
+ const attestation = claimAttestation(mesh.publicKey, keyPair.publicKey, mesh.localWriterKey);
2312
+ await this.local.recordMemberProof({ meshKey: mesh.key, memberId, proof, attestedAt: 0 });
2313
+ return {
2314
+ member: { id: memberId, name, joinedAt: Date.now(), updatedAt: 0 },
2315
+ proof: await attestMemberClaim(attestation, keyPair, proof)
2316
+ };
2348
2317
  }
2349
- async _waitForCandidateWritable(candidate, bounded) {
2350
- if (candidate.writable)
2351
- return;
2352
- await new Promise((resolve, reject) => {
2353
- let offClose = noop;
2354
- const cleanup = () => {
2355
- clearTimeout(timer);
2356
- candidate.autobee.off('writable', onWritable);
2357
- candidate.autobee.off('interrupt', onInterrupt);
2358
- candidate.off('kicked', onKicked);
2359
- offClose();
2360
- if (this._cancelCandidateJoin === onCanceled) {
2361
- this._cancelCandidateJoin = null;
2362
- }
2363
- };
2364
- const onWritable = () => {
2365
- cleanup();
2366
- resolve();
2367
- };
2368
- const onKicked = () => {
2369
- cleanup();
2370
- reject(new Error('Device is not permitted to join the invited mesh'));
2371
- };
2372
- const onInterrupt = (reason) => {
2373
- cleanup();
2374
- reject(reason instanceof Error ? reason : new Error('Apply interrupted'));
2375
- };
2376
- const onCanceled = () => {
2377
- cleanup();
2378
- reject(new Error(MESH_JOIN_CANCELED));
2379
- };
2380
- const timer = bounded
2381
- ? setTimeout(() => {
2382
- cleanup();
2383
- reject(new Error(GRANT_JOIN_TIMED_OUT));
2384
- }, this._grantWritableMs)
2385
- : undefined;
2386
- this._cancelCandidateJoin = onCanceled;
2387
- candidate.autobee.on('writable', onWritable);
2388
- candidate.autobee.on('interrupt', onInterrupt);
2389
- candidate.on('kicked', onKicked);
2390
- offClose = this._onClose(onCanceled);
2391
- });
2318
+ _onMeshSeedDue(mesh) {
2319
+ const account = this._accountMesh();
2320
+ if (account) {
2321
+ void this._backfillMeshSeeds(account, { register: mesh === account }).catch(noop);
2322
+ }
2392
2323
  }
2393
2324
  _onMeshWritable(mesh) {
2394
2325
  if (!this._isPooled(mesh))
2395
2326
  return;
2327
+ void this.local.markAwaitingAdmission(mesh.key, { awaitingAdmission: false }).catch(noop);
2328
+ this._onMeshSeedDue(mesh);
2396
2329
  if (this._isAccount(mesh)) {
2397
2330
  this._kicked = false;
2398
2331
  }
@@ -2403,7 +2336,7 @@ export class Core extends ReadyResource {
2403
2336
  return;
2404
2337
  this._log?.error(['p2p', 'mesh'], 'mesh autobee died - retiring it', err);
2405
2338
  if (this._isAccount(mesh)) {
2406
- this._statusError = statusErrorMessage('the local database died', err);
2339
+ this._statusError = statusErrorMessage(MESH_DIED, err);
2407
2340
  }
2408
2341
  this._retirePooledMesh(mesh).catch((retireErr) => {
2409
2342
  this._log?.warn(['p2p', 'mesh'], 'retiring the dead mesh failed', retireErr);
@@ -2427,6 +2360,9 @@ export class Core extends ReadyResource {
2427
2360
  entry.handlers = handlers;
2428
2361
  mesh.on('kicked', () => this._onKicked(mesh));
2429
2362
  mesh.on('unattested-device', () => void this._settleMembership(mesh));
2363
+ if (mesh.writable) {
2364
+ this._onMeshSeedDue(mesh);
2365
+ }
2430
2366
  mesh.on('members-dirty', () => void this._revalidateLoans());
2431
2367
  mesh.on('members-dirty', () => {
2432
2368
  if (!this._isAccount(mesh))
@@ -2439,7 +2375,6 @@ export class Core extends ReadyResource {
2439
2375
  void this._followJoinedMeshes(mesh);
2440
2376
  }
2441
2377
  });
2442
- mesh.on('mesh-kind', () => void this._refuseSealedReader(mesh));
2443
2378
  mesh.on('membership-tallied', () => this._notifyStatus());
2444
2379
  mesh.on('mesh-named', () => this._notifyStatus());
2445
2380
  mesh.on('membership', (membership) => {
@@ -2467,24 +2402,6 @@ export class Core extends ReadyResource {
2467
2402
  this._kicked = false;
2468
2403
  }
2469
2404
  this._notifyStatus();
2470
- void this._refuseSealedReader(mesh);
2471
- }
2472
- async _refuseSealedReader(mesh) {
2473
- if (!this._isAccount(mesh) || mesh.writable)
2474
- return;
2475
- const kind = await mesh.view.get(MESH_KINDS, { id: 'mesh' }).catch(noop);
2476
- if (kind?.kind !== 'account') {
2477
- return;
2478
- }
2479
- const own = await mesh.view.get(DEVICES, {
2480
- id: this._requireDeviceKeyPair().publicKey
2481
- });
2482
- if (own) {
2483
- return;
2484
- }
2485
- this._statusError = SEALED_ACCOUNT_READER;
2486
- this._log?.error(['p2p', 'mesh'], SEALED_ACCOUNT_READER);
2487
- this._notifyStatus();
2488
2405
  }
2489
2406
  _pooled(meshKey) {
2490
2407
  if (meshKey === null)
@@ -2757,6 +2674,13 @@ export class Core extends ReadyResource {
2757
2674
  }
2758
2675
  await candidate.close().catch(noop);
2759
2676
  }
2677
+ async _recordDeparture(mesh) {
2678
+ const pending = !mesh.writable && (mesh.localLength > 0 || mesh.joining);
2679
+ if (pending || (await this._departureHasAudience(mesh))) {
2680
+ await this.local.recordDeparture(mesh.key, { creator: mesh.minted });
2681
+ }
2682
+ await this._announceDeparture(mesh);
2683
+ }
2760
2684
  async _departureHasAudience(mesh) {
2761
2685
  if (!mesh.writable)
2762
2686
  return false;
@@ -2819,10 +2743,7 @@ export class Core extends ReadyResource {
2819
2743
  return;
2820
2744
  }
2821
2745
  }
2822
- if (await this._departureHasAudience(departing)) {
2823
- await this.local.recordDeparture(departing.key, { creator: departing.minted });
2824
- }
2825
- await this._announceDeparture(departing);
2746
+ await this._recordDeparture(departing);
2826
2747
  await this._releaseJoinedMesh(departing.key);
2827
2748
  await this._archiveMembership(departing);
2828
2749
  if (!leavingAccount) {
@@ -2845,13 +2766,7 @@ export class Core extends ReadyResource {
2845
2766
  async _departOnLogout(key) {
2846
2767
  try {
2847
2768
  const mesh = await this._openPooledMesh(b4a.toHex(key));
2848
- if (!(await this.local.awaitsAdmission(key))) {
2849
- await mesh.waitForWritable(this._accountWritableMs);
2850
- }
2851
- if (await this._departureHasAudience(mesh)) {
2852
- await this.local.recordDeparture(mesh.key, { creator: mesh.minted });
2853
- }
2854
- await this._announceDeparture(mesh);
2769
+ await this._recordDeparture(mesh);
2855
2770
  await this._archiveMembership(mesh);
2856
2771
  await this._retirePooledMesh(mesh);
2857
2772
  }
@@ -3119,10 +3034,10 @@ export class Core extends ReadyResource {
3119
3034
  throw new Error('Mesh transition already in progress');
3120
3035
  }
3121
3036
  const registry = opts.registry ?? (await this._writableAccount());
3122
- if (!registry) {
3037
+ if (shared && !registry) {
3123
3038
  throw new Error(MESH_WITHOUT_ACCOUNT);
3124
3039
  }
3125
- if (!registry.ourMemberId) {
3040
+ if (registry && !registry.ourMemberId) {
3126
3041
  throw new Error(ACCOUNT_MEMBER_UNKNOWN);
3127
3042
  }
3128
3043
  const mesh = await this._openMesh({ new: true, sharing: true, compute });
@@ -3133,7 +3048,9 @@ export class Core extends ReadyResource {
3133
3048
  if (!mesh.ourMemberId) {
3134
3049
  throw new Error(MESH_WITHOUT_MEMBER);
3135
3050
  }
3136
- await this._registerMesh(mesh.key, { shared, registry });
3051
+ if (registry) {
3052
+ await this._registerMesh(mesh.key, { shared, registry });
3053
+ }
3137
3054
  }
3138
3055
  catch (error) {
3139
3056
  await this._discardMesh(mesh);
@@ -3225,7 +3142,13 @@ export class Core extends ReadyResource {
3225
3142
  throw halted;
3226
3143
  }
3227
3144
  this._wireMesh(this._poolEntry(mesh));
3145
+ if (this._isAccount(mesh) && this._statusError?.startsWith(MESH_DIED)) {
3146
+ this._statusError = null;
3147
+ }
3228
3148
  this._bringMeshOnline(mesh);
3149
+ if (mesh.writable) {
3150
+ void this.local.markAwaitingAdmission(mesh.key, { awaitingAdmission: false }).catch(noop);
3151
+ }
3229
3152
  this._notifyStatus();
3230
3153
  this._debugOnMeshOpened?.(mesh, meshId);
3231
3154
  return mesh;
@@ -3236,7 +3159,6 @@ export class Core extends ReadyResource {
3236
3159
  const account = !!accountKey && b4a.equals(accountKey, row.key);
3237
3160
  const joined = meshes.get(meshId);
3238
3161
  const state = this._meshEntryState(mesh, account);
3239
- const admission = state === 'joining' ? await this._admission(mesh) : {};
3240
3162
  return {
3241
3163
  meshId,
3242
3164
  state,
@@ -3248,33 +3170,7 @@ export class Core extends ReadyResource {
3248
3170
  members: mesh?.membership.members ?? 0,
3249
3171
  shared: joined?.shared ?? false,
3250
3172
  sharedAt: joined?.shared ? joined.sharedAt : undefined,
3251
- compute: row.compute ?? false,
3252
- reason: admission.reason,
3253
- awaitingFrom: admission.awaitingFrom
3254
- };
3255
- }
3256
- async _admission(mesh) {
3257
- if (!mesh || mesh.writable)
3258
- return {};
3259
- const ours = mesh.localWriterKey;
3260
- const admitters = [];
3261
- try {
3262
- for await (const device of mesh.view.find(DEVICES)) {
3263
- if (device.revoked || (device.writerKey && b4a.equals(device.writerKey, ours))) {
3264
- continue;
3265
- }
3266
- admitters.push(device.name);
3267
- if (admitters.length > 1) {
3268
- break;
3269
- }
3270
- }
3271
- }
3272
- catch {
3273
- return { reason: AWAITING_ADMISSION };
3274
- }
3275
- return {
3276
- reason: AWAITING_ADMISSION,
3277
- awaitingFrom: admitters.length === 1 ? admitters[0] : undefined
3173
+ compute: row.compute ?? false
3278
3174
  };
3279
3175
  }
3280
3176
  _meshEntryState(mesh, account) {
@@ -3507,24 +3403,6 @@ export class Core extends ReadyResource {
3507
3403
  throw new Error(DEVICE_INVITE_MESH_REFUSED);
3508
3404
  }
3509
3405
  }
3510
- let grantSecret;
3511
- if (opts.memberInvite) {
3512
- grantSecret = crypto.randomBytes(32);
3513
- const now = Date.now();
3514
- const terms = { expiresAt: now + GRANT_DEFAULT_TTL_MS, maxUses: GRANT_DEFAULT_MAX_USES };
3515
- const refusal = inviteGrantTermsRefusal(terms, now);
3516
- if (refusal) {
3517
- throw new Error(refusal);
3518
- }
3519
- await mesh.dispatch('put-mesh-invite-grant', {
3520
- grant: {
3521
- grantKey: grantKeyPair(grantSecret).publicKey,
3522
- kind: 'member',
3523
- ...terms,
3524
- updatedAt: now
3525
- }
3526
- });
3527
- }
3528
3406
  const auto = mesh.autobee;
3529
3407
  const info = await auto.system.getInfo();
3530
3408
  const head = info && info.view.length > 0 ? await auto.trusted.mostRecentTrusted(auto.view, null) : null;
@@ -3532,6 +3410,7 @@ export class Core extends ReadyResource {
3532
3410
  ? this._swarm.dht.toArray({ limit: INVITE_DHT_NODES_LIMIT })
3533
3411
  : [];
3534
3412
  const memberId = opts.memberInvite ? undefined : (mesh.ourMemberId ?? undefined);
3413
+ const delegation = memberId ? await this._delegatePairing(mesh) : null;
3535
3414
  const inviterName = memberId
3536
3415
  ? ((await mesh.view.get(MEMBERS, { id: memberId }))?.name ?? undefined)
3537
3416
  : undefined;
@@ -3555,11 +3434,26 @@ export class Core extends ReadyResource {
3555
3434
  : this._resolveBlindPeerKeys().slice(0, INVITE_BLIND_PEERS_LIMIT),
3556
3435
  memberInvite: opts.memberInvite ?? false,
3557
3436
  memberId,
3558
- inviteNonce: grantSecret ?? (memberId ? await this._deviceInvites.issue() : undefined),
3559
3437
  compute: mesh.compute,
3560
- inviterName
3438
+ inviterName,
3439
+ deviceSeed: delegation?.deviceSeed,
3440
+ deviceProof: delegation?.deviceProof
3561
3441
  };
3562
3442
  }
3443
+ async _delegatePairing(account) {
3444
+ const keyPair = this._requireDeviceKeyPair();
3445
+ const own = await account.view.get(DEVICES, { id: keyPair.publicKey });
3446
+ if (!own?.memberProof) {
3447
+ return null;
3448
+ }
3449
+ const deviceSeed = crypto.randomBytes(32);
3450
+ const delegate = crypto.keyPair(deviceSeed);
3451
+ const deviceProof = await attestDeviceProof(delegate.publicKey, keyPair, own.memberProof);
3452
+ if (!(await delegationFits(deviceProof, delegate))) {
3453
+ throw new Error(DEVICE_INVITE_CHAIN_TOO_LONG);
3454
+ }
3455
+ return { deviceSeed, deviceProof };
3456
+ }
3563
3457
  previewMeshInvite(invite) {
3564
3458
  const decoded = decodeMeshInvite(invite);
3565
3459
  if (!decoded) {
@@ -3568,8 +3462,9 @@ export class Core extends ReadyResource {
3568
3462
  return { memberInvite: decoded.memberInvite ?? false, inviterName: decoded.inviterName };
3569
3463
  }
3570
3464
  async _closeSwarm() {
3571
- this._wakeup.destroy();
3572
- this._meshSeedHandoff.destroy();
3465
+ if (this._ownsWakeup) {
3466
+ this._wakeup.destroy();
3467
+ }
3573
3468
  this._harnessLoan.destroy();
3574
3469
  this._secretHandoff.destroy();
3575
3470
  this._skillOverlayHandoff.destroy();
@@ -3579,6 +3474,15 @@ export class Core extends ReadyResource {
3579
3474
  }
3580
3475
  const swarm = this._swarm;
3581
3476
  this._swarm = null;
3477
+ swarm.off('connection', this._onSwarmConnection);
3478
+ if (!this._ownsSwarm) {
3479
+ for (const detach of this._peers.values()) {
3480
+ detach();
3481
+ }
3482
+ this._peers.clear();
3483
+ await Promise.all([...this._meshes.values()].map(({ mesh }) => swarm.leave(mesh.discoveryKey)));
3484
+ return;
3485
+ }
3582
3486
  for (const conn of swarm.connections) {
3583
3487
  conn.destroy?.();
3584
3488
  }
@@ -3674,17 +3578,16 @@ export class Core extends ReadyResource {
3674
3578
  return;
3675
3579
  }
3676
3580
  if (held) {
3677
- await this._requestMeshAdmission(memberMesh, held, ours);
3678
3581
  return;
3679
3582
  }
3680
3583
  if (await this.local.readMeshSession(joined.meshKey)) {
3681
3584
  if (this._upgradeRequired !== 'halt' &&
3682
3585
  (await this.local.awaitsAdmission(joined.meshKey))) {
3683
- await this._requestMeshAdmission(memberMesh, await this._openPooledMesh(meshId), ours);
3586
+ await this._openPooledMesh(meshId);
3684
3587
  }
3685
3588
  return;
3686
3589
  }
3687
- await this._openFollowedMesh(memberMesh, joined.meshKey, ours);
3590
+ await this._openFollowedMesh(memberMesh, joined);
3688
3591
  });
3689
3592
  if (failed && !joined.leftAt) {
3690
3593
  pending = true;
@@ -3713,10 +3616,7 @@ export class Core extends ReadyResource {
3713
3616
  if (this._leaving || this._minting)
3714
3617
  return;
3715
3618
  try {
3716
- if (await this._departureHasAudience(mesh)) {
3717
- await this.local.recordDeparture(mesh.key, { creator: mesh.minted });
3718
- }
3719
- await this._announceDeparture(mesh);
3619
+ await this._recordDeparture(mesh);
3720
3620
  if (this._isAccount(mesh)) {
3721
3621
  await this._replaceRevokedAccount(mesh);
3722
3622
  }
@@ -3735,226 +3635,26 @@ export class Core extends ReadyResource {
3735
3635
  await this.local.archiveMesh(meshKey);
3736
3636
  this._notifyStatus();
3737
3637
  }
3738
- async _openOwnMesh(invite) {
3739
- const meshKey = invite.baseKey;
3740
- const meshId = b4a.toHex(meshKey);
3741
- if (!invite.inviteNonce) {
3742
- return false;
3743
- }
3744
- const grantKey = grantKeyPair(invite.inviteNonce).publicKey;
3745
- if (await this.local.readMeshSession(meshKey)) {
3746
- return this._adoptOwnMesh(await this._openPooledMesh(meshId), grantKey);
3747
- }
3748
- const registry = await this._accountMesh();
3749
- const ours = registry?.ourMemberId;
3750
- if (!registry || !ours || !(await this._isOwnMeshIn(registry, meshKey))) {
3751
- return false;
3752
- }
3753
- if (this._blockedByMinVersion()) {
3754
- throw new Error(MESH_BELOW_FLOOR);
3755
- }
3756
- this._throwIfJoinCanceled();
3757
- await this._openFollowedMesh(registry, meshKey, ours).catch((error) => {
3758
- this._log?.warn(['p2p', 'mesh'], 'opening a mesh of ours through a sibling failed', error);
3759
- });
3760
- if (this.closing) {
3761
- throw new Error('Core is closing');
3762
- }
3763
- this._throwIfJoinCanceled();
3764
- const mesh = this._pooled(meshId);
3765
- return mesh ? this._adoptOwnMesh(mesh, grantKey) : false;
3766
- }
3767
- async _adoptOwnMesh(mesh, grantKey) {
3768
- const id = this._requireDeviceKeyPair().publicKey;
3769
- const ours = this._accountMesh()?.ourMemberId ?? null;
3770
- await this._awaitGrantRow(mesh, grantKey);
3771
- let refused = false;
3772
- const settled = await this._untilDeadline(async () => {
3773
- const own = await mesh.view.get(DEVICES, { id });
3774
- if (own && !own.revoked && own.writerKey && b4a.equals(own.writerKey, mesh.localWriterKey)) {
3775
- return true;
3776
- }
3777
- refused = await this._refusedInMesh(mesh, grantKey, ours);
3778
- return refused;
3779
- });
3780
- if (refused) {
3781
- const own = await mesh.view.get(DEVICES, { id });
3782
- await this._refuseUnredeemable(mesh, grantKey, own?.memberId ?? mesh.ourMemberId);
3783
- await this._retirePooledMesh(mesh);
3784
- await this.local.archiveMesh(mesh.key);
3785
- return false;
3786
- }
3787
- if (!settled) {
3788
- throw new Error(MESH_ADMISSION_PENDING);
3789
- }
3790
- return true;
3791
- }
3792
- async _refusedInMesh(mesh, grantKey, ours) {
3793
- await mesh.autobee.update();
3794
- const status = await syncStatusSnapshot(mesh);
3795
- if (!status.devices?.length || status.state !== 'synced') {
3796
- return false;
3797
- }
3798
- const self = this._requireDeviceKeyPair().publicKey;
3799
- const own = await mesh.view.get(DEVICES, { id: self });
3800
- if (own?.revoked &&
3801
- !(own.revokedBy && b4a.equals(own.revokedBy, own.writerKey ?? b4a.alloc(0)))) {
3802
- return true;
3803
- }
3804
- if (!ours) {
3805
- return false;
3806
- }
3807
- const grant = await mesh.view.get(MESH_INVITE_GRANTS, { grantKey });
3808
- if (grant && (await memberStanding(mesh.view, ours, grant)) === 'removed') {
3809
- return true;
3810
- }
3811
- for await (const device of mesh.view.find(DEVICES)) {
3812
- if (!device.revoked &&
3813
- device.memberId &&
3814
- b4a.equals(device.memberId, ours) &&
3815
- !b4a.equals(device.id, self)) {
3816
- return false;
3817
- }
3818
- }
3819
- return true;
3820
- }
3821
- async _openFollowedMesh(memberMesh, meshKey, ours) {
3822
- if (this._blockedByMinVersion())
3823
- return;
3824
- const seed = await this._requestMeshSeed(memberMesh, meshKey, ours);
3825
- if (!seed || this.closing) {
3638
+ async _openFollowedMesh(memberMesh, joined) {
3639
+ if (this._blockedByMinVersion() || this._upgradeRequired || !this._mayFollow(memberMesh)) {
3826
3640
  return;
3827
3641
  }
3828
- const moving = this._joinCancellation ? this._joinCancellation.account : this._minting;
3829
- const leaving = this._leaving === this._meshIdOf(memberMesh);
3830
- if (moving || leaving || !this._isAccount(memberMesh)) {
3642
+ const { meshKey } = joined;
3643
+ const held = await memberMesh.view.get(MESH_SEEDS, { meshKey });
3644
+ if (!held || this.closing) {
3831
3645
  return;
3832
3646
  }
3833
- const session = this.local.createCandidateSession(seed, meshKey);
3647
+ const session = this.local.createCandidateSession(held.seed, meshKey);
3834
3648
  await this.local.recordMesh(meshKey, session, {
3835
3649
  deviceId: this._requireDeviceKeyPair().publicKey
3836
3650
  });
3837
3651
  await this.local.markAwaitingAdmission(meshKey, { awaitingAdmission: true });
3838
- let mesh;
3839
- try {
3840
- mesh = await this._openPooledMesh(b4a.toHex(meshKey));
3841
- }
3842
- catch (error) {
3843
- if (this._upgradeRequired !== 'halt') {
3844
- await this.local.archiveMesh(meshKey).catch(noop);
3845
- }
3846
- throw error;
3847
- }
3848
- await this._requestMeshAdmission(memberMesh, mesh, ours);
3849
- }
3850
- async _requestMeshSeed(memberMesh, meshKey, ours) {
3851
- let unanswered = null;
3852
- for (const sibling of await this._liveSiblings(memberMesh, ours)) {
3853
- if (this.closing)
3854
- return null;
3855
- const seed = await this._meshSeedHandoff.request(sibling, meshKey).catch((error) => {
3856
- this._log?.warn(['p2p', 'mesh'], 'a sibling did not hand over a mesh seed', error);
3857
- if (UNANSWERED_SEED_REQUESTS.has(error.message)) {
3858
- unanswered = error;
3859
- }
3860
- });
3861
- if (seed) {
3862
- return seed;
3863
- }
3864
- }
3865
- if (unanswered) {
3866
- throw unanswered;
3867
- }
3868
- return null;
3869
- }
3870
- async _requestMeshAdmission(memberMesh, mesh, ours) {
3871
- if (mesh.writable) {
3872
- return this.local.markAwaitingAdmission(mesh.key, { awaitingAdmission: false });
3873
- }
3874
- const { publicKey: deviceId, secretKey } = this._requireDeviceKeyPair();
3875
- const own = await mesh.view.get(DEVICES, { id: deviceId });
3876
- if (own && (await this._removedFor(mesh, own))) {
3877
- return this.local.markAwaitingAdmission(mesh.key, { awaitingAdmission: false });
3878
- }
3879
- const binding = {
3880
- meshPublicKey: mesh.publicKey,
3881
- deviceId,
3882
- writerKey: mesh.localWriterKey,
3883
- joinedAt: Date.now()
3884
- };
3885
- const claim = {
3886
- writerKey: binding.writerKey,
3887
- joinedAt: binding.joinedAt,
3888
- deviceSignature: signWriterClaim(secretKey, binding),
3889
- memberSignature: signMemberWriterClaim(secretKey, { ...binding, memberId: ours })
3890
- };
3891
- const known = await mesh.bornKind();
3892
- const deferred = known !== null && (await this._askSiblingsToAdmit(memberMesh, mesh, { ours, claim }));
3893
- if (this.closing) {
3894
- return;
3895
- }
3896
- if (!mesh.writable && (await mesh.waitForGenesis(this._accountWritableMs)) === null) {
3897
- await mesh.appendBirthOp(this._requireDeviceKeyPair(), this._localDevice ?? undefined);
3898
- await mesh.waitForWritable(this._accountWritableMs);
3899
- }
3900
- const meshId = this._meshIdOf(mesh);
3901
- if (deferred && !mesh.writable) {
3902
- this._retryAdmission(memberMesh, meshId);
3903
- return;
3904
- }
3905
- this._admissionRetries.delete(meshId);
3906
- if (mesh.writable) {
3907
- await this.local.markAwaitingAdmission(mesh.key, { awaitingAdmission: false });
3908
- }
3909
- }
3910
- async _askSiblingsToAdmit(memberMesh, mesh, { ours, claim }) {
3911
- let deferred = false;
3912
- for (const sibling of await this._liveSiblings(memberMesh, ours)) {
3913
- if (this.closing || mesh.writable)
3914
- break;
3915
- const admitted = await this._meshSeedHandoff
3916
- .admit(sibling, mesh.key, claim)
3917
- .catch((error) => {
3918
- deferred ||= error.message === ADMISSION_DEFERRED;
3919
- return false;
3920
- });
3921
- if (admitted) {
3922
- await mesh.waitForWritable(this._accountWritableMs);
3923
- }
3924
- }
3925
- return deferred && !mesh.writable;
3652
+ await this._openPooledMesh(b4a.toHex(meshKey));
3926
3653
  }
3927
- _retryAdmission(memberMesh, meshId) {
3928
- const tries = this._admissionRetries.get(meshId) ?? 0;
3929
- if (tries >= ADMISSION_RETRIES) {
3930
- return;
3931
- }
3932
- this._admissionRetries.set(meshId, tries + 1);
3933
- const memberId = this._meshIdOf(memberMesh);
3934
- if (this._admissionRetryPending.has(memberId)) {
3935
- return;
3936
- }
3937
- this._admissionRetryPending.add(memberId);
3938
- Promise.race([after(INVITER_RETRY_MS), this._closeSignal])
3939
- .then(() => {
3940
- this._admissionRetryPending.delete(memberId);
3941
- const current = this.closing ? null : this._pooled(memberId);
3942
- return current ? this._followJoinedMeshes(current) : undefined;
3943
- })
3944
- .catch(noop);
3945
- }
3946
- async _liveSiblings(memberMesh, ours) {
3947
- const self = this._deviceKeyPair?.publicKey;
3948
- const siblings = [];
3949
- for await (const device of memberMesh.view.find(DEVICES)) {
3950
- if (device.revoked || !(device.memberId && b4a.equals(device.memberId, ours))) {
3951
- continue;
3952
- }
3953
- if (self && b4a.equals(device.id, self))
3954
- continue;
3955
- siblings.push(device.id);
3956
- }
3957
- return siblings;
3654
+ _mayFollow(memberMesh) {
3655
+ const moving = this._joinCancellation ? this._joinCancellation.account : this._minting;
3656
+ const leaving = this._leaving === this._meshIdOf(memberMesh);
3657
+ return !this.closing && !moving && !leaving && this._isAccount(memberMesh);
3958
3658
  }
3959
3659
  registerLoanableHarness(serve) {
3960
3660
  this._serveLoanedHarness = serve;
@@ -4012,193 +3712,6 @@ export class Core extends ReadyResource {
4012
3712
  }
4013
3713
  return false;
4014
3714
  }
4015
- async _serveMeshSeed(request, fromDeviceId) {
4016
- const asker = b4a.from(fromDeviceId);
4017
- if ('writerKey' in request) {
4018
- return request.inviteToken
4019
- ? this._admitInvitedDevice(request, asker)
4020
- : this._admitIntoMesh(request, asker);
4021
- }
4022
- const standing = await this._askerStanding(asker);
4023
- if (standing === 'unseen') {
4024
- throw new Error(SEED_ASKER_UNSEEN);
4025
- }
4026
- if (standing === 'refused') {
4027
- this._log?.warn(['p2p', 'mesh'], 'seed request refused: not a device of this member');
4028
- return null;
4029
- }
4030
- if (!(await this._isJoinedMeshKey(request.meshKey))) {
4031
- this._log?.warn(['p2p', 'mesh'], 'seed request refused: not a mesh this member joined');
4032
- return null;
4033
- }
4034
- const mesh = this._pooled(b4a.toHex(request.meshKey));
4035
- const row = mesh ? await mesh.view.get(DEVICES, { id: asker }) : null;
4036
- if (mesh && row && (await this._removedFor(mesh, row))) {
4037
- this._log?.warn(['p2p', 'mesh'], 'seed request refused: removed from this mesh');
4038
- return null;
4039
- }
4040
- const session = await this.local.readMeshSession(request.meshKey);
4041
- return session?.seed ? { seed: session.seed } : null;
4042
- }
4043
- async _admitInvitedDevice(request, asker) {
4044
- const account = await this._accountMesh();
4045
- if (!(account?.key && b4a.equals(account?.key, request.meshKey))) {
4046
- this._log?.warn(['p2p', 'mesh'], "device invite refused: not this device's account");
4047
- return null;
4048
- }
4049
- const context = { deviceKeyPair: this._requireDeviceKeyPair(), log: this._log };
4050
- const admitted = await admitInvitedDevice(account, context, {
4051
- invites: this._deviceInvites,
4052
- deviceId: asker,
4053
- request
4054
- });
4055
- if (admitted) {
4056
- return { admitted: true };
4057
- }
4058
- if (request.inviteToken && (await this._deviceInvites.spent(request.inviteToken, asker))) {
4059
- throw new Error(DEVICE_INVITE_SPENT);
4060
- }
4061
- return null;
4062
- }
4063
- async _admitIntoMesh(request, asker) {
4064
- if (this._meshTransitionLock.locked) {
4065
- throw new Error(ADMISSION_DEFERRED);
4066
- }
4067
- await this._meshTransitionLock.lock();
4068
- try {
4069
- return await this._admitIntoMeshLocked(request, asker);
4070
- }
4071
- finally {
4072
- this._meshTransitionLock.unlock();
4073
- }
4074
- }
4075
- async _admitIntoMeshLocked(request, asker) {
4076
- if (this._leaving)
4077
- return null;
4078
- if (this._minting) {
4079
- throw new Error(ADMISSION_DEFERRED);
4080
- }
4081
- const account = await this._accountMesh();
4082
- const ours = account?.ourMemberId;
4083
- const row = ours ? await account.view.get(DEVICES, { id: asker }) : null;
4084
- if (!ours ||
4085
- !row ||
4086
- row.revoked ||
4087
- !(row.memberId && b4a.equals(row.memberId, ours)) ||
4088
- (await this._askerStanding(asker)) !== 'ours') {
4089
- this._log?.warn(['p2p', 'mesh'], 'admission refused: not a live device of this member');
4090
- return null;
4091
- }
4092
- if (!(await this._isJoinedMeshKey(request.meshKey))) {
4093
- this._log?.warn(['p2p', 'mesh'], 'admission refused: not a mesh this member joined');
4094
- return null;
4095
- }
4096
- const session = await this.local.readMeshSession(request.meshKey);
4097
- if (!session?.seed) {
4098
- return null;
4099
- }
4100
- const writerKey = request.writerKey;
4101
- const joinedAt = request.joinedAt ?? 0;
4102
- const claim = {
4103
- meshPublicKey: meshPublicKeyFromSeed(session.seed),
4104
- deviceId: asker,
4105
- writerKey,
4106
- joinedAt
4107
- };
4108
- if (!verifyWriterClaim(request.deviceSignature, claim) ||
4109
- !verifyMemberWriterClaim(request.memberSignature, { ...claim, memberId: ours })) {
4110
- this._log?.warn(['p2p', 'mesh'], 'admission refused: the asker did not sign its claim');
4111
- return null;
4112
- }
4113
- const mesh = await this._openPooledMesh(b4a.toHex(request.meshKey));
4114
- await mesh.autobee.update();
4115
- if (!mesh.writable) {
4116
- throw new Error(ADMISSION_DEFERRED);
4117
- }
4118
- if ((await mesh.bornKind()) !== 'shared') {
4119
- this._log?.warn(['p2p', 'mesh'], 'admission refused: a sibling joins this mesh by its seed');
4120
- return null;
4121
- }
4122
- const self = await mesh.view.get(DEVICES, {
4123
- id: this._requireDeviceKeyPair().publicKey
4124
- });
4125
- if (!(self?.memberId && b4a.equals(self.memberId, ours)) ||
4126
- self.revoked ||
4127
- !(self.writerKey && b4a.equals(self.writerKey, mesh.localWriterKey))) {
4128
- this._log?.warn(['p2p', 'mesh'], 'admission refused: this device is not linked in the mesh');
4129
- return null;
4130
- }
4131
- const current = await mesh.view.get(DEVICES, { id: asker });
4132
- if (current && (await this._removedFor(mesh, current))) {
4133
- this._log?.warn(['p2p', 'mesh'], 'admission refused: removed from this mesh');
4134
- return null;
4135
- }
4136
- if (current && !current.revoked) {
4137
- if (current.writerKey && b4a.equals(current.writerKey, writerKey)) {
4138
- return { admitted: true };
4139
- }
4140
- if (!(row.joinedAt > current.joinedAt)) {
4141
- throw new Error(ADMISSION_DEFERRED);
4142
- }
4143
- }
4144
- await mesh.dispatch('vouched-join', {
4145
- signature: mesh.sign(writerKey),
4146
- deviceId: asker,
4147
- device: claimableDevice({
4148
- id: asker,
4149
- joinedAt,
4150
- name: row.name,
4151
- capabilityProfile: row.capabilityProfile,
4152
- recommendedModelName: row.recommendedModelName
4153
- }),
4154
- deviceSignature: request.deviceSignature,
4155
- writerKey,
4156
- memberProof: row.memberProof,
4157
- memberSignature: request.memberSignature
4158
- });
4159
- return { admitted: true };
4160
- }
4161
- async _isJoinedMeshKey(meshKey) {
4162
- for (const entry of this._meshes.values()) {
4163
- const ours = entry.mesh.ourMemberId;
4164
- if (!ours) {
4165
- continue;
4166
- }
4167
- const row = await entry.mesh.view.get(JOINED_MESHES, { meshKey }).catch(noop);
4168
- if (row?.memberId && b4a.equals(row.memberId, ours) && !row.leftAt) {
4169
- return true;
4170
- }
4171
- }
4172
- return false;
4173
- }
4174
- async _removedFor(mesh, row) {
4175
- if (!row.revoked)
4176
- return false;
4177
- const by = await removedBy(mesh.view, row);
4178
- if (by !== 'own-member') {
4179
- return by === 'another-member';
4180
- }
4181
- const paired = await this._accountMesh()?.view.get(DEVICES, { id: row.id });
4182
- return !(paired && !paired.revoked && row.revokedAt && paired.joinedAt > row.revokedAt);
4183
- }
4184
- async _askerStanding(deviceId) {
4185
- const account = this._accountMesh();
4186
- const ours = account?.ourMemberId;
4187
- if (!account || !ours) {
4188
- return 'unseen';
4189
- }
4190
- const device = await account.view.get(DEVICES, { id: deviceId });
4191
- if (!device) {
4192
- return 'unseen';
4193
- }
4194
- if (device.revoked) {
4195
- return 'refused';
4196
- }
4197
- if (!device.memberId) {
4198
- return 'unseen';
4199
- }
4200
- return b4a.equals(device.memberId, ours) ? 'ours' : 'refused';
4201
- }
4202
3715
  async _skillOverlayOp(agentId, op) {
4203
3716
  const host = await this._resolveAgentHost(agentId);
4204
3717
  if (!host) {
@@ -4371,47 +3884,61 @@ export class Core extends ReadyResource {
4371
3884
  conn.on('error', noop);
4372
3885
  return;
4373
3886
  }
4374
- this._trackPeerConnection(conn);
4375
3887
  const remoteId = b4a.toHex(conn.remotePublicKey).slice(0, 8);
4376
3888
  const start = Date.now();
4377
3889
  let bytes = 0;
4378
3890
  let connErr = null;
4379
3891
  this._log?.info(['p2p', 'peer'], `got ${conn.isInitiator ? 'client' : 'server'} connection to ${remoteId}`);
4380
- conn.once('error', (err) => {
3892
+ const onerror = (err) => {
4381
3893
  connErr = err;
4382
- });
4383
- conn.once('close', () => {
3894
+ };
3895
+ const onclose = () => {
4384
3896
  this._log?.info(['p2p', 'peer'], `connection closed to ${remoteId}, with err: ${connErr} (${Date.now() - start} ms ${bytes} bytes recv)`);
4385
- });
4386
- conn.on('data', (data) => {
3897
+ };
3898
+ const ondata = (data) => {
4387
3899
  bytes += data.byteLength;
3900
+ };
3901
+ conn.once('error', onerror);
3902
+ conn.once('close', onclose);
3903
+ conn.on('data', ondata);
3904
+ this._trackPeerConnection(conn, () => {
3905
+ conn.off('error', onerror);
3906
+ conn.off('close', onclose);
3907
+ conn.off('data', ondata);
4388
3908
  });
4389
- this._wakeup.addStream(conn);
4390
3909
  this._secretHandoff.addStream(conn);
4391
3910
  this._skillOverlayHandoff.addStream(conn);
4392
- this._meshSeedHandoff.addStream(conn);
4393
3911
  this._harnessLoan.addStream(conn);
4394
3912
  this._inferenceCallsHandoff.addStream(conn);
3913
+ if (this._ownsSwarm) {
3914
+ this._wakeup.addStream(conn);
3915
+ this.rootCorestore.replicate(conn);
3916
+ }
3917
+ if (this.suspended) {
3918
+ this._peerArrivedSuspended = true;
3919
+ return;
3920
+ }
3921
+ this._retryOnPeer();
3922
+ }
3923
+ _retryOnPeer() {
4395
3924
  for (const entry of this._meshes.values()) {
4396
3925
  entry.hostedAgents?.kick();
4397
3926
  void this._followJoinedMeshes(entry.mesh);
4398
- }
4399
- this.rootCorestore.replicate(conn);
4400
- for (const entry of this._meshes.values()) {
4401
3927
  entry.mesh.files.reconnect();
4402
3928
  }
4403
3929
  }
4404
- _trackPeerConnection(conn) {
4405
- this._peers.add(conn);
4406
- this._onPeerChange();
4407
- let removed = false;
3930
+ _trackPeerConnection(conn, detach) {
4408
3931
  const remove = () => {
4409
- if (removed)
4410
- return;
4411
- removed = true;
4412
- this._peers.delete(conn);
4413
- this._onPeerChange();
3932
+ if (this._peers.delete(conn)) {
3933
+ this._onPeerChange();
3934
+ }
4414
3935
  };
3936
+ this._peers.set(conn, () => {
3937
+ detach();
3938
+ conn.off('close', remove);
3939
+ conn.off('error', remove);
3940
+ });
3941
+ this._onPeerChange();
4415
3942
  conn.once('close', remove);
4416
3943
  conn.once('error', remove);
4417
3944
  }
@@ -4564,6 +4091,14 @@ export class Core extends ReadyResource {
4564
4091
  }
4565
4092
  };
4566
4093
  }
4094
+ registerModelMemory(hook) {
4095
+ this._modelMemory = hook;
4096
+ return () => {
4097
+ if (this._modelMemory === hook) {
4098
+ this._modelMemory = null;
4099
+ }
4100
+ };
4101
+ }
4567
4102
  registerDeleteModel(hook) {
4568
4103
  this._deleteModel = hook;
4569
4104
  return () => {
@@ -4630,6 +4165,13 @@ export class Core extends ReadyResource {
4630
4165
  }
4631
4166
  return null;
4632
4167
  }
4168
+ async _readModelList(request) {
4169
+ const models = await this._readModels(request);
4170
+ if (!models) {
4171
+ return null;
4172
+ }
4173
+ return { models, memory: (await this._modelMemory?.()) ?? undefined };
4174
+ }
4633
4175
  async _readModels({ excludeCustom, ...options }) {
4634
4176
  const list = await this._listModels?.(options);
4635
4177
  if (!list) {
@@ -4939,9 +4481,6 @@ export class Core extends ReadyResource {
4939
4481
  }
4940
4482
  }
4941
4483
  }
4942
- function after(ms) {
4943
- return new Promise((resolve) => unref(setTimeout(resolve, ms)));
4944
- }
4945
4484
  function unmarkedNotice(losers) {
4946
4485
  if (losers.length === 0)
4947
4486
  return undefined;
@@ -4949,6 +4488,9 @@ function unmarkedNotice(losers) {
4949
4488
  return `another device of yours opened a shared mesh at the same moment, so ${ids} is an ordinary mesh now`;
4950
4489
  }
4951
4490
  function noop() { }
4491
+ function isPeerConnection(conn) {
4492
+ return 'remotePublicKey' in conn;
4493
+ }
4952
4494
  async function closeConfigReader(reader) {
4953
4495
  if (!reader)
4954
4496
  return;