@qvac/core 0.6.7 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (146) hide show
  1. package/CHANGELOG.md +211 -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/api/chat.d.ts +1 -1
  6. package/dist-lib/lib/api/chat.js +9 -0
  7. package/dist-lib/lib/api/index.d.ts +2 -1
  8. package/dist-lib/lib/api/index.js +2 -1
  9. package/dist-lib/lib/api/network.d.ts +8 -1
  10. package/dist-lib/lib/api/network.js +6 -0
  11. package/dist-lib/lib/assistant/fake-models.d.ts +22 -0
  12. package/dist-lib/lib/assistant/fake-models.js +112 -0
  13. package/dist-lib/lib/assistant/harness.d.ts +12 -0
  14. package/dist-lib/lib/assistant/harness.js +26 -0
  15. package/dist-lib/lib/assistant/history.d.ts +5 -1
  16. package/dist-lib/lib/assistant/history.js +14 -2
  17. package/dist-lib/lib/assistant-tree.d.ts +4 -0
  18. package/dist-lib/lib/assistant-tree.js +45 -7
  19. package/dist-lib/lib/assistant.d.ts +15 -2
  20. package/dist-lib/lib/assistant.js +181 -42
  21. package/dist-lib/lib/constants.d.ts +6 -0
  22. package/dist-lib/lib/constants.js +6 -0
  23. package/dist-lib/lib/core/agent.d.ts +2 -0
  24. package/dist-lib/lib/core/agent.js +16 -5
  25. package/dist-lib/lib/core/blind-peers.d.ts +4 -0
  26. package/dist-lib/lib/core/blind-peers.js +1 -0
  27. package/dist-lib/lib/core/chat-activity.d.ts +1 -1
  28. package/dist-lib/lib/core/chat-activity.js +3 -0
  29. package/dist-lib/lib/core/chat-copy.d.ts +4 -0
  30. package/dist-lib/lib/core/chat-copy.js +1 -1
  31. package/dist-lib/lib/core/chat-fork.d.ts +21 -0
  32. package/dist-lib/lib/core/chat-fork.js +245 -0
  33. package/dist-lib/lib/core/chat-move.js +1 -4
  34. package/dist-lib/lib/core/chat.d.ts +1 -0
  35. package/dist-lib/lib/core/chat.js +1 -1
  36. package/dist-lib/lib/core/collections.d.ts +1 -0
  37. package/dist-lib/lib/core/collections.js +1 -0
  38. package/dist-lib/lib/core/contract/compute.d.ts +1 -1
  39. package/dist-lib/lib/core/contract/compute.js +2 -1
  40. package/dist-lib/lib/core/contract/index.js +6 -1
  41. package/dist-lib/lib/core/contract/invites.d.ts +1 -11
  42. package/dist-lib/lib/core/contract/invites.js +3 -3
  43. package/dist-lib/lib/core/contract/mesh-seeds.d.ts +2 -0
  44. package/dist-lib/lib/core/contract/mesh-seeds.js +26 -0
  45. package/dist-lib/lib/core/contract/open-join.d.ts +2 -0
  46. package/dist-lib/lib/core/contract/open-join.js +57 -0
  47. package/dist-lib/lib/core/departures.d.ts +8 -1
  48. package/dist-lib/lib/core/departures.js +59 -2
  49. package/dist-lib/lib/core/fs-drives.d.ts +8 -1
  50. package/dist-lib/lib/core/fs-drives.js +47 -15
  51. package/dist-lib/lib/core/harness-loan.d.ts +4 -20
  52. package/dist-lib/lib/core/harness-loan.js +7 -184
  53. package/dist-lib/lib/core/id.d.ts +1 -0
  54. package/dist-lib/lib/core/id.js +3 -0
  55. package/dist-lib/lib/core/identity.d.ts +1 -1
  56. package/dist-lib/lib/core/identity.js +6 -4
  57. package/dist-lib/lib/core/invite.d.ts +2 -0
  58. package/dist-lib/lib/core/invite.js +6 -2
  59. package/dist-lib/lib/core/membership.d.ts +1 -12
  60. package/dist-lib/lib/core/membership.js +1 -94
  61. package/dist-lib/lib/core/mesh.d.ts +11 -15
  62. package/dist-lib/lib/core/mesh.js +52 -88
  63. package/dist-lib/lib/core/migrations/agent-tools-scope.d.ts +2 -0
  64. package/dist-lib/lib/core/migrations/agent-tools-scope.js +24 -0
  65. package/dist-lib/lib/core/migrations/index.js +3 -1
  66. package/dist-lib/lib/core/peer-duplex.d.ts +32 -0
  67. package/dist-lib/lib/core/peer-duplex.js +207 -0
  68. package/dist-lib/lib/core/sync/index.js +2 -16
  69. package/dist-lib/lib/core.d.ts +47 -50
  70. package/dist-lib/lib/core.js +524 -878
  71. package/dist-lib/lib/diagnostics.d.ts +7 -0
  72. package/dist-lib/lib/diagnostics.js +22 -0
  73. package/dist-lib/lib/doctor/checks.d.ts +15 -0
  74. package/dist-lib/lib/doctor/checks.js +272 -0
  75. package/dist-lib/lib/doctor/redact.d.ts +4 -0
  76. package/dist-lib/lib/doctor/redact.js +31 -0
  77. package/dist-lib/lib/doctor/render.d.ts +8 -0
  78. package/dist-lib/lib/doctor/render.js +83 -0
  79. package/dist-lib/lib/doctor/tree.d.ts +5 -0
  80. package/dist-lib/lib/doctor/tree.js +167 -0
  81. package/dist-lib/lib/downloads-allowed-pusher.d.ts +10 -0
  82. package/dist-lib/lib/downloads-allowed-pusher.js +37 -0
  83. package/dist-lib/lib/guards.d.ts +1 -0
  84. package/dist-lib/lib/guards.js +4 -0
  85. package/dist-lib/lib/inference-calls.js +1 -5
  86. package/dist-lib/lib/kv-cache-reclaim.d.ts +2 -1
  87. package/dist-lib/lib/kv-cache-reclaim.js +24 -0
  88. package/dist-lib/lib/mesh-scoped-engine.js +4 -0
  89. package/dist-lib/lib/model-download-status.d.ts +1 -1
  90. package/dist-lib/lib/model-download.d.ts +9 -10
  91. package/dist-lib/lib/model-download.js +49 -16
  92. package/dist-lib/lib/model-memory-cap.d.ts +29 -0
  93. package/dist-lib/lib/model-memory-cap.js +69 -0
  94. package/dist-lib/lib/models-watch.d.ts +1 -1
  95. package/dist-lib/lib/models-watch.js +4 -4
  96. package/dist-lib/lib/oauth/flow.js +1 -1
  97. package/dist-lib/lib/oauth/loopback.js +16 -2
  98. package/dist-lib/lib/oauth/redirect-types.d.ts +1 -0
  99. package/dist-lib/lib/run-replay.d.ts +26 -0
  100. package/dist-lib/lib/run-replay.js +107 -0
  101. package/dist-lib/schema/config.d.ts +1 -1
  102. package/dist-lib/schema/config.js +1 -1
  103. package/dist-lib/schema/content.js +9 -1
  104. package/dist-lib/schema/engine.js +75 -6
  105. package/dist-lib/schema/field-order.js +1 -1
  106. package/dist-lib/schema/local.js +3 -2
  107. package/dist-lib/schema/qvac.js +28 -2
  108. package/dist-lib/spec/bind.js +17 -0
  109. package/dist-lib/spec/calls.js +4 -0
  110. package/dist-lib/spec/capabilities.d.ts +8 -0
  111. package/dist-lib/spec/config/hyperconf/index.js +1 -1
  112. package/dist-lib/spec/hrpc/hrpc.json +52 -0
  113. package/dist-lib/spec/hrpc/index.js +53 -4
  114. package/dist-lib/spec/hrpc/messages.js +1498 -1278
  115. package/dist-lib/spec/hyperschema/index.js +1498 -1278
  116. package/dist-lib/spec/hyperschema/schema.json +182 -0
  117. package/dist-lib/spec/hyperschema/types.d.ts +49 -3
  118. package/dist-lib/spec/local/hyperdb/messages.js +22 -10
  119. package/dist-lib/spec/local/hyperschema/index.js +16 -7
  120. package/dist-lib/spec/local/hyperschema/schema.json +10 -1
  121. package/dist-lib/spec/local/hyperschema/types.d.ts +3 -2
  122. package/dist-lib/spec/mesh/hyperdb/db.json +16 -1
  123. package/dist-lib/spec/mesh/hyperdb/index.js +71 -2
  124. package/dist-lib/spec/mesh/hyperdb/messages.js +530 -246
  125. package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +13 -1
  126. package/dist-lib/spec/mesh/hyperdispatch/index.js +35 -1
  127. package/dist-lib/spec/mesh/hyperdispatch/messages.js +223 -10
  128. package/dist-lib/spec/mesh/hyperschema/index.js +223 -10
  129. package/dist-lib/spec/mesh/hyperschema/schema.json +128 -1
  130. package/dist-lib/spec/mesh/hyperschema/types.d.ts +34 -2
  131. package/package.json +14 -19
  132. package/tui/README.md +7 -1
  133. package/tui/app.mjs +366 -78
  134. package/tui/boot.mjs +1 -0
  135. package/tui/index.mjs +1 -0
  136. package/tui/lib/fork-command.mjs +43 -0
  137. package/tui/lib/render.mjs +25 -1
  138. package/worker/assistant-sidecar.mjs +5 -2
  139. package/dist-lib/lib/core/device-invites.d.ts +0 -11
  140. package/dist-lib/lib/core/device-invites.js +0 -63
  141. package/dist-lib/lib/core/grant-expiry.d.ts +0 -11
  142. package/dist-lib/lib/core/grant-expiry.js +0 -66
  143. package/dist-lib/lib/core/mesh-seed-handoff.d.ts +0 -27
  144. package/dist-lib/lib/core/mesh-seed-handoff.js +0 -53
  145. package/dist-lib/lib/oauth/loopback.stub.d.ts +0 -3
  146. package/dist-lib/lib/oauth/loopback.stub.js +0 -6
@@ -13,10 +13,11 @@ import ReadyResource from 'ready-resource';
13
13
  import ScopeLock from 'scope-lock';
14
14
  import { isAndroid } from 'which-runtime';
15
15
  import { createApi } from './api/index.js';
16
+ import { writeAutoDownloadModels } from './api/app-config.js';
16
17
  import { NO_SUCH_MESH, requireMesh } from './api/current-mesh.js';
17
18
  import { ChatMoves } from './core/chat-move.js';
18
19
  import { asEncoded, watchModels } from './models-watch.js';
19
- import { BLIND_PEER_KEYS, BLIND_RELAY_KEYS } from './core/blind-peers.js';
20
+ import { BLIND_PEER_KEYS, BLIND_PEERING_OFF_KEY, BLIND_RELAY_KEYS } from './core/blind-peers.js';
20
21
  import { CoreBlobServer, MemoryVectorIndex } from './core/files/index.js';
21
22
  import { getChat, removeChatAttachments, removeChatKnowledge } from './core/chat.js';
22
23
  import { rebuildChatOrder, refreshChatOrder } from './core/chat-order.js';
@@ -24,37 +25,29 @@ import { createFsDriveHost } from './core/fs-drives.js';
24
25
  import { sweepDriveStaging } from './fs-drive-bridge.js';
25
26
  import { decodeMeshInvite, encodeMeshInvite } from './core/invite.js';
26
27
  import { fileKeyOf, knownFile, referencedFile } from './core/files/local-index.js';
27
- import { grantKeyPair, inviteGrantTermsRefusal } from './core/invite-grants.js';
28
- import { belongsToAnotherMember, inviteStanding, memberStanding, removedBy } from './core/contract/invites.js';
29
- import { syncStatusSnapshot } from './core/sync-status.js';
30
28
  import { openLocalStore } from './core/local/index.js';
31
29
  import { MAX_MESH_NAME_CHARS, meshNameWithinBounds } from '../schema/qvac.js';
32
- import { getAgent } from './core/agent.js';
30
+ import { getAgent, rehomeAgents } from './core/agent.js';
33
31
  import { countContents, migrateContents, withArchivedMesh } from './core/archived-mesh.js';
34
32
  import { HostedAgents } from './core/hosted-agents.js';
35
33
  import { HarnessLoan } from './core/harness-loan.js';
36
34
  import { duplexPair } from '@qvac/harness/transport';
37
- import { signMemberWriterClaim, signWriterClaim, verifyMemberWriterClaim, verifyWriterClaim } from './core/device-attestation.js';
38
- import { HOST_SILENT, HOST_UNREACHABLE } from './core/handoff-channel.js';
39
- import { MeshSeedHandoff, SEED_REFUSED, ADMISSION_DEFERRED, SEED_ASKER_UNSEEN } from './core/mesh-seed-handoff.js';
40
35
  import { SecretHandoff } from './core/secret-handoff.js';
41
36
  import { InferenceCallsHandoff } from './core/inference-calls-handoff.js';
42
37
  import { createInferenceCallsReceiver, NO_RECORD_STORE } from './core/inference-calls-receiver.js';
43
38
  import { SkillOverlayHandoff } from './core/skill-overlay-handoff.js';
44
39
  import { createSkillOverlayReceiver } from './core/skill-overlay-receiver.js';
45
40
  import { createSkillSecretReceiver, HOST_REVOKED, NO_SUCH_AGENT, NOT_THE_HOST } from './core/skill-secrets.js';
46
- import { attestMemberClaim, claimAttestation, deviceInviteToken, openIdentity } from './core/identity.js';
41
+ import { attestDeviceProof, attestMemberClaim, claimAttestation, delegationFits, openIdentity } from './core/identity.js';
47
42
  import { openRecoveryCore, publishRecovery, readRecovery, recoveryKeys } from './core/account-recovery.js';
48
- import { admitInvitedDevice, attestUnattestedDevices, claimOwnMembership, legacyNameStamp, namedAt, renameFoundedMember, sealAccount } from './core/membership.js';
49
- import { DEVICE_INVITE_SPENT, openDeviceInvites } from './core/device-invites.js';
43
+ import { claimOwnMembership, legacyNameStamp, namedAt, renameFoundedMember } from './core/membership.js';
50
44
  import { runMigrations } from './core/migrations/index.js';
51
45
  import { backfillFileIndex } from './core/migrations/file-index.js';
52
46
  import { advertiseDeviceVersion, retractInventory } from './core/devices.js';
53
47
  import { OtaConfigReader, configCoreFor, resolveConfigChannel, vendoredMinSupportedVersion } from './core/ota-config.js';
54
48
  import { updateRequired } from './core/min-version.js';
55
49
  import { PendingDepartures } from './core/departures.js';
56
- import { isKickedRow } from './core/helpers/index.js';
57
- import { Mesh, claimableDevice, meshPublicKeyFromSeed } from './core/mesh.js';
50
+ import { Mesh } from './core/mesh.js';
58
51
  import { raceResource } from './race-resource.js';
59
52
  import { createSystemInfoSampler } from './core/system-info.js';
60
53
  import { setLogger } from './logger.js';
@@ -63,19 +56,19 @@ import { openLocalSpec } from './spec-loader.js';
63
56
  import { unref } from './unref.js';
64
57
  import { local } from '../spec/index.js';
65
58
  import { isRecord } from './guards.js';
66
- 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';
67
61
  const QvacState = local.getEnum('@local/qvac-state');
68
62
  const top = new Top();
69
63
  const FILE_GC_INTERVAL_MS = 24 * 60 * 60 * 1000;
70
64
  const SWARM_KEYPAIR_NAME = 'qvac-swarm-keypair';
71
65
  const QVAC_CORE_NAMESPACE = 'qvac-core';
72
66
  const MESH_JOIN_CANCELED = 'Mesh join canceled';
73
- const GRANT_JOIN_TIMED_OUT = 'Join did not complete in time - the invite may already be spent, expired or revoked';
74
67
  const MESH_ID = /^[0-9a-f]{64}$/;
75
68
  const MESH_BELOW_FLOOR = 'Build is below the mesh floor';
76
69
  const MESH_WITHOUT_ACCOUNT = 'Recording a mesh needs the account mesh';
77
70
  export const ACCOUNT_MEMBER_UNKNOWN = 'This device is not part of its account yet';
78
- export const AWAITING_ADMISSION = 'awaiting-admission';
71
+ export const DEVICE_UNKNOWN = 'This device has no device details: pass `device` when creating Core';
79
72
  const MESH_WITHOUT_MEMBER = 'A mesh needs this device to belong to its account member';
80
73
  const NO_WRITABLE_ACCOUNT = 'The account mesh is not writable';
81
74
  const NOT_AN_ARCHIVED_ACCOUNT = 'Not an account this device left';
@@ -83,35 +76,18 @@ const NO_ACCOUNT_TO_RECOVER = 'This device has no account to make recoverable';
83
76
  const PHRASE_NOT_OURS = 'The phrase does not derive the identity this device holds';
84
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";
85
78
  const NO_RECOVERY_RECORD = 'No recovery record was found for this phrase';
86
- const RECOVERY_DEVICE_REMOVED = 'This device was removed from the account, so it cannot recover it: wipe it and recover as a new device';
87
79
  const RECOVERY_WAIT_MS = 30_000;
88
80
  export const MEMBER_INVITE_HOME_REFUSED = 'An account mesh cannot be invited into';
89
81
  export const DEVICE_INVITE_MESH_REFUSED = 'Only an account mesh can take a device invite';
90
82
  export const MESH_NAME_ACCOUNT_REFUSED = 'An account mesh has no shared name';
91
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';
92
85
  export const MESH_HELD_BY_SIBLING = 'Another of your devices holds this mesh: bring that device online, or invite from it instead';
93
- export const INVITE_GRANT_REVOKED = 'This invite was revoked';
94
- export const MESH_ALREADY_JOINED = 'This device is already in this mesh';
95
- export const MESH_ADMISSION_PENDING = 'This mesh has not admitted this device yet: try again in a moment';
96
- export const MEMBER_ALREADY_IN_MESH = 'Another of your devices is already in this mesh: join through it once it is online';
97
- export const DEVICE_OF_ANOTHER_MEMBER = 'This device belongs to another member in this mesh';
98
- export const MEMBER_REMOVED_SINCE_INVITE = 'You were removed from this mesh after this invite was made: ask for a new one';
99
- export const INVITE_GRANT_EXPIRED = 'This invite has expired';
100
- export const INVITE_GRANT_EXHAUSTED = 'This invite has already been used';
101
- export const SEALED_ACCOUNT_READER = 'This account admits no device by its seed alone: pair this device with a device invite';
102
- 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';
103
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';
104
- const UNANSWERED_SEED_REQUESTS = new Set([HOST_SILENT, SEED_ASKER_UNSEEN]);
105
88
  const INVITE_DHT_NODES_LIMIT = 3;
106
89
  const INVITE_BLIND_PEERS_LIMIT = 3;
107
- const GRANT_DEFAULT_MAX_USES = 1;
108
- const GRANT_DEFAULT_TTL_MS = 48 * 60 * 60 * 1000;
109
- const GRANT_VISIBLE_MS = 10_000;
110
90
  const DIAGNOSTICS_SAMPLE_MS = 2000;
111
- const ACCOUNT_WRITABLE_MS = 15_000;
112
- const GRANT_WRITABLE_MS = 60_000;
113
- const INVITER_RETRY_MS = 1_000;
114
- const ADMISSION_RETRIES = 60;
115
91
  export class Core extends ReadyResource {
116
92
  _storagePath;
117
93
  store;
@@ -121,7 +97,6 @@ export class Core extends ReadyResource {
121
97
  _ownsSwarm;
122
98
  _ownsWakeup;
123
99
  local;
124
- _deviceInvites;
125
100
  state;
126
101
  _transport;
127
102
  _apiLifecycle;
@@ -134,6 +109,8 @@ export class Core extends ReadyResource {
134
109
  _otaConfigCore;
135
110
  _otaConfig = null;
136
111
  _configPeering = null;
112
+ _blindPeeringOn = true;
113
+ _blindPeeringListeners = new Set();
137
114
  _recoveryCore = null;
138
115
  _recoverySigner = null;
139
116
  _recoveryPeering = null;
@@ -141,15 +118,14 @@ export class Core extends ReadyResource {
141
118
  _blindPeerKeys;
142
119
  _blindPeeringOpts;
143
120
  _fileGc;
121
+ _autoDownloadModels;
144
122
  _fileGcTimer = null;
145
123
  _fileGcRun = null;
146
124
  _meshSeed;
147
125
  _meshKey;
148
126
  _additionalNodes;
149
- _grantWritableMs;
150
- _accountWritableMs;
151
- _grantVisibleMs;
152
127
  _listModels;
128
+ _modelMemory = null;
153
129
  _deleteModel;
154
130
  _clearKvCache;
155
131
  _deleteChatKvCache;
@@ -198,17 +174,17 @@ export class Core extends ReadyResource {
198
174
  _identityPhrase = null;
199
175
  _identityPublicKey = null;
200
176
  _swarm;
177
+ _siblings = new Map();
178
+ _seenPeers = new Set();
179
+ _siblingLock = new ScopeLock({ debounce: true });
201
180
  _onSwarmConnection;
202
181
  _departures = null;
203
182
  _fsDrives = null;
204
183
  _blobServer = null;
205
184
  _wakeup;
206
- _meshSeedHandoff;
207
185
  _harnessLoan;
208
186
  _serveLoanedHarness = null;
209
187
  _following = new Map();
210
- _admissionRetries = new Map();
211
- _admissionRetryPending = new Set();
212
188
  _revokingInMeshes = null;
213
189
  _revokeInMeshesAgain = false;
214
190
  _sweepUnknownEverywhere = false;
@@ -217,6 +193,7 @@ export class Core extends ReadyResource {
217
193
  _skillOverlayHandoff;
218
194
  _inferenceCallsHandoff;
219
195
  _minting;
196
+ _backfill = Promise.resolve();
220
197
  _leaving;
221
198
  _kicked;
222
199
  _kickedPending;
@@ -231,7 +208,7 @@ export class Core extends ReadyResource {
231
208
  _systemInfo;
232
209
  _statusError;
233
210
  _upgradeRequired;
234
- 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, 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 }) {
211
+ 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 }) {
235
212
  super();
236
213
  this._transport = transport;
237
214
  this._apiLifecycle = apiLifecycle ?? {
@@ -257,7 +234,6 @@ export class Core extends ReadyResource {
257
234
  this.rootCorestore = store ?? new Corestore(this.storage, { globalCache: new Rache() });
258
235
  this.store = this.rootCorestore.namespace(QVAC_CORE_NAMESPACE);
259
236
  this.local = openLocalStore(this.storage);
260
- this._deviceInvites = openDeviceInvites(this.local);
261
237
  this.state = QvacState.RESUME;
262
238
  this._spec = spec;
263
239
  this._bootstrapNodes = bootstrapNodes;
@@ -274,12 +250,10 @@ export class Core extends ReadyResource {
274
250
  this._blindPeerKeys = blindPeerKeys;
275
251
  this._blindPeeringOpts = blindPeeringOpts;
276
252
  this._fileGc = fileGc;
253
+ this._autoDownloadModels = autoDownloadModels;
277
254
  this._meshSeed = meshSeed;
278
255
  this._meshKey = meshKey;
279
256
  this._additionalNodes = additionalNodes;
280
- this._grantWritableMs = grantWritableMs;
281
- this._accountWritableMs = accountWritableMs;
282
- this._grantVisibleMs = grantVisibleMs;
283
257
  this._listModels = listModels ?? null;
284
258
  this._deleteModel = deleteModel ?? null;
285
259
  this._clearKvCache = clearKvCache ?? null;
@@ -336,9 +310,6 @@ export class Core extends ReadyResource {
336
310
  this._skillOverlayHandoff = new SkillOverlayHandoff({
337
311
  onrequest: (op, from) => this._serveSkillOverlayOp(op, from)
338
312
  });
339
- this._meshSeedHandoff = new MeshSeedHandoff({
340
- onrequest: (request, from) => this._serveMeshSeed(request, from)
341
- });
342
313
  this._harnessLoan = new HarnessLoan({ onborrow: (from) => this._lendHarness(from) });
343
314
  this._inferenceCallsHandoff = new InferenceCallsHandoff({
344
315
  onrequest: (request, from) => this._serveInferenceCalls(request, from)
@@ -394,20 +365,27 @@ export class Core extends ReadyResource {
394
365
  this._deviceKeyPair =
395
366
  this._swarm?.keyPair ?? (await this.store.createKeyPair(SWARM_KEYPAIR_NAME));
396
367
  this._identityPublicKey = (await this.local.readIdentity())?.publicKey ?? null;
368
+ this._blindPeeringOn = (await this.local.get(BLIND_PEERING_OFF_KEY)) !== true;
397
369
  this._localDevice = await this.local.ensureDevice(this._deviceKeyPair.publicKey, this._device);
370
+ if (this._autoDownloadModels !== undefined) {
371
+ await writeAutoDownloadModels(this.local, this._autoDownloadModels);
372
+ }
398
373
  const swarm = this._openSwarm(this._deviceKeyPair);
399
374
  this._departures = new PendingDepartures(this.store, this.local, {
400
375
  swarm,
401
376
  isPooled: (key) => this._pooled(b4a.toHex(key)) !== null,
377
+ deviceId: () => this._deviceKeyPair?.publicKey ?? null,
402
378
  logger: this._log,
403
379
  blindPeerKeys: this._resolveBlindPeerKeys(),
404
380
  blindPeeringOpts: this._blindPeeringOpts,
381
+ blindPeering: this._blindPeeringOn,
405
382
  wakeup: this._wakeup
406
383
  });
407
384
  this._fsDrives = createFsDriveHost(this.store, {
408
385
  swarm,
409
386
  blindPeerKeys: this._resolveBlindPeerKeys(),
410
- blindPeeringOpts: this._blindPeeringOpts
387
+ blindPeeringOpts: this._blindPeeringOpts,
388
+ blindPeering: this._blindPeeringOn
411
389
  });
412
390
  void sweepDriveStaging();
413
391
  this._blobServer = new CoreBlobServer(this.store, (coreKey, blockOffset, meshKey) => this._meshForBlobCore(coreKey, blockOffset, meshKey), { onRead: (mesh, coreKey, blockOffset) => this._servedFile(mesh, coreKey, blockOffset) });
@@ -430,6 +408,7 @@ export class Core extends ReadyResource {
430
408
  mesh: this._meshPool(),
431
409
  meshStatus,
432
410
  network: this._networkStatusSource(),
411
+ blindPeering: this._blindPeeringSource(),
433
412
  diagnostics: this._diagnosticsSource(),
434
413
  inferenceCalls: {
435
414
  list: (meshId, chatId) => this._listInferenceCalls(meshId, chatId),
@@ -445,7 +424,7 @@ export class Core extends ReadyResource {
445
424
  computeLender: () => this._computeLender(),
446
425
  pushSkillSecret: (request) => this.pushSkillSecret(request),
447
426
  listModels: this._hostsModelSource || this._listModels
448
- ? async (request) => ({ models: (await this._readModels(request)) ?? [] })
427
+ ? async (request) => (await this._readModelList(request)) ?? { models: [] }
449
428
  : null,
450
429
  deleteModel: this._hostsModelSource || this._deleteModel
451
430
  ? (options) => this._deleteModel?.(options) ??
@@ -478,7 +457,7 @@ export class Core extends ReadyResource {
478
457
  ? () => this._watchModelCatalog()
479
458
  : null,
480
459
  modelsWatch: this._hostsModelSource || this._modelCatalogWatch
481
- ? (request) => watchModels(() => this._modelCatalogRevisions(), () => this._readModels(request))
460
+ ? (request) => watchModels(() => this._modelCatalogRevisions(), () => this._readModelList(request))
482
461
  : null,
483
462
  retryModelDownload: this._hostsModelSource || this._retryModelDownload
484
463
  ? ({ name }) => this._retryModelDownload?.(name) ??
@@ -725,7 +704,11 @@ export class Core extends ReadyResource {
725
704
  }
726
705
  _peerConfigCore() {
727
706
  const config = this._otaConfig;
728
- if (!config || this._configPeering || this._accountMesh() || !this._swarm) {
707
+ if (!config ||
708
+ !this._blindPeeringOn ||
709
+ this._configPeering ||
710
+ this._accountMesh() ||
711
+ !this._swarm) {
729
712
  return;
730
713
  }
731
714
  const keys = this._resolveBlindPeerKeys();
@@ -734,6 +717,7 @@ export class Core extends ReadyResource {
734
717
  }
735
718
  this._configPeering = new BlindPeering(this._swarm.dht, this.store, {
736
719
  pick: keys.length,
720
+ suspended: this.suspended,
737
721
  ...this._blindPeeringOpts
738
722
  });
739
723
  this._configPeering.addCoreBackground(config.core, { keys });
@@ -862,6 +846,7 @@ export class Core extends ReadyResource {
862
846
  if (this._recoveryPeering) {
863
847
  await this._recoveryPeering.suspend();
864
848
  }
849
+ await this._fsDrives?.suspend();
865
850
  if (this._swarm && this._ownsSwarm) {
866
851
  await this._swarm.suspend();
867
852
  }
@@ -903,6 +888,7 @@ export class Core extends ReadyResource {
903
888
  if (this._recoveryPeering) {
904
889
  await this._recoveryPeering.resume();
905
890
  }
891
+ await this._fsDrives?.resume();
906
892
  for (const { mesh } of this._meshes.values()) {
907
893
  await mesh.resume();
908
894
  }
@@ -916,16 +902,28 @@ export class Core extends ReadyResource {
916
902
  }
917
903
  this._startFileGc();
918
904
  if (this._swarm) {
905
+ for (const [id, key] of this._siblings) {
906
+ if (this._seenPeers.has(id)) {
907
+ this._swarm.joinPeer(key);
908
+ }
909
+ }
919
910
  void this._refreshTopics(this._swarm).catch(noop);
920
911
  }
921
912
  void this._pushIdentityPhrase();
922
913
  this._log?.info('lifecycle', 'core resume');
923
914
  }
924
915
  async _refreshTopics(swarm) {
916
+ let topics = 0;
925
917
  for (const discovery of this._joinedTopics(swarm)) {
926
918
  await discovery.flushed();
927
919
  await discovery.refresh();
920
+ topics++;
928
921
  }
922
+ this._log?.info(['p2p', 'swarm'], 'topics refreshed after resume', {
923
+ topics,
924
+ peers: this.peerDeviceIds.length,
925
+ siblings: this._siblings.size
926
+ });
929
927
  }
930
928
  _joinedTopics(swarm) {
931
929
  if (this._ownsSwarm) {
@@ -1076,31 +1074,61 @@ export class Core extends ReadyResource {
1076
1074
  });
1077
1075
  try {
1078
1076
  await mesh.ready();
1079
- await raceResource(mesh.autobee, (async () => {
1080
- const birth = sharing
1081
- ? await this._accountBirthMember(mesh, session)
1082
- : await this._mintBirthMember(mesh, deviceId, session);
1083
- await mesh.appendBirthOp(this._requireDeviceKeyPair(), this._localDevice ?? undefined, {
1084
- member: birth?.member,
1085
- sharing
1086
- });
1077
+ if (session.baseKey && mesh.localLength === 0) {
1078
+ this._requireLocalDevice();
1087
1079
  await this.local.recordMesh(mesh.key, session, { deviceId, account });
1088
- if (account && birth?.mnemonic) {
1089
- await this._publishRecovery(birth.mnemonic, {
1090
- meshKey: mesh.key,
1091
- seed: session.seed,
1092
- createdAt: Date.now()
1093
- });
1094
- }
1095
- })(), 'mesh');
1080
+ void this._appendJoinLater(mesh, () => this._knownBirthMember(mesh, deviceId), sharing);
1081
+ }
1082
+ else {
1083
+ await this._appendOwnBirth(mesh, session, { deviceId, account, sharing });
1084
+ }
1096
1085
  }
1097
1086
  catch (err) {
1087
+ if (mesh.opened && mesh.interrupted && !this._upgradeRequired) {
1088
+ this._setUpgradeRequired('halt');
1089
+ }
1098
1090
  await mesh.close().catch(noop);
1099
1091
  throw err;
1100
1092
  }
1101
1093
  this._poolEntry(mesh);
1102
1094
  return mesh;
1103
1095
  }
1096
+ async _appendOwnBirth(mesh, session, { deviceId, account, sharing }) {
1097
+ await raceResource(mesh.autobee, (async () => {
1098
+ const birth = sharing
1099
+ ? await this._accountBirthMember(mesh, session)
1100
+ : await this._mintBirthMember(mesh, deviceId, session);
1101
+ await mesh.appendBirthOp(this._requireDeviceKeyPair(), this._localDevice ?? undefined, {
1102
+ member: birth?.member,
1103
+ sharing
1104
+ });
1105
+ await this.local.recordMesh(mesh.key, session, { deviceId, account });
1106
+ if (account && birth?.mnemonic) {
1107
+ await this._publishRecovery(birth.mnemonic, {
1108
+ meshKey: mesh.key,
1109
+ seed: session.seed,
1110
+ createdAt: Date.now()
1111
+ });
1112
+ }
1113
+ })(), 'mesh');
1114
+ }
1115
+ _requireLocalDevice() {
1116
+ if (!this._localDevice)
1117
+ throw new Error(DEVICE_UNKNOWN);
1118
+ return this._localDevice;
1119
+ }
1120
+ _appendJoinLater(mesh, member, sharing = false) {
1121
+ const birth = async () => ({ member: (await member()) ?? undefined, sharing });
1122
+ const appended = mesh.appendBirthOp(this._requireDeviceKeyPair(), this._localDevice ?? undefined, birth);
1123
+ return appended.catch((err) => {
1124
+ if (this.closing || mesh.closing)
1125
+ return;
1126
+ if (mesh.interrupted && !this._upgradeRequired) {
1127
+ this._setUpgradeRequired('halt');
1128
+ }
1129
+ this._log?.warn(['p2p', 'mesh'], 'appending the join failed', err);
1130
+ });
1131
+ }
1104
1132
  async _accountBirthMember(mesh, session) {
1105
1133
  if (!this._localDevice || session.baseKey || mesh.localLength > 0) {
1106
1134
  return undefined;
@@ -1182,7 +1210,7 @@ export class Core extends ReadyResource {
1182
1210
  const core = await this._openOwnRecoveryCore(recoveryKeys(identity));
1183
1211
  const appended = await publishRecovery(core, record);
1184
1212
  const held = await readRecovery(core);
1185
- if (held?.meshKey && b4a.equals(held.meshKey, record.meshKey)) {
1213
+ if (this._blindPeeringOn && held?.meshKey && b4a.equals(held.meshKey, record.meshKey)) {
1186
1214
  await this._markRecoverable(held, core);
1187
1215
  }
1188
1216
  this._peerRecoveryCore(core);
@@ -1213,6 +1241,7 @@ export class Core extends ReadyResource {
1213
1241
  }
1214
1242
  async recoverAccount(mnemonic, { timeout = RECOVERY_WAIT_MS } = {}) {
1215
1243
  await this._awaitMesh();
1244
+ this._requireLocalDevice();
1216
1245
  const identity = await openIdentity(mnemonic);
1217
1246
  const keys = recoveryKeys(identity);
1218
1247
  const core = openRecoveryCore(this.store, keys);
@@ -1236,7 +1265,8 @@ export class Core extends ReadyResource {
1236
1265
  ownsTransition = true;
1237
1266
  this._minting = true;
1238
1267
  this._notifyStatus();
1239
- await this._recoverAccount(identity, record);
1268
+ const joined = await this._joinRecoveredAccount(identity, record);
1269
+ await this._rerootRecovered(identity, record, joined);
1240
1270
  await this._markRecoverable(record, core);
1241
1271
  await this._adoptRecoveryCore(core, keys.coreKeyPair.publicKey);
1242
1272
  }
@@ -1253,21 +1283,24 @@ export class Core extends ReadyResource {
1253
1283
  this._departures?.flush();
1254
1284
  }
1255
1285
  }
1256
- async _recoverAccount(identity, record) {
1286
+ async _joinRecoveredAccount(identity, record) {
1257
1287
  const deviceId = this._requireDeviceKeyPair().publicKey;
1258
1288
  const proof = await identity.bootstrap(deviceId);
1259
1289
  const meshId = b4a.toHex(record.meshKey);
1260
1290
  await this._opening.get(meshId)?.catch(noop);
1261
1291
  const pooled = this._pooled(meshId);
1262
- const recovery = pooled?.writable ? undefined : { memberId: identity.identityPublicKey, proof };
1263
1292
  if (pooled?.writable) {
1264
1293
  if (!this._isAccount(pooled)) {
1265
1294
  await this._adoptAccount(pooled);
1266
1295
  }
1296
+ return { proof, recovered: false };
1267
1297
  }
1268
- else {
1269
- await this._joinMesh({ seed: record.seed, baseKey: record.meshKey, fastForward: null, recovery }, pooled);
1270
- }
1298
+ const recovery = { memberId: identity.identityPublicKey, proof };
1299
+ await this._joinMesh({ seed: record.seed, baseKey: record.meshKey, fastForward: null, recovery }, pooled);
1300
+ return { proof, recovered: true };
1301
+ }
1302
+ async _rerootRecovered(identity, record, { proof, recovered }) {
1303
+ const deviceId = this._requireDeviceKeyPair().publicKey;
1271
1304
  const mesh = this._accountMesh();
1272
1305
  if (!mesh?.key || !b4a.equals(mesh.key, record.meshKey)) {
1273
1306
  throw new Error('the recovered account did not adopt');
@@ -1283,7 +1316,7 @@ export class Core extends ReadyResource {
1283
1316
  createdAt: now
1284
1317
  });
1285
1318
  this._identityPublicKey = identity.identityPublicKey;
1286
- if (recovery) {
1319
+ if (recovered) {
1287
1320
  return;
1288
1321
  }
1289
1322
  await mesh.dispatch('attest-member-device', {
@@ -1308,7 +1341,7 @@ export class Core extends ReadyResource {
1308
1341
  throw new Error(NO_RECOVERY_RECORD);
1309
1342
  }
1310
1343
  _peerRecoveryCore(core) {
1311
- if (!this._swarm || this.closing)
1344
+ if (!this._swarm || this.closing || !this._blindPeeringOn)
1312
1345
  return;
1313
1346
  const keys = this._resolveBlindPeerKeys();
1314
1347
  if (keys.length === 0) {
@@ -1317,6 +1350,7 @@ export class Core extends ReadyResource {
1317
1350
  if (!this._recoveryPeering) {
1318
1351
  this._recoveryPeering = new BlindPeering(this._swarm.dht, this.store, {
1319
1352
  pick: keys.length,
1353
+ suspended: this.suspended,
1320
1354
  ...this._blindPeeringOpts
1321
1355
  });
1322
1356
  }
@@ -1337,23 +1371,12 @@ export class Core extends ReadyResource {
1337
1371
  if (!local || mesh.localLength > 0) {
1338
1372
  return undefined;
1339
1373
  }
1340
- const now = Date.now();
1341
- const attestation = claimAttestation(mesh.publicKey, deviceId, mesh.localWriterKey);
1342
- const known = await this._knownMember(mesh);
1374
+ const known = await this._knownBirthMember(mesh, deviceId);
1343
1375
  if (known) {
1344
- const identity = await this.local.readIdentity();
1345
- const name = identity?.displayName;
1346
- const ours = identity && name && namedAt(identity) >= known.updatedAt;
1347
- return {
1348
- member: {
1349
- id: known.id,
1350
- name: ours ? name : known.name,
1351
- joinedAt: now,
1352
- updatedAt: ours ? namedAt(identity) : known.updatedAt
1353
- },
1354
- proof: await attestMemberClaim(attestation, this._requireDeviceKeyPair(), known.proof)
1355
- };
1376
+ return known;
1356
1377
  }
1378
+ const now = Date.now();
1379
+ const attestation = claimAttestation(mesh.publicKey, deviceId, mesh.localWriterKey);
1357
1380
  const { identity, mnemonic } = await this.local.ensureIdentity(deviceId, now);
1358
1381
  this._identityPublicKey = identity.publicKey;
1359
1382
  this._holdIdentityPhrase(mnemonic);
@@ -1368,6 +1391,46 @@ export class Core extends ReadyResource {
1368
1391
  proof
1369
1392
  };
1370
1393
  }
1394
+ async _knownBirthMember(mesh, deviceId) {
1395
+ if (!this._localDevice || mesh.localLength > 0)
1396
+ return null;
1397
+ const known = await this._knownMember(mesh);
1398
+ if (!known) {
1399
+ return null;
1400
+ }
1401
+ const identity = await this.local.readIdentity();
1402
+ const name = identity?.displayName;
1403
+ const ours = identity && name && namedAt(identity) >= known.updatedAt;
1404
+ const attestation = claimAttestation(mesh.publicKey, deviceId, mesh.localWriterKey);
1405
+ return {
1406
+ member: {
1407
+ id: known.id,
1408
+ name: ours ? name : known.name,
1409
+ joinedAt: Date.now(),
1410
+ updatedAt: ours ? namedAt(identity) : known.updatedAt
1411
+ },
1412
+ proof: await attestMemberClaim(attestation, this._requireDeviceKeyPair(), known.proof)
1413
+ };
1414
+ }
1415
+ async _recoveryBirthMember(mesh, recovery) {
1416
+ const deviceKeyPair = this._requireDeviceKeyPair();
1417
+ const attestation = claimAttestation(mesh.publicKey, deviceKeyPair.publicKey, mesh.localWriterKey);
1418
+ await this.local.recordMemberProof({
1419
+ meshKey: mesh.key,
1420
+ memberId: recovery.memberId,
1421
+ proof: recovery.proof,
1422
+ attestedAt: 0
1423
+ });
1424
+ return {
1425
+ member: {
1426
+ id: recovery.memberId,
1427
+ name: this._localDevice?.name ?? '',
1428
+ joinedAt: Date.now(),
1429
+ updatedAt: 0
1430
+ },
1431
+ proof: await attestMemberClaim(attestation, deviceKeyPair, recovery.proof)
1432
+ };
1433
+ }
1371
1434
  _holdIdentityPhrase(mnemonic) {
1372
1435
  if (!mnemonic)
1373
1436
  return;
@@ -1395,16 +1458,63 @@ export class Core extends ReadyResource {
1395
1458
  return false;
1396
1459
  }
1397
1460
  }
1461
+ _startBlindPeering(mesh) {
1462
+ if (this._swarm && this._blindPeeringOn) {
1463
+ mesh.startBlindPeering(this._swarm.dht);
1464
+ }
1465
+ }
1466
+ async setBlindPeering(enabled) {
1467
+ if (enabled !== this._blindPeeringOn) {
1468
+ this._blindPeeringOn = enabled;
1469
+ await (enabled ? this._resumeBlindPeering() : this._stopBlindPeering());
1470
+ this._log?.info(['p2p', 'mesh'], enabled ? 'blind peering on' : 'blind peering off');
1471
+ for (const listener of this._blindPeeringListeners) {
1472
+ listener();
1473
+ }
1474
+ }
1475
+ await this.local.set(BLIND_PEERING_OFF_KEY, !this._blindPeeringOn);
1476
+ return { enabled: this._blindPeeringOn };
1477
+ }
1478
+ _peeredMeshes() {
1479
+ const meshes = [...this._meshes.values()].map((entry) => entry.mesh);
1480
+ return this._candidateMesh ? [...meshes, this._candidateMesh] : meshes;
1481
+ }
1482
+ async _stopBlindPeering() {
1483
+ const recovery = this._recoveryPeering;
1484
+ this._recoveryPeering = null;
1485
+ await Promise.all([
1486
+ ...this._peeredMeshes().map((mesh) => mesh.stopBlindPeering()),
1487
+ this._departures?.stopBlindPeering(),
1488
+ this._fsDrives?.stopBlindPeering(),
1489
+ this._closeConfigPeering(),
1490
+ recovery?.close()
1491
+ ]);
1492
+ }
1493
+ async _resumeBlindPeering() {
1494
+ const suspended = this.suspended;
1495
+ if (this._swarm) {
1496
+ for (const mesh of this._peeredMeshes()) {
1497
+ mesh.startBlindPeering(this._swarm.dht, { suspended });
1498
+ }
1499
+ }
1500
+ this._peerConfigCore();
1501
+ if (this._recoveryCore) {
1502
+ this._peerRecoveryCore(this._recoveryCore);
1503
+ }
1504
+ this._departures?.startBlindPeering({ suspended });
1505
+ await this._fsDrives?.startBlindPeering();
1506
+ }
1398
1507
  _bringMeshOnline(mesh, opts = {}) {
1399
1508
  if (this.closing || !this._isPooled(mesh))
1400
1509
  return;
1401
1510
  if (this._swarm) {
1402
1511
  this._swarm.join(mesh.discoveryKey, { server: true, client: true });
1403
- mesh.startBlindPeering(this._swarm.dht);
1512
+ this._startBlindPeering(mesh);
1404
1513
  if (this.suspended) {
1405
1514
  void mesh.suspend().catch(noop);
1406
1515
  }
1407
1516
  this._log?.info(['p2p', 'swarm'], 'announced and peering the mesh');
1517
+ void this._dialSiblings(mesh).catch(noop);
1408
1518
  }
1409
1519
  if (!opts.new) {
1410
1520
  void this._ensureNotRevoked(mesh);
@@ -1415,6 +1525,57 @@ export class Core extends ReadyResource {
1415
1525
  void this._retractInventory(mesh, this._requireDeviceKeyPair().publicKey);
1416
1526
  void this._settleMembership(mesh, { migrate: true });
1417
1527
  }
1528
+ async _dialSiblings(mesh) {
1529
+ if (!this._isAccount(mesh) || !(await this._siblingLock.lock())) {
1530
+ return;
1531
+ }
1532
+ try {
1533
+ await this._redialSiblings(mesh);
1534
+ }
1535
+ finally {
1536
+ this._siblingLock.unlock();
1537
+ }
1538
+ }
1539
+ async _redialSiblings(mesh) {
1540
+ const swarm = this._swarm;
1541
+ if (!swarm || this.closing) {
1542
+ return;
1543
+ }
1544
+ const live = await this._siblingsToDial(mesh, swarm);
1545
+ if (!this._isAccount(mesh) || this.closing) {
1546
+ return;
1547
+ }
1548
+ let changed = false;
1549
+ for (const [id, key] of this._siblings) {
1550
+ if (live.has(id))
1551
+ continue;
1552
+ swarm.leavePeer(key);
1553
+ changed = true;
1554
+ }
1555
+ for (const [id, key] of live) {
1556
+ if (this._siblings.has(id))
1557
+ continue;
1558
+ swarm.joinPeer(key);
1559
+ changed = true;
1560
+ }
1561
+ this._siblings = live;
1562
+ if (changed) {
1563
+ this._log?.info(['p2p', 'swarm'], 'dialling sibling devices', { devices: live.size });
1564
+ }
1565
+ }
1566
+ async _siblingsToDial(mesh, swarm) {
1567
+ const self = this._requireDeviceKeyPair().publicKey;
1568
+ const hosts = new Set([...swarm.explicitPeers].map((peer) => b4a.toHex(peer.publicKey)));
1569
+ const live = new Map();
1570
+ for await (const device of mesh.view.find(DEVICES)) {
1571
+ const id = b4a.toHex(device.id);
1572
+ const theirs = hosts.has(id) && !this._siblings.has(id);
1573
+ if (!device.revoked && !theirs && !b4a.equals(device.id, self)) {
1574
+ live.set(id, device.id);
1575
+ }
1576
+ }
1577
+ return live;
1578
+ }
1418
1579
  async _advertiseDeviceProfile(mesh, deviceId) {
1419
1580
  const local = this._localDevice;
1420
1581
  if (!local?.platform) {
@@ -1516,8 +1677,9 @@ export class Core extends ReadyResource {
1516
1677
  });
1517
1678
  }
1518
1679
  await this._claimOwnMembership(mesh, context);
1519
- await attestUnattestedDevices(mesh, context, this._deviceInvites);
1520
- await this._sealAccount(mesh);
1680
+ await this._backfillMeshSeeds(mesh).catch((err) => {
1681
+ this._log?.warn(['p2p', 'mesh'], 'backfilling mesh seeds failed', err);
1682
+ });
1521
1683
  await this._renameFoundedMember(mesh, context);
1522
1684
  await this._revokeInMeshes();
1523
1685
  }
@@ -1528,12 +1690,6 @@ export class Core extends ReadyResource {
1528
1690
  entry.memberLock.unlock();
1529
1691
  }
1530
1692
  }
1531
- async _sealAccount(mesh) {
1532
- if ((this._meshSeed && b4a.equals(this._meshSeed, mesh.seed)) || !this._isAccount(mesh)) {
1533
- return;
1534
- }
1535
- await sealAccount(mesh, { deviceKeyPair: this._requireDeviceKeyPair(), log: this._log });
1536
- }
1537
1693
  async _renameFoundedMember(mesh, context) {
1538
1694
  let identity = await this.local.readIdentity();
1539
1695
  if (!identity) {
@@ -1603,7 +1759,16 @@ export class Core extends ReadyResource {
1603
1759
  };
1604
1760
  }
1605
1761
  }
1606
- return null;
1762
+ return this._recordedMember();
1763
+ }
1764
+ async _recordedMember() {
1765
+ const account = await this._storedAccountKey();
1766
+ const link = account ? await this.local.readMemberProof(account) : null;
1767
+ if (!link) {
1768
+ return null;
1769
+ }
1770
+ const name = (await this.local.readIdentity())?.displayName ?? '';
1771
+ return { id: link.memberId, name, updatedAt: 0, proof: link.proof };
1607
1772
  }
1608
1773
  _migrationContext() {
1609
1774
  const local = this._localDevice;
@@ -1814,6 +1979,7 @@ export class Core extends ReadyResource {
1814
1979
  throw new Error('Invalid mesh invite');
1815
1980
  }
1816
1981
  await this._awaitMesh();
1982
+ this._requireLocalDevice();
1817
1983
  await this._meshTransitionLock.lock();
1818
1984
  let ownsTransition = false;
1819
1985
  try {
@@ -1856,15 +2022,10 @@ export class Core extends ReadyResource {
1856
2022
  throw new Error(MESH_JOIN_CANCELED);
1857
2023
  }
1858
2024
  async _joinMesh(invite, retire = null) {
1859
- const cancellation = {
1860
- canceled: false,
1861
- accepting: true,
1862
- deadline: Date.now() + this._grantVisibleMs,
1863
- account: !invite.memberInvite
1864
- };
2025
+ const cancellation = { canceled: false, accepting: true, account: !invite.memberInvite };
1865
2026
  this._joinCancellation = cancellation;
1866
2027
  try {
1867
- if (invite.memberInvite && invite.inviteNonce) {
2028
+ if (invite.memberInvite) {
1868
2029
  await this._joinMeshInvite(invite);
1869
2030
  }
1870
2031
  else if (await this._reopenOwnAccount(invite)) {
@@ -1881,7 +2042,7 @@ export class Core extends ReadyResource {
1881
2042
  }
1882
2043
  }
1883
2044
  async _reopenOwnAccount(invite) {
1884
- if (invite.recovery || !invite.memberId || !invite.inviteNonce)
2045
+ if (invite.recovery || !invite.memberId)
1885
2046
  return false;
1886
2047
  const stored = await this._storedAccountKey();
1887
2048
  if (!stored || !b4a.equals(stored, invite.baseKey)) {
@@ -1891,12 +2052,7 @@ export class Core extends ReadyResource {
1891
2052
  return false;
1892
2053
  }
1893
2054
  const account = await this._openPooledMesh(b4a.toHex(invite.baseKey));
1894
- const own = await account.view.get(DEVICES, {
1895
- id: this._requireDeviceKeyPair().publicKey
1896
- });
1897
- if (!own ||
1898
- own.revoked ||
1899
- !(own.writerKey && b4a.equals(own.writerKey, account.localWriterKey))) {
2055
+ if (!(await this._keepsWriter(account))) {
1900
2056
  return false;
1901
2057
  }
1902
2058
  if (!this._isAccount(account)) {
@@ -1929,7 +2085,7 @@ export class Core extends ReadyResource {
1929
2085
  const meshId = b4a.toHex(invite.baseKey);
1930
2086
  await this._following.get(meshId);
1931
2087
  const joining = (async () => {
1932
- if (!(await this._openOwnMesh(invite))) {
2088
+ if (!(await this._reopenRecordedMesh(invite))) {
1933
2089
  await this._claimJoin(invite, this._pooled(meshId));
1934
2090
  }
1935
2091
  })();
@@ -1944,6 +2100,22 @@ export class Core extends ReadyResource {
1944
2100
  }
1945
2101
  }
1946
2102
  }
2103
+ async _reopenRecordedMesh(invite) {
2104
+ if (!(await this.local.readMeshSession(invite.baseKey)))
2105
+ return false;
2106
+ return this._keepsWriter(await this._openPooledMesh(b4a.toHex(invite.baseKey)));
2107
+ }
2108
+ async _keepsWriter(mesh) {
2109
+ if (mesh.writable)
2110
+ return true;
2111
+ if (await mesh.ownWriterRefused()) {
2112
+ return false;
2113
+ }
2114
+ const own = await mesh.view.get(DEVICES, {
2115
+ id: this._requireDeviceKeyPair().publicKey
2116
+ });
2117
+ return !(own && isOwnRevokedRow(mesh, own));
2118
+ }
1947
2119
  async _runJoinMesh(invite, claim) {
1948
2120
  const deviceId = this._requireDeviceKeyPair().publicKey;
1949
2121
  const source = this._accountMesh();
@@ -1982,32 +2154,44 @@ export class Core extends ReadyResource {
1982
2154
  this._joinCancellation.accepting = false;
1983
2155
  }
1984
2156
  try {
1985
- if (source && !invite.memberInvite && (await this._departureHasAudience(source))) {
1986
- await this.local.recordDeparture(source.key, { creator: source.minted });
1987
- }
1988
2157
  await this.local.recordMesh(candidate.key, session, { deviceId });
1989
2158
  }
1990
2159
  catch (error) {
1991
2160
  await this._discardCandidateMesh();
1992
2161
  throw error;
1993
2162
  }
1994
- return this._landJoinedMesh(invite, candidate, { source, deviceId });
2163
+ const member = await this._candidateBirthMember(candidate, invite);
2164
+ const appended = this._appendJoinLater(candidate, async () => member);
2165
+ return this._landJoinedMesh(candidate, {
2166
+ source,
2167
+ deviceId,
2168
+ memberInvite: !!invite.memberInvite,
2169
+ appended
2170
+ });
1995
2171
  }
1996
- async _landJoinedMesh(invite, candidate, { source, deviceId }) {
1997
- if (!invite.memberInvite) {
2172
+ async _landJoinedMesh(candidate, { source, deviceId, memberInvite, appended }) {
2173
+ if (!memberInvite) {
1998
2174
  await this._departOldAccount(candidate.key, source);
1999
2175
  }
2000
2176
  if (this._blockedByMinVersion()) {
2001
- if (!invite.memberInvite) {
2177
+ if (!memberInvite) {
2002
2178
  await this.local.setAccountMesh(deviceId, candidate.key);
2003
2179
  }
2004
- await this._retireMesh(candidate).catch((err) => this._log?.warn(['p2p', 'mesh'], 'withholding the joined mesh failed', err));
2180
+ void appended
2181
+ .then(() => this._retireMesh(candidate))
2182
+ .catch((err) => this._log?.warn(['p2p', 'mesh'], 'withholding the joined mesh failed', err));
2005
2183
  return null;
2006
2184
  }
2007
- if (invite.memberInvite || candidate.compute) {
2185
+ if (memberInvite || candidate.compute) {
2008
2186
  this._candidateMesh = null;
2187
+ if (!candidate.writable) {
2188
+ await this.local.markAwaitingAdmission(candidate.key, { awaitingAdmission: true });
2189
+ }
2009
2190
  this._wireMesh(this._poolEntry(candidate));
2010
2191
  this._bringMeshOnline(candidate);
2192
+ if (candidate.writable) {
2193
+ await this.local.markAwaitingAdmission(candidate.key, { awaitingAdmission: false });
2194
+ }
2011
2195
  this._notifyStatus();
2012
2196
  }
2013
2197
  else {
@@ -2017,7 +2201,7 @@ export class Core extends ReadyResource {
2017
2201
  void this._advertiseDeviceProfile(candidate, deviceId);
2018
2202
  void candidate.refreshOurMember().catch(noop);
2019
2203
  void this._settleMembership(candidate, { migrate: true });
2020
- if (invite.memberInvite && !candidate.compute) {
2204
+ if (memberInvite && !candidate.compute) {
2021
2205
  void this._recordJoinedMesh(candidate.key);
2022
2206
  }
2023
2207
  this.emit('mesh-changed', candidate);
@@ -2030,9 +2214,6 @@ export class Core extends ReadyResource {
2030
2214
  }
2031
2215
  const rows = (await this.local.listMeshes()).filter((row) => !b4a.equals(row.key, joined));
2032
2216
  const meshes = rows.filter((row) => !(account && b4a.equals(account, row.key)));
2033
- if (account) {
2034
- await this._writableAccount();
2035
- }
2036
2217
  for (const mesh of meshes) {
2037
2218
  await this._departIfPooled(mesh.key, 'mesh');
2038
2219
  }
@@ -2051,16 +2232,7 @@ export class Core extends ReadyResource {
2051
2232
  return;
2052
2233
  }
2053
2234
  try {
2054
- await Promise.race([
2055
- mesh.waitForWritable(),
2056
- this._closeSignal,
2057
- after(this._accountWritableMs)
2058
- ]);
2059
- if (!mesh.writable) {
2060
- this._log?.warn(['p2p', 'mesh'], `the old ${kind} never went writable, so its writer was not revoked`, { meshId });
2061
- return;
2062
- }
2063
- await this._announceDeparture(mesh);
2235
+ await this._recordDeparture(mesh);
2064
2236
  await this._retirePooledMesh(mesh);
2065
2237
  }
2066
2238
  catch (error) {
@@ -2078,17 +2250,70 @@ export class Core extends ReadyResource {
2078
2250
  async _registerMesh(meshKey, opts) {
2079
2251
  const { shared } = opts;
2080
2252
  const registry = opts.registry ?? (await this._writableAccount());
2081
- if (!registry) {
2082
- throw new Error(MESH_WITHOUT_ACCOUNT);
2083
- }
2084
- const ours = registry.ourMemberId;
2085
- if (!ours) {
2086
- throw new Error(ACCOUNT_MEMBER_UNKNOWN);
2253
+ const ours = registry?.ourMemberId;
2254
+ if (!registry || !ours) {
2255
+ return;
2087
2256
  }
2088
2257
  const now = Date.now();
2089
2258
  await registry.dispatch('put-joined-mesh', {
2090
2259
  room: { meshKey, memberId: ours, joinedAt: now, shared, sharedAt: shared ? now : 0 }
2091
2260
  });
2261
+ await this._backfillMeshSeeds(registry).catch((err) => {
2262
+ this._log?.warn(['p2p', 'mesh'], 'writing the mesh seed failed; writable or settle retries', err);
2263
+ });
2264
+ }
2265
+ async _backfillMeshSeeds(account, { register = true } = {}) {
2266
+ if (!this._isAccount(account))
2267
+ return;
2268
+ const pass = this._backfill.then(() => this._writeMeshSeeds(account, register));
2269
+ this._backfill = pass.catch(noop);
2270
+ await pass;
2271
+ }
2272
+ async _writeMeshSeeds(account, register) {
2273
+ const ours = account.ourMemberId;
2274
+ if (!ours || !this._isAccount(account) || !account.writable) {
2275
+ return;
2276
+ }
2277
+ const batch = account.batch();
2278
+ const registered = register
2279
+ ? await this._registerRecordedMeshes(account, ours, batch)
2280
+ : new Set();
2281
+ for (const { mesh } of this._meshes.values()) {
2282
+ if (await this._seedRowDue(account, mesh, { ours, registered })) {
2283
+ batch.dispatch('put-mesh-seed', { meshKey: mesh.key, seed: mesh.seed });
2284
+ }
2285
+ }
2286
+ await batch.flush();
2287
+ }
2288
+ async _registerRecordedMeshes(account, ours, batch) {
2289
+ const registered = new Set();
2290
+ const now = Date.now();
2291
+ for (const row of await this.local.listMeshes()) {
2292
+ if (b4a.equals(row.key, account.key) || row.compute)
2293
+ continue;
2294
+ const held = await account.view.get(JOINED_MESHES, { meshKey: row.key });
2295
+ if (held) {
2296
+ continue;
2297
+ }
2298
+ batch.dispatch('put-joined-mesh', {
2299
+ room: { meshKey: row.key, memberId: ours, joinedAt: now, shared: false, sharedAt: 0 }
2300
+ });
2301
+ registered.add(b4a.toHex(row.key));
2302
+ }
2303
+ return registered;
2304
+ }
2305
+ async _seedRowDue(account, mesh, { ours, registered }) {
2306
+ if (!mesh.writable || mesh === account || mesh.compute)
2307
+ return false;
2308
+ if (registered.has(this._meshIdOf(mesh))) {
2309
+ return true;
2310
+ }
2311
+ const joined = await account.view.get(JOINED_MESHES, { meshKey: mesh.key });
2312
+ if (!joined?.memberId || !b4a.equals(joined.memberId, ours) || joined.leftAt) {
2313
+ return false;
2314
+ }
2315
+ const held = await account.view.get(MESH_SEEDS, { meshKey: mesh.key });
2316
+ return !held || !b4a.equals(held.seed, mesh.seed);
2092
2317
  }
2093
2318
  async _releaseJoinedMesh(meshKey, registry) {
2094
2319
  try {
@@ -2118,13 +2343,6 @@ export class Core extends ReadyResource {
2118
2343
  const key = await this._storedAccountKey();
2119
2344
  const held = key ? this._pooled(b4a.toHex(key)) : null;
2120
2345
  const mesh = key ? await this._openPooledMesh(b4a.toHex(key)).catch(noop) : null;
2121
- if (mesh && !mesh.writable) {
2122
- await Promise.race([
2123
- mesh.waitForWritable(),
2124
- this._closeSignal,
2125
- after(this._accountWritableMs)
2126
- ]);
2127
- }
2128
2346
  if (mesh && !mesh.ourMemberId) {
2129
2347
  await mesh.refreshOurMember();
2130
2348
  }
@@ -2137,114 +2355,8 @@ export class Core extends ReadyResource {
2137
2355
  }
2138
2356
  return mesh;
2139
2357
  }
2140
- async _awaitGrantRow(mesh, grantKey) {
2141
- await this._untilDeadline(async () => Boolean(await mesh.view.get(MESH_INVITE_GRANTS, { grantKey })));
2142
- }
2143
- async _untilDeadline(done) {
2144
- const deadline = this._joinCancellation?.deadline ?? Date.now();
2145
- for (;;) {
2146
- if (await done())
2147
- return true;
2148
- if (Date.now() >= deadline)
2149
- return false;
2150
- this._throwIfJoinCanceled();
2151
- await after(250);
2152
- }
2153
- }
2154
- async _refuseIfGrantUnusable(candidate, grantSecret, memberId) {
2155
- const grantKey = grantKeyPair(grantSecret).publicKey;
2156
- await this._awaitGrantRow(candidate, grantKey);
2157
- await this._untilDeadline(async () => {
2158
- await candidate.autobee.update();
2159
- const status = await syncStatusSnapshot(candidate);
2160
- return !!status.devices?.length && status.state === 'synced';
2161
- });
2162
- await this._refuseUnredeemable(candidate, grantKey, memberId);
2163
- }
2164
- async _refuseUnredeemable(mesh, grantKey, memberId) {
2165
- const deviceId = this._requireDeviceKeyPair().publicKey;
2166
- const snap = mesh.view.snapshot();
2167
- try {
2168
- const grant = await snap.get(MESH_INVITE_GRANTS, { grantKey });
2169
- if (!grant) {
2170
- return;
2171
- }
2172
- const issuer = grant.issuerDeviceId && (await snap.get(DEVICES, { id: grant.issuerDeviceId }));
2173
- const spent = await snap.get(MESH_INVITE_SPENDS, {
2174
- grantKey,
2175
- deviceId
2176
- });
2177
- const own = await snap.get(DEVICES, { id: deviceId });
2178
- const live = own && !own.revoked;
2179
- const expired = !!grant.expiresAt && Date.now() > grant.expiresAt;
2180
- if (expired && (!spent || grant.revokedAt)) {
2181
- throw new Error(INVITE_GRANT_EXPIRED);
2182
- }
2183
- if (grant.revokedAt || !issuer || issuer.revoked) {
2184
- throw new Error(INVITE_GRANT_REVOKED);
2185
- }
2186
- if (spent && !live) {
2187
- throw new Error(INVITE_GRANT_EXHAUSTED);
2188
- }
2189
- if (spent) {
2190
- return;
2191
- }
2192
- if (grant.maxUses && (grant.uses ?? 0) >= grant.maxUses) {
2193
- throw new Error(INVITE_GRANT_EXHAUSTED);
2194
- }
2195
- if (memberId && (await belongsToAnotherMember(snap, own ?? null, memberId))) {
2196
- throw new Error(DEVICE_OF_ANOTHER_MEMBER);
2197
- }
2198
- if (live) {
2199
- throw new Error(MESH_ALREADY_JOINED);
2200
- }
2201
- const standing = memberId &&
2202
- (await inviteStanding(snap, {
2203
- device: own ?? null,
2204
- memberId,
2205
- grant,
2206
- compute: mesh.compute
2207
- }));
2208
- if (standing === 'present') {
2209
- throw new Error(MEMBER_ALREADY_IN_MESH);
2210
- }
2211
- if (standing === 'removed') {
2212
- throw new Error(MEMBER_REMOVED_SINCE_INVITE);
2213
- }
2214
- }
2215
- finally {
2216
- await snap.close();
2217
- }
2218
- }
2219
- async _refuseFromOwnCopy(invite, grantKey) {
2220
- if (this._pooled(b4a.toHex(invite.baseKey)))
2221
- return;
2222
- const recorded = await this.local.readMeshSession(invite.baseKey);
2223
- const archived = (await this.local.listArchivedMeshes()).find((row) => b4a.equals(row.key, invite.baseKey));
2224
- const writerSeed = recorded?.writerSeed ?? archived?.writerSeed;
2225
- if (!writerSeed) {
2226
- return;
2227
- }
2228
- const copy = new Mesh(this.store, {
2229
- seed: invite.seed,
2230
- key: invite.baseKey,
2231
- deviceId: this._requireDeviceKeyPair().publicKey,
2232
- writerSeed,
2233
- logger: this._log
2234
- });
2235
- try {
2236
- await copy.ready();
2237
- await this._refuseUnredeemable(copy, grantKey, null);
2238
- }
2239
- finally {
2240
- await copy.close();
2241
- }
2242
- }
2243
2358
  async _openCandidateMesh(invite, session) {
2244
2359
  this._throwIfJoinCanceled();
2245
- if (invite.memberInvite && invite.inviteNonce) {
2246
- await this._refuseFromOwnCopy(invite, grantKeyPair(invite.inviteNonce).publicKey);
2247
- }
2248
2360
  const candidate = new Mesh(this.store, {
2249
2361
  seed: session.seed,
2250
2362
  key: session.baseKey ?? null,
@@ -2282,25 +2394,8 @@ export class Core extends ReadyResource {
2282
2394
  await candidate.close().catch(noop);
2283
2395
  throw this._joinCancellation?.canceled ? new Error(MESH_JOIN_CANCELED) : error;
2284
2396
  }
2285
- if (this._swarm) {
2286
- candidate.startBlindPeering(this._swarm.dht);
2287
- }
2397
+ this._startBlindPeering(candidate);
2288
2398
  try {
2289
- this._throwIfJoinCanceled();
2290
- const member = invite.memberInvite
2291
- ? await this._joinBirthMember(candidate, this._requireDeviceKeyPair().publicKey)
2292
- : undefined;
2293
- const invited = !member && invite.memberId && invite.inviteNonce;
2294
- const grantSecret = invite.memberInvite ? invite.inviteNonce : undefined;
2295
- if (grantSecret) {
2296
- await this._refuseIfGrantUnusable(candidate, grantSecret, member?.member.id ?? null);
2297
- }
2298
- await candidate.appendBirthOp(this._requireDeviceKeyPair(), this._localDevice ?? undefined, {
2299
- member,
2300
- grantSecret,
2301
- recovery: invite.recovery,
2302
- vouched: Boolean(invited)
2303
- });
2304
2399
  this._throwIfJoinCanceled();
2305
2400
  if (!b4a.equals(candidate.key, invite.baseKey)) {
2306
2401
  throw new Error('Device is not permitted to join the invited mesh');
@@ -2308,22 +2403,6 @@ export class Core extends ReadyResource {
2308
2403
  if ((await candidate.checkResumedRevocation()) === 'kicked') {
2309
2404
  throw new Error('Device is not permitted to join the invited mesh');
2310
2405
  }
2311
- if (invite.recovery && (await this._isRemovedFrom(candidate))) {
2312
- throw new Error(RECOVERY_DEVICE_REMOVED);
2313
- }
2314
- this._throwIfJoinCanceled();
2315
- disarm();
2316
- const inviter = invited
2317
- ? this._requestInvitedAdmission(candidate, invite.memberId, invite.inviteNonce)
2318
- : null;
2319
- await this._waitForCandidateWritable(candidate, !invite.recovery).catch((error) => {
2320
- throw inviter && !inviter.answered && error.message === GRANT_JOIN_TIMED_OUT
2321
- ? new Error(INVITER_OFFLINE)
2322
- : error;
2323
- });
2324
- if (!candidate.writable) {
2325
- throw new Error('Device is not permitted to join the invited mesh');
2326
- }
2327
2406
  this._throwIfJoinCanceled();
2328
2407
  return candidate;
2329
2408
  }
@@ -2335,104 +2414,43 @@ export class Core extends ReadyResource {
2335
2414
  disarm();
2336
2415
  }
2337
2416
  }
2338
- _requestInvitedAdmission(candidate, memberId, nonce) {
2339
- const { publicKey: deviceId, secretKey } = this._requireDeviceKeyPair();
2340
- const local = this._localDevice;
2341
- if (!local) {
2342
- throw new Error('local device read before the offline boot');
2417
+ async _candidateBirthMember(candidate, invite) {
2418
+ const deviceId = this._requireDeviceKeyPair().publicKey;
2419
+ if (invite.recovery) {
2420
+ return this._recoveryBirthMember(candidate, invite.recovery);
2421
+ }
2422
+ const { deviceSeed, deviceProof, memberId } = invite;
2423
+ if (deviceSeed && deviceProof && memberId) {
2424
+ return this._delegatedBirthMember(candidate, {
2425
+ deviceSeed,
2426
+ deviceProof,
2427
+ memberId,
2428
+ name: invite.inviterName ?? ''
2429
+ });
2343
2430
  }
2344
- const joinedAt = Date.now();
2345
- const request = {
2346
- writerKey: candidate.localWriterKey,
2347
- joinedAt,
2348
- deviceSignature: candidate.signDeviceClaim(secretKey, deviceId, joinedAt),
2349
- memberSignature: signMemberWriterClaim(secretKey, {
2350
- meshPublicKey: candidate.publicKey,
2351
- deviceId,
2352
- writerKey: candidate.localWriterKey,
2353
- joinedAt,
2354
- memberId
2355
- }),
2356
- inviteToken: deviceInviteToken(nonce, deviceId),
2357
- name: local.name,
2358
- capabilityProfile: local.capabilityProfile,
2359
- recommendedModelName: local.recommendedModelName
2360
- };
2361
- const state = { answered: false };
2362
- const asking = () => this._candidateMesh === candidate && !candidate.writable && !this.closing;
2363
- const ask = (inviter) => this._meshSeedHandoff.admit(inviter, candidate.key, request).then((admitted) => {
2364
- state.answered = true;
2365
- return admitted;
2366
- }, (error) => {
2367
- if (![HOST_UNREACHABLE, HOST_SILENT, SEED_REFUSED].includes(error.message)) {
2368
- state.answered = true;
2369
- }
2370
- return false;
2371
- });
2372
- void (async () => {
2373
- while (asking()) {
2374
- for (const inviter of await this._liveSiblings(candidate, memberId)) {
2375
- if (!asking() || (await ask(inviter)))
2376
- return;
2377
- }
2378
- await Promise.race([after(INVITER_RETRY_MS), this._closeSignal]);
2379
- }
2380
- })().catch(noop);
2381
- return state;
2431
+ return invite.memberInvite ? this._joinBirthMember(candidate, deviceId) : undefined;
2382
2432
  }
2383
- async _isRemovedFrom(mesh) {
2384
- const device = await mesh.view
2385
- .get(DEVICES, { id: this._requireDeviceKeyPair().publicKey })
2386
- .catch(noop);
2387
- return device ? isKickedRow(device) : false;
2433
+ async _delegatedBirthMember(mesh, { deviceSeed, deviceProof, memberId, name }) {
2434
+ const keyPair = this._requireDeviceKeyPair();
2435
+ const proof = await attestDeviceProof(keyPair.publicKey, crypto.keyPair(deviceSeed), deviceProof);
2436
+ const attestation = claimAttestation(mesh.publicKey, keyPair.publicKey, mesh.localWriterKey);
2437
+ await this.local.recordMemberProof({ meshKey: mesh.key, memberId, proof, attestedAt: 0 });
2438
+ return {
2439
+ member: { id: memberId, name, joinedAt: Date.now(), updatedAt: 0 },
2440
+ proof: await attestMemberClaim(attestation, keyPair, proof)
2441
+ };
2388
2442
  }
2389
- async _waitForCandidateWritable(candidate, bounded) {
2390
- if (candidate.writable)
2391
- return;
2392
- await new Promise((resolve, reject) => {
2393
- let offClose = noop;
2394
- const cleanup = () => {
2395
- clearTimeout(timer);
2396
- candidate.autobee.off('writable', onWritable);
2397
- candidate.autobee.off('interrupt', onInterrupt);
2398
- candidate.off('kicked', onKicked);
2399
- offClose();
2400
- if (this._cancelCandidateJoin === onCanceled) {
2401
- this._cancelCandidateJoin = null;
2402
- }
2403
- };
2404
- const onWritable = () => {
2405
- cleanup();
2406
- resolve();
2407
- };
2408
- const onKicked = () => {
2409
- cleanup();
2410
- reject(new Error('Device is not permitted to join the invited mesh'));
2411
- };
2412
- const onInterrupt = (reason) => {
2413
- cleanup();
2414
- reject(reason instanceof Error ? reason : new Error('Apply interrupted'));
2415
- };
2416
- const onCanceled = () => {
2417
- cleanup();
2418
- reject(new Error(MESH_JOIN_CANCELED));
2419
- };
2420
- const timer = bounded
2421
- ? setTimeout(() => {
2422
- cleanup();
2423
- reject(new Error(GRANT_JOIN_TIMED_OUT));
2424
- }, this._grantWritableMs)
2425
- : undefined;
2426
- this._cancelCandidateJoin = onCanceled;
2427
- candidate.autobee.on('writable', onWritable);
2428
- candidate.autobee.on('interrupt', onInterrupt);
2429
- candidate.on('kicked', onKicked);
2430
- offClose = this._onClose(onCanceled);
2431
- });
2443
+ _onMeshSeedDue(mesh) {
2444
+ const account = this._accountMesh();
2445
+ if (account) {
2446
+ void this._backfillMeshSeeds(account, { register: mesh === account }).catch(noop);
2447
+ }
2432
2448
  }
2433
2449
  _onMeshWritable(mesh) {
2434
2450
  if (!this._isPooled(mesh))
2435
2451
  return;
2452
+ void this.local.markAwaitingAdmission(mesh.key, { awaitingAdmission: false }).catch(noop);
2453
+ this._onMeshSeedDue(mesh);
2436
2454
  if (this._isAccount(mesh)) {
2437
2455
  this._kicked = false;
2438
2456
  }
@@ -2443,7 +2461,7 @@ export class Core extends ReadyResource {
2443
2461
  return;
2444
2462
  this._log?.error(['p2p', 'mesh'], 'mesh autobee died - retiring it', err);
2445
2463
  if (this._isAccount(mesh)) {
2446
- this._statusError = statusErrorMessage('the local database died', err);
2464
+ this._statusError = statusErrorMessage(MESH_DIED, err);
2447
2465
  }
2448
2466
  this._retirePooledMesh(mesh).catch((retireErr) => {
2449
2467
  this._log?.warn(['p2p', 'mesh'], 'retiring the dead mesh failed', retireErr);
@@ -2467,10 +2485,14 @@ export class Core extends ReadyResource {
2467
2485
  entry.handlers = handlers;
2468
2486
  mesh.on('kicked', () => this._onKicked(mesh));
2469
2487
  mesh.on('unattested-device', () => void this._settleMembership(mesh));
2488
+ if (mesh.writable) {
2489
+ this._onMeshSeedDue(mesh);
2490
+ }
2470
2491
  mesh.on('members-dirty', () => void this._revalidateLoans());
2471
2492
  mesh.on('members-dirty', () => {
2472
2493
  if (!this._isAccount(mesh))
2473
2494
  return;
2495
+ void this._dialSiblings(mesh).catch(noop);
2474
2496
  void this._followAccountName(mesh);
2475
2497
  void this._revokeInMeshes();
2476
2498
  });
@@ -2479,7 +2501,6 @@ export class Core extends ReadyResource {
2479
2501
  void this._followJoinedMeshes(mesh);
2480
2502
  }
2481
2503
  });
2482
- mesh.on('mesh-kind', () => void this._refuseSealedReader(mesh));
2483
2504
  mesh.on('membership-tallied', () => this._notifyStatus());
2484
2505
  mesh.on('mesh-named', () => this._notifyStatus());
2485
2506
  mesh.on('membership', (membership) => {
@@ -2500,6 +2521,7 @@ export class Core extends ReadyResource {
2500
2521
  await this.local.setAccountMesh(this._requireDeviceKeyPair().publicKey, mesh.key);
2501
2522
  this._wireMesh(this._poolEntry(mesh));
2502
2523
  this._accountId = this._meshIdOf(mesh);
2524
+ void this._dialSiblings(mesh).catch(noop);
2503
2525
  this._statusError = null;
2504
2526
  this._upgradeRequired = null;
2505
2527
  void this._closeConfigPeering().catch(noop);
@@ -2507,24 +2529,6 @@ export class Core extends ReadyResource {
2507
2529
  this._kicked = false;
2508
2530
  }
2509
2531
  this._notifyStatus();
2510
- void this._refuseSealedReader(mesh);
2511
- }
2512
- async _refuseSealedReader(mesh) {
2513
- if (!this._isAccount(mesh) || mesh.writable)
2514
- return;
2515
- const kind = await mesh.view.get(MESH_KINDS, { id: 'mesh' }).catch(noop);
2516
- if (kind?.kind !== 'account') {
2517
- return;
2518
- }
2519
- const own = await mesh.view.get(DEVICES, {
2520
- id: this._requireDeviceKeyPair().publicKey
2521
- });
2522
- if (own) {
2523
- return;
2524
- }
2525
- this._statusError = SEALED_ACCOUNT_READER;
2526
- this._log?.error(['p2p', 'mesh'], SEALED_ACCOUNT_READER);
2527
- this._notifyStatus();
2528
2532
  }
2529
2533
  _pooled(meshKey) {
2530
2534
  if (meshKey === null)
@@ -2797,6 +2801,13 @@ export class Core extends ReadyResource {
2797
2801
  }
2798
2802
  await candidate.close().catch(noop);
2799
2803
  }
2804
+ async _recordDeparture(mesh) {
2805
+ const pending = !mesh.writable && (mesh.localLength > 0 || mesh.joining);
2806
+ if (pending || (await this._departureHasAudience(mesh))) {
2807
+ await this.local.recordDeparture(mesh.key, { creator: mesh.minted });
2808
+ }
2809
+ await this._announceDeparture(mesh);
2810
+ }
2800
2811
  async _departureHasAudience(mesh) {
2801
2812
  if (!mesh.writable)
2802
2813
  return false;
@@ -2859,10 +2870,7 @@ export class Core extends ReadyResource {
2859
2870
  return;
2860
2871
  }
2861
2872
  }
2862
- if (await this._departureHasAudience(departing)) {
2863
- await this.local.recordDeparture(departing.key, { creator: departing.minted });
2864
- }
2865
- await this._announceDeparture(departing);
2873
+ await this._recordDeparture(departing);
2866
2874
  await this._releaseJoinedMesh(departing.key);
2867
2875
  await this._archiveMembership(departing);
2868
2876
  if (!leavingAccount) {
@@ -2885,13 +2893,7 @@ export class Core extends ReadyResource {
2885
2893
  async _departOnLogout(key) {
2886
2894
  try {
2887
2895
  const mesh = await this._openPooledMesh(b4a.toHex(key));
2888
- if (!(await this.local.awaitsAdmission(key))) {
2889
- await mesh.waitForWritable(this._accountWritableMs);
2890
- }
2891
- if (await this._departureHasAudience(mesh)) {
2892
- await this.local.recordDeparture(mesh.key, { creator: mesh.minted });
2893
- }
2894
- await this._announceDeparture(mesh);
2896
+ await this._recordDeparture(mesh);
2895
2897
  await this._archiveMembership(mesh);
2896
2898
  await this._retirePooledMesh(mesh);
2897
2899
  }
@@ -3159,10 +3161,10 @@ export class Core extends ReadyResource {
3159
3161
  throw new Error('Mesh transition already in progress');
3160
3162
  }
3161
3163
  const registry = opts.registry ?? (await this._writableAccount());
3162
- if (!registry) {
3164
+ if (shared && !registry) {
3163
3165
  throw new Error(MESH_WITHOUT_ACCOUNT);
3164
3166
  }
3165
- if (!registry.ourMemberId) {
3167
+ if (registry && !registry.ourMemberId) {
3166
3168
  throw new Error(ACCOUNT_MEMBER_UNKNOWN);
3167
3169
  }
3168
3170
  const mesh = await this._openMesh({ new: true, sharing: true, compute });
@@ -3173,7 +3175,9 @@ export class Core extends ReadyResource {
3173
3175
  if (!mesh.ourMemberId) {
3174
3176
  throw new Error(MESH_WITHOUT_MEMBER);
3175
3177
  }
3176
- await this._registerMesh(mesh.key, { shared, registry });
3178
+ if (registry) {
3179
+ await this._registerMesh(mesh.key, { shared, registry });
3180
+ }
3177
3181
  }
3178
3182
  catch (error) {
3179
3183
  await this._discardMesh(mesh);
@@ -3265,7 +3269,13 @@ export class Core extends ReadyResource {
3265
3269
  throw halted;
3266
3270
  }
3267
3271
  this._wireMesh(this._poolEntry(mesh));
3272
+ if (this._isAccount(mesh) && this._statusError?.startsWith(MESH_DIED)) {
3273
+ this._statusError = null;
3274
+ }
3268
3275
  this._bringMeshOnline(mesh);
3276
+ if (mesh.writable) {
3277
+ void this.local.markAwaitingAdmission(mesh.key, { awaitingAdmission: false }).catch(noop);
3278
+ }
3269
3279
  this._notifyStatus();
3270
3280
  this._debugOnMeshOpened?.(mesh, meshId);
3271
3281
  return mesh;
@@ -3276,7 +3286,6 @@ export class Core extends ReadyResource {
3276
3286
  const account = !!accountKey && b4a.equals(accountKey, row.key);
3277
3287
  const joined = meshes.get(meshId);
3278
3288
  const state = this._meshEntryState(mesh, account);
3279
- const admission = state === 'joining' ? await this._admission(mesh) : {};
3280
3289
  return {
3281
3290
  meshId,
3282
3291
  state,
@@ -3288,33 +3297,7 @@ export class Core extends ReadyResource {
3288
3297
  members: mesh?.membership.members ?? 0,
3289
3298
  shared: joined?.shared ?? false,
3290
3299
  sharedAt: joined?.shared ? joined.sharedAt : undefined,
3291
- compute: row.compute ?? false,
3292
- reason: admission.reason,
3293
- awaitingFrom: admission.awaitingFrom
3294
- };
3295
- }
3296
- async _admission(mesh) {
3297
- if (!mesh || mesh.writable)
3298
- return {};
3299
- const ours = mesh.localWriterKey;
3300
- const admitters = [];
3301
- try {
3302
- for await (const device of mesh.view.find(DEVICES)) {
3303
- if (device.revoked || (device.writerKey && b4a.equals(device.writerKey, ours))) {
3304
- continue;
3305
- }
3306
- admitters.push(device.name);
3307
- if (admitters.length > 1) {
3308
- break;
3309
- }
3310
- }
3311
- }
3312
- catch {
3313
- return { reason: AWAITING_ADMISSION };
3314
- }
3315
- return {
3316
- reason: AWAITING_ADMISSION,
3317
- awaitingFrom: admitters.length === 1 ? admitters[0] : undefined
3300
+ compute: row.compute ?? false
3318
3301
  };
3319
3302
  }
3320
3303
  _meshEntryState(mesh, account) {
@@ -3336,9 +3319,15 @@ export class Core extends ReadyResource {
3336
3319
  throw new Error('this device holds no account mesh');
3337
3320
  }
3338
3321
  const paired = await this._accountMesh()?.view.get(DEVICES, { id: deviceId });
3322
+ const row = await target.view.get(DEVICES, { id: deviceId });
3339
3323
  const revokedAt = Math.max(Date.now(), (paired?.joinedAt ?? 0) + 1);
3340
3324
  await target.dispatch('remove-writer', { writerKey, deviceId, revokedAt });
3341
3325
  await target.autobee.update();
3326
+ const self = this._requireDeviceKeyPair().publicKey;
3327
+ const ours = target === this._accountMesh() || ownedBy(row, target.ourMemberId);
3328
+ if (row && ours && !b4a.equals(deviceId, self)) {
3329
+ await rehomeAgents(target, deviceId, self).catch((err) => this._log?.warn(['p2p', 'mesh'], "handing over a removed device's agents failed", err));
3330
+ }
3342
3331
  if (target === this._accountMesh()) {
3343
3332
  this._sweepUnknownEverywhere = true;
3344
3333
  }
@@ -3399,6 +3388,23 @@ export class Core extends ReadyResource {
3399
3388
  });
3400
3389
  }
3401
3390
  }
3391
+ async _rehomeRemoved(account, mesh, self) {
3392
+ if (!mesh.writable)
3393
+ return;
3394
+ const removed = [];
3395
+ for await (const row of mesh.view.find(DEVICES)) {
3396
+ const ours = mesh === account || ownedBy(row, mesh.ourMemberId);
3397
+ if (row.revoked && ours && !b4a.equals(row.id, self)) {
3398
+ removed.push(row.id);
3399
+ }
3400
+ }
3401
+ for (const id of removed) {
3402
+ const device = await account.view.get(DEVICES, { id });
3403
+ if (device?.revokedBy && b4a.equals(device.revokedBy, account.localWriterKey)) {
3404
+ await rehomeAgents(mesh, id, self).catch((err) => this._log?.warn(['p2p', 'mesh'], "handing over a removed device's agents failed", err));
3405
+ }
3406
+ }
3407
+ }
3402
3408
  async _revokeInMeshesOnce() {
3403
3409
  try {
3404
3410
  const account = this._accountMesh();
@@ -3410,6 +3416,7 @@ export class Core extends ReadyResource {
3410
3416
  await this._sweepEveryOwnMesh(account, account.ourMemberId);
3411
3417
  }
3412
3418
  const self = this._requireDeviceKeyPair().publicKey;
3419
+ await this._rehomeRemoved(account, account, self);
3413
3420
  for (const { mesh } of [...this._meshes.values()]) {
3414
3421
  if (mesh === account || !mesh.writable)
3415
3422
  continue;
@@ -3424,6 +3431,7 @@ export class Core extends ReadyResource {
3424
3431
  const meshId = b4a.toHex(mesh.key);
3425
3432
  const unknownToo = this._sweepUnknownIn.has(meshId);
3426
3433
  await this._revokeStaleRows(account, mesh, { self, ours: mesh.ourMemberId, unknownToo });
3434
+ await this._rehomeRemoved(account, mesh, self);
3427
3435
  this._sweepUnknownIn.delete(meshId);
3428
3436
  }
3429
3437
  }
@@ -3547,24 +3555,6 @@ export class Core extends ReadyResource {
3547
3555
  throw new Error(DEVICE_INVITE_MESH_REFUSED);
3548
3556
  }
3549
3557
  }
3550
- let grantSecret;
3551
- if (opts.memberInvite) {
3552
- grantSecret = crypto.randomBytes(32);
3553
- const now = Date.now();
3554
- const terms = { expiresAt: now + GRANT_DEFAULT_TTL_MS, maxUses: GRANT_DEFAULT_MAX_USES };
3555
- const refusal = inviteGrantTermsRefusal(terms, now);
3556
- if (refusal) {
3557
- throw new Error(refusal);
3558
- }
3559
- await mesh.dispatch('put-mesh-invite-grant', {
3560
- grant: {
3561
- grantKey: grantKeyPair(grantSecret).publicKey,
3562
- kind: 'member',
3563
- ...terms,
3564
- updatedAt: now
3565
- }
3566
- });
3567
- }
3568
3558
  const auto = mesh.autobee;
3569
3559
  const info = await auto.system.getInfo();
3570
3560
  const head = info && info.view.length > 0 ? await auto.trusted.mostRecentTrusted(auto.view, null) : null;
@@ -3572,6 +3562,7 @@ export class Core extends ReadyResource {
3572
3562
  ? this._swarm.dht.toArray({ limit: INVITE_DHT_NODES_LIMIT })
3573
3563
  : [];
3574
3564
  const memberId = opts.memberInvite ? undefined : (mesh.ourMemberId ?? undefined);
3565
+ const delegation = memberId ? await this._delegatePairing(mesh) : null;
3575
3566
  const inviterName = memberId
3576
3567
  ? ((await mesh.view.get(MEMBERS, { id: memberId }))?.name ?? undefined)
3577
3568
  : undefined;
@@ -3595,11 +3586,26 @@ export class Core extends ReadyResource {
3595
3586
  : this._resolveBlindPeerKeys().slice(0, INVITE_BLIND_PEERS_LIMIT),
3596
3587
  memberInvite: opts.memberInvite ?? false,
3597
3588
  memberId,
3598
- inviteNonce: grantSecret ?? (memberId ? await this._deviceInvites.issue() : undefined),
3599
3589
  compute: mesh.compute,
3600
- inviterName
3590
+ inviterName,
3591
+ deviceSeed: delegation?.deviceSeed,
3592
+ deviceProof: delegation?.deviceProof
3601
3593
  };
3602
3594
  }
3595
+ async _delegatePairing(account) {
3596
+ const keyPair = this._requireDeviceKeyPair();
3597
+ const own = await account.view.get(DEVICES, { id: keyPair.publicKey });
3598
+ if (!own?.memberProof) {
3599
+ return null;
3600
+ }
3601
+ const deviceSeed = crypto.randomBytes(32);
3602
+ const delegate = crypto.keyPair(deviceSeed);
3603
+ const deviceProof = await attestDeviceProof(delegate.publicKey, keyPair, own.memberProof);
3604
+ if (!(await delegationFits(deviceProof, delegate))) {
3605
+ throw new Error(DEVICE_INVITE_CHAIN_TOO_LONG);
3606
+ }
3607
+ return { deviceSeed, deviceProof };
3608
+ }
3603
3609
  previewMeshInvite(invite) {
3604
3610
  const decoded = decodeMeshInvite(invite);
3605
3611
  if (!decoded) {
@@ -3607,11 +3613,16 @@ export class Core extends ReadyResource {
3607
3613
  }
3608
3614
  return { memberInvite: decoded.memberInvite ?? false, inviterName: decoded.inviterName };
3609
3615
  }
3616
+ _leaveSiblings(swarm) {
3617
+ for (const key of this._siblings.values()) {
3618
+ swarm.leavePeer(key);
3619
+ }
3620
+ this._siblings.clear();
3621
+ }
3610
3622
  async _closeSwarm() {
3611
3623
  if (this._ownsWakeup) {
3612
3624
  this._wakeup.destroy();
3613
3625
  }
3614
- this._meshSeedHandoff.destroy();
3615
3626
  this._harnessLoan.destroy();
3616
3627
  this._secretHandoff.destroy();
3617
3628
  this._skillOverlayHandoff.destroy();
@@ -3627,6 +3638,7 @@ export class Core extends ReadyResource {
3627
3638
  detach();
3628
3639
  }
3629
3640
  this._peers.clear();
3641
+ this._leaveSiblings(swarm);
3630
3642
  await Promise.all([...this._meshes.values()].map(({ mesh }) => swarm.leave(mesh.discoveryKey)));
3631
3643
  return;
3632
3644
  }
@@ -3725,17 +3737,16 @@ export class Core extends ReadyResource {
3725
3737
  return;
3726
3738
  }
3727
3739
  if (held) {
3728
- await this._requestMeshAdmission(memberMesh, held, ours);
3729
3740
  return;
3730
3741
  }
3731
3742
  if (await this.local.readMeshSession(joined.meshKey)) {
3732
3743
  if (this._upgradeRequired !== 'halt' &&
3733
3744
  (await this.local.awaitsAdmission(joined.meshKey))) {
3734
- await this._requestMeshAdmission(memberMesh, await this._openPooledMesh(meshId), ours);
3745
+ await this._openPooledMesh(meshId);
3735
3746
  }
3736
3747
  return;
3737
3748
  }
3738
- await this._openFollowedMesh(memberMesh, joined.meshKey, ours);
3749
+ await this._openFollowedMesh(memberMesh, joined);
3739
3750
  });
3740
3751
  if (failed && !joined.leftAt) {
3741
3752
  pending = true;
@@ -3764,10 +3775,7 @@ export class Core extends ReadyResource {
3764
3775
  if (this._leaving || this._minting)
3765
3776
  return;
3766
3777
  try {
3767
- if (await this._departureHasAudience(mesh)) {
3768
- await this.local.recordDeparture(mesh.key, { creator: mesh.minted });
3769
- }
3770
- await this._announceDeparture(mesh);
3778
+ await this._recordDeparture(mesh);
3771
3779
  if (this._isAccount(mesh)) {
3772
3780
  await this._replaceRevokedAccount(mesh);
3773
3781
  }
@@ -3786,226 +3794,26 @@ export class Core extends ReadyResource {
3786
3794
  await this.local.archiveMesh(meshKey);
3787
3795
  this._notifyStatus();
3788
3796
  }
3789
- async _openOwnMesh(invite) {
3790
- const meshKey = invite.baseKey;
3791
- const meshId = b4a.toHex(meshKey);
3792
- if (!invite.inviteNonce) {
3793
- return false;
3794
- }
3795
- const grantKey = grantKeyPair(invite.inviteNonce).publicKey;
3796
- if (await this.local.readMeshSession(meshKey)) {
3797
- return this._adoptOwnMesh(await this._openPooledMesh(meshId), grantKey);
3798
- }
3799
- const registry = await this._accountMesh();
3800
- const ours = registry?.ourMemberId;
3801
- if (!registry || !ours || !(await this._isOwnMeshIn(registry, meshKey))) {
3802
- return false;
3803
- }
3804
- if (this._blockedByMinVersion()) {
3805
- throw new Error(MESH_BELOW_FLOOR);
3806
- }
3807
- this._throwIfJoinCanceled();
3808
- await this._openFollowedMesh(registry, meshKey, ours).catch((error) => {
3809
- this._log?.warn(['p2p', 'mesh'], 'opening a mesh of ours through a sibling failed', error);
3810
- });
3811
- if (this.closing) {
3812
- throw new Error('Core is closing');
3813
- }
3814
- this._throwIfJoinCanceled();
3815
- const mesh = this._pooled(meshId);
3816
- return mesh ? this._adoptOwnMesh(mesh, grantKey) : false;
3817
- }
3818
- async _adoptOwnMesh(mesh, grantKey) {
3819
- const id = this._requireDeviceKeyPair().publicKey;
3820
- const ours = this._accountMesh()?.ourMemberId ?? null;
3821
- await this._awaitGrantRow(mesh, grantKey);
3822
- let refused = false;
3823
- const settled = await this._untilDeadline(async () => {
3824
- const own = await mesh.view.get(DEVICES, { id });
3825
- if (own && !own.revoked && own.writerKey && b4a.equals(own.writerKey, mesh.localWriterKey)) {
3826
- return true;
3827
- }
3828
- refused = await this._refusedInMesh(mesh, grantKey, ours);
3829
- return refused;
3830
- });
3831
- if (refused) {
3832
- const own = await mesh.view.get(DEVICES, { id });
3833
- await this._refuseUnredeemable(mesh, grantKey, own?.memberId ?? mesh.ourMemberId);
3834
- await this._retirePooledMesh(mesh);
3835
- await this.local.archiveMesh(mesh.key);
3836
- return false;
3837
- }
3838
- if (!settled) {
3839
- throw new Error(MESH_ADMISSION_PENDING);
3840
- }
3841
- return true;
3842
- }
3843
- async _refusedInMesh(mesh, grantKey, ours) {
3844
- await mesh.autobee.update();
3845
- const status = await syncStatusSnapshot(mesh);
3846
- if (!status.devices?.length || status.state !== 'synced') {
3847
- return false;
3848
- }
3849
- const self = this._requireDeviceKeyPair().publicKey;
3850
- const own = await mesh.view.get(DEVICES, { id: self });
3851
- if (own?.revoked &&
3852
- !(own.revokedBy && b4a.equals(own.revokedBy, own.writerKey ?? b4a.alloc(0)))) {
3853
- return true;
3854
- }
3855
- if (!ours) {
3856
- return false;
3857
- }
3858
- const grant = await mesh.view.get(MESH_INVITE_GRANTS, { grantKey });
3859
- if (grant && (await memberStanding(mesh.view, ours, grant)) === 'removed') {
3860
- return true;
3861
- }
3862
- for await (const device of mesh.view.find(DEVICES)) {
3863
- if (!device.revoked &&
3864
- device.memberId &&
3865
- b4a.equals(device.memberId, ours) &&
3866
- !b4a.equals(device.id, self)) {
3867
- return false;
3868
- }
3869
- }
3870
- return true;
3871
- }
3872
- async _openFollowedMesh(memberMesh, meshKey, ours) {
3873
- if (this._blockedByMinVersion())
3874
- return;
3875
- const seed = await this._requestMeshSeed(memberMesh, meshKey, ours);
3876
- if (!seed || this.closing) {
3797
+ async _openFollowedMesh(memberMesh, joined) {
3798
+ if (this._blockedByMinVersion() || this._upgradeRequired || !this._mayFollow(memberMesh)) {
3877
3799
  return;
3878
3800
  }
3879
- const moving = this._joinCancellation ? this._joinCancellation.account : this._minting;
3880
- const leaving = this._leaving === this._meshIdOf(memberMesh);
3881
- if (moving || leaving || !this._isAccount(memberMesh)) {
3801
+ const { meshKey } = joined;
3802
+ const held = await memberMesh.view.get(MESH_SEEDS, { meshKey });
3803
+ if (!held || this.closing) {
3882
3804
  return;
3883
3805
  }
3884
- const session = this.local.createCandidateSession(seed, meshKey);
3806
+ const session = this.local.createCandidateSession(held.seed, meshKey);
3885
3807
  await this.local.recordMesh(meshKey, session, {
3886
3808
  deviceId: this._requireDeviceKeyPair().publicKey
3887
3809
  });
3888
3810
  await this.local.markAwaitingAdmission(meshKey, { awaitingAdmission: true });
3889
- let mesh;
3890
- try {
3891
- mesh = await this._openPooledMesh(b4a.toHex(meshKey));
3892
- }
3893
- catch (error) {
3894
- if (this._upgradeRequired !== 'halt') {
3895
- await this.local.archiveMesh(meshKey).catch(noop);
3896
- }
3897
- throw error;
3898
- }
3899
- await this._requestMeshAdmission(memberMesh, mesh, ours);
3900
- }
3901
- async _requestMeshSeed(memberMesh, meshKey, ours) {
3902
- let unanswered = null;
3903
- for (const sibling of await this._liveSiblings(memberMesh, ours)) {
3904
- if (this.closing)
3905
- return null;
3906
- const seed = await this._meshSeedHandoff.request(sibling, meshKey).catch((error) => {
3907
- this._log?.warn(['p2p', 'mesh'], 'a sibling did not hand over a mesh seed', error);
3908
- if (UNANSWERED_SEED_REQUESTS.has(error.message)) {
3909
- unanswered = error;
3910
- }
3911
- });
3912
- if (seed) {
3913
- return seed;
3914
- }
3915
- }
3916
- if (unanswered) {
3917
- throw unanswered;
3918
- }
3919
- return null;
3811
+ await this._openPooledMesh(b4a.toHex(meshKey));
3920
3812
  }
3921
- async _requestMeshAdmission(memberMesh, mesh, ours) {
3922
- if (mesh.writable) {
3923
- return this.local.markAwaitingAdmission(mesh.key, { awaitingAdmission: false });
3924
- }
3925
- const { publicKey: deviceId, secretKey } = this._requireDeviceKeyPair();
3926
- const own = await mesh.view.get(DEVICES, { id: deviceId });
3927
- if (own && (await this._removedFor(mesh, own))) {
3928
- return this.local.markAwaitingAdmission(mesh.key, { awaitingAdmission: false });
3929
- }
3930
- const binding = {
3931
- meshPublicKey: mesh.publicKey,
3932
- deviceId,
3933
- writerKey: mesh.localWriterKey,
3934
- joinedAt: Date.now()
3935
- };
3936
- const claim = {
3937
- writerKey: binding.writerKey,
3938
- joinedAt: binding.joinedAt,
3939
- deviceSignature: signWriterClaim(secretKey, binding),
3940
- memberSignature: signMemberWriterClaim(secretKey, { ...binding, memberId: ours })
3941
- };
3942
- const known = await mesh.bornKind();
3943
- const deferred = known !== null && (await this._askSiblingsToAdmit(memberMesh, mesh, { ours, claim }));
3944
- if (this.closing) {
3945
- return;
3946
- }
3947
- if (!mesh.writable && (await mesh.waitForGenesis(this._accountWritableMs)) === null) {
3948
- await mesh.appendBirthOp(this._requireDeviceKeyPair(), this._localDevice ?? undefined);
3949
- await mesh.waitForWritable(this._accountWritableMs);
3950
- }
3951
- const meshId = this._meshIdOf(mesh);
3952
- if (deferred && !mesh.writable) {
3953
- this._retryAdmission(memberMesh, meshId);
3954
- return;
3955
- }
3956
- this._admissionRetries.delete(meshId);
3957
- if (mesh.writable) {
3958
- await this.local.markAwaitingAdmission(mesh.key, { awaitingAdmission: false });
3959
- }
3960
- }
3961
- async _askSiblingsToAdmit(memberMesh, mesh, { ours, claim }) {
3962
- let deferred = false;
3963
- for (const sibling of await this._liveSiblings(memberMesh, ours)) {
3964
- if (this.closing || mesh.writable)
3965
- break;
3966
- const admitted = await this._meshSeedHandoff
3967
- .admit(sibling, mesh.key, claim)
3968
- .catch((error) => {
3969
- deferred ||= error.message === ADMISSION_DEFERRED;
3970
- return false;
3971
- });
3972
- if (admitted) {
3973
- await mesh.waitForWritable(this._accountWritableMs);
3974
- }
3975
- }
3976
- return deferred && !mesh.writable;
3977
- }
3978
- _retryAdmission(memberMesh, meshId) {
3979
- const tries = this._admissionRetries.get(meshId) ?? 0;
3980
- if (tries >= ADMISSION_RETRIES) {
3981
- return;
3982
- }
3983
- this._admissionRetries.set(meshId, tries + 1);
3984
- const memberId = this._meshIdOf(memberMesh);
3985
- if (this._admissionRetryPending.has(memberId)) {
3986
- return;
3987
- }
3988
- this._admissionRetryPending.add(memberId);
3989
- Promise.race([after(INVITER_RETRY_MS), this._closeSignal])
3990
- .then(() => {
3991
- this._admissionRetryPending.delete(memberId);
3992
- const current = this.closing ? null : this._pooled(memberId);
3993
- return current ? this._followJoinedMeshes(current) : undefined;
3994
- })
3995
- .catch(noop);
3996
- }
3997
- async _liveSiblings(memberMesh, ours) {
3998
- const self = this._deviceKeyPair?.publicKey;
3999
- const siblings = [];
4000
- for await (const device of memberMesh.view.find(DEVICES)) {
4001
- if (device.revoked || !(device.memberId && b4a.equals(device.memberId, ours))) {
4002
- continue;
4003
- }
4004
- if (self && b4a.equals(device.id, self))
4005
- continue;
4006
- siblings.push(device.id);
4007
- }
4008
- return siblings;
3813
+ _mayFollow(memberMesh) {
3814
+ const moving = this._joinCancellation ? this._joinCancellation.account : this._minting;
3815
+ const leaving = this._leaving === this._meshIdOf(memberMesh);
3816
+ return !this.closing && !moving && !leaving && this._isAccount(memberMesh);
4009
3817
  }
4010
3818
  registerLoanableHarness(serve) {
4011
3819
  this._serveLoanedHarness = serve;
@@ -4063,193 +3871,6 @@ export class Core extends ReadyResource {
4063
3871
  }
4064
3872
  return false;
4065
3873
  }
4066
- async _serveMeshSeed(request, fromDeviceId) {
4067
- const asker = b4a.from(fromDeviceId);
4068
- if ('writerKey' in request) {
4069
- return request.inviteToken
4070
- ? this._admitInvitedDevice(request, asker)
4071
- : this._admitIntoMesh(request, asker);
4072
- }
4073
- const standing = await this._askerStanding(asker);
4074
- if (standing === 'unseen') {
4075
- throw new Error(SEED_ASKER_UNSEEN);
4076
- }
4077
- if (standing === 'refused') {
4078
- this._log?.warn(['p2p', 'mesh'], 'seed request refused: not a device of this member');
4079
- return null;
4080
- }
4081
- if (!(await this._isJoinedMeshKey(request.meshKey))) {
4082
- this._log?.warn(['p2p', 'mesh'], 'seed request refused: not a mesh this member joined');
4083
- return null;
4084
- }
4085
- const mesh = this._pooled(b4a.toHex(request.meshKey));
4086
- const row = mesh ? await mesh.view.get(DEVICES, { id: asker }) : null;
4087
- if (mesh && row && (await this._removedFor(mesh, row))) {
4088
- this._log?.warn(['p2p', 'mesh'], 'seed request refused: removed from this mesh');
4089
- return null;
4090
- }
4091
- const session = await this.local.readMeshSession(request.meshKey);
4092
- return session?.seed ? { seed: session.seed } : null;
4093
- }
4094
- async _admitInvitedDevice(request, asker) {
4095
- const account = await this._accountMesh();
4096
- if (!(account?.key && b4a.equals(account?.key, request.meshKey))) {
4097
- this._log?.warn(['p2p', 'mesh'], "device invite refused: not this device's account");
4098
- return null;
4099
- }
4100
- const context = { deviceKeyPair: this._requireDeviceKeyPair(), log: this._log };
4101
- const admitted = await admitInvitedDevice(account, context, {
4102
- invites: this._deviceInvites,
4103
- deviceId: asker,
4104
- request
4105
- });
4106
- if (admitted) {
4107
- return { admitted: true };
4108
- }
4109
- if (request.inviteToken && (await this._deviceInvites.spent(request.inviteToken, asker))) {
4110
- throw new Error(DEVICE_INVITE_SPENT);
4111
- }
4112
- return null;
4113
- }
4114
- async _admitIntoMesh(request, asker) {
4115
- if (this._meshTransitionLock.locked) {
4116
- throw new Error(ADMISSION_DEFERRED);
4117
- }
4118
- await this._meshTransitionLock.lock();
4119
- try {
4120
- return await this._admitIntoMeshLocked(request, asker);
4121
- }
4122
- finally {
4123
- this._meshTransitionLock.unlock();
4124
- }
4125
- }
4126
- async _admitIntoMeshLocked(request, asker) {
4127
- if (this._leaving)
4128
- return null;
4129
- if (this._minting) {
4130
- throw new Error(ADMISSION_DEFERRED);
4131
- }
4132
- const account = await this._accountMesh();
4133
- const ours = account?.ourMemberId;
4134
- const row = ours ? await account.view.get(DEVICES, { id: asker }) : null;
4135
- if (!ours ||
4136
- !row ||
4137
- row.revoked ||
4138
- !(row.memberId && b4a.equals(row.memberId, ours)) ||
4139
- (await this._askerStanding(asker)) !== 'ours') {
4140
- this._log?.warn(['p2p', 'mesh'], 'admission refused: not a live device of this member');
4141
- return null;
4142
- }
4143
- if (!(await this._isJoinedMeshKey(request.meshKey))) {
4144
- this._log?.warn(['p2p', 'mesh'], 'admission refused: not a mesh this member joined');
4145
- return null;
4146
- }
4147
- const session = await this.local.readMeshSession(request.meshKey);
4148
- if (!session?.seed) {
4149
- return null;
4150
- }
4151
- const writerKey = request.writerKey;
4152
- const joinedAt = request.joinedAt ?? 0;
4153
- const claim = {
4154
- meshPublicKey: meshPublicKeyFromSeed(session.seed),
4155
- deviceId: asker,
4156
- writerKey,
4157
- joinedAt
4158
- };
4159
- if (!verifyWriterClaim(request.deviceSignature, claim) ||
4160
- !verifyMemberWriterClaim(request.memberSignature, { ...claim, memberId: ours })) {
4161
- this._log?.warn(['p2p', 'mesh'], 'admission refused: the asker did not sign its claim');
4162
- return null;
4163
- }
4164
- const mesh = await this._openPooledMesh(b4a.toHex(request.meshKey));
4165
- await mesh.autobee.update();
4166
- if (!mesh.writable) {
4167
- throw new Error(ADMISSION_DEFERRED);
4168
- }
4169
- if ((await mesh.bornKind()) !== 'shared') {
4170
- this._log?.warn(['p2p', 'mesh'], 'admission refused: a sibling joins this mesh by its seed');
4171
- return null;
4172
- }
4173
- const self = await mesh.view.get(DEVICES, {
4174
- id: this._requireDeviceKeyPair().publicKey
4175
- });
4176
- if (!(self?.memberId && b4a.equals(self.memberId, ours)) ||
4177
- self.revoked ||
4178
- !(self.writerKey && b4a.equals(self.writerKey, mesh.localWriterKey))) {
4179
- this._log?.warn(['p2p', 'mesh'], 'admission refused: this device is not linked in the mesh');
4180
- return null;
4181
- }
4182
- const current = await mesh.view.get(DEVICES, { id: asker });
4183
- if (current && (await this._removedFor(mesh, current))) {
4184
- this._log?.warn(['p2p', 'mesh'], 'admission refused: removed from this mesh');
4185
- return null;
4186
- }
4187
- if (current && !current.revoked) {
4188
- if (current.writerKey && b4a.equals(current.writerKey, writerKey)) {
4189
- return { admitted: true };
4190
- }
4191
- if (!(row.joinedAt > current.joinedAt)) {
4192
- throw new Error(ADMISSION_DEFERRED);
4193
- }
4194
- }
4195
- await mesh.dispatch('vouched-join', {
4196
- signature: mesh.sign(writerKey),
4197
- deviceId: asker,
4198
- device: claimableDevice({
4199
- id: asker,
4200
- joinedAt,
4201
- name: row.name,
4202
- capabilityProfile: row.capabilityProfile,
4203
- recommendedModelName: row.recommendedModelName
4204
- }),
4205
- deviceSignature: request.deviceSignature,
4206
- writerKey,
4207
- memberProof: row.memberProof,
4208
- memberSignature: request.memberSignature
4209
- });
4210
- return { admitted: true };
4211
- }
4212
- async _isJoinedMeshKey(meshKey) {
4213
- for (const entry of this._meshes.values()) {
4214
- const ours = entry.mesh.ourMemberId;
4215
- if (!ours) {
4216
- continue;
4217
- }
4218
- const row = await entry.mesh.view.get(JOINED_MESHES, { meshKey }).catch(noop);
4219
- if (row?.memberId && b4a.equals(row.memberId, ours) && !row.leftAt) {
4220
- return true;
4221
- }
4222
- }
4223
- return false;
4224
- }
4225
- async _removedFor(mesh, row) {
4226
- if (!row.revoked)
4227
- return false;
4228
- const by = await removedBy(mesh.view, row);
4229
- if (by !== 'own-member') {
4230
- return by === 'another-member';
4231
- }
4232
- const paired = await this._accountMesh()?.view.get(DEVICES, { id: row.id });
4233
- return !(paired && !paired.revoked && row.revokedAt && paired.joinedAt > row.revokedAt);
4234
- }
4235
- async _askerStanding(deviceId) {
4236
- const account = this._accountMesh();
4237
- const ours = account?.ourMemberId;
4238
- if (!account || !ours) {
4239
- return 'unseen';
4240
- }
4241
- const device = await account.view.get(DEVICES, { id: deviceId });
4242
- if (!device) {
4243
- return 'unseen';
4244
- }
4245
- if (device.revoked) {
4246
- return 'refused';
4247
- }
4248
- if (!device.memberId) {
4249
- return 'unseen';
4250
- }
4251
- return b4a.equals(device.memberId, ours) ? 'ours' : 'refused';
4252
- }
4253
3874
  async _skillOverlayOp(agentId, op) {
4254
3875
  const host = await this._resolveAgentHost(agentId);
4255
3876
  if (!host) {
@@ -4446,7 +4067,6 @@ export class Core extends ReadyResource {
4446
4067
  });
4447
4068
  this._secretHandoff.addStream(conn);
4448
4069
  this._skillOverlayHandoff.addStream(conn);
4449
- this._meshSeedHandoff.addStream(conn);
4450
4070
  this._harnessLoan.addStream(conn);
4451
4071
  this._inferenceCallsHandoff.addStream(conn);
4452
4072
  if (this._ownsSwarm) {
@@ -4467,6 +4087,7 @@ export class Core extends ReadyResource {
4467
4087
  }
4468
4088
  }
4469
4089
  _trackPeerConnection(conn, detach) {
4090
+ this._seenPeers.add(b4a.toHex(conn.remotePublicKey));
4470
4091
  const remove = () => {
4471
4092
  if (this._peers.delete(conn)) {
4472
4093
  this._onPeerChange();
@@ -4531,6 +4152,14 @@ export class Core extends ReadyResource {
4531
4152
  displayName: () => this.displayName()
4532
4153
  };
4533
4154
  }
4155
+ _blindPeeringSource() {
4156
+ return {
4157
+ watch: (listener) => void this._blindPeeringListeners.add(listener),
4158
+ unwatch: (listener) => void this._blindPeeringListeners.delete(listener),
4159
+ snapshot: () => ({ enabled: this._blindPeeringOn }),
4160
+ set: (enabled) => this.setBlindPeering(enabled)
4161
+ };
4162
+ }
4534
4163
  _networkStatusSource() {
4535
4164
  const listeners = new Set();
4536
4165
  let timer = null;
@@ -4630,6 +4259,14 @@ export class Core extends ReadyResource {
4630
4259
  }
4631
4260
  };
4632
4261
  }
4262
+ registerModelMemory(hook) {
4263
+ this._modelMemory = hook;
4264
+ return () => {
4265
+ if (this._modelMemory === hook) {
4266
+ this._modelMemory = null;
4267
+ }
4268
+ };
4269
+ }
4633
4270
  registerDeleteModel(hook) {
4634
4271
  this._deleteModel = hook;
4635
4272
  return () => {
@@ -4696,6 +4333,13 @@ export class Core extends ReadyResource {
4696
4333
  }
4697
4334
  return null;
4698
4335
  }
4336
+ async _readModelList(request) {
4337
+ const models = await this._readModels(request);
4338
+ if (!models) {
4339
+ return null;
4340
+ }
4341
+ return { models, memory: (await this._modelMemory?.()) ?? undefined };
4342
+ }
4699
4343
  async _readModels({ excludeCustom, ...options }) {
4700
4344
  const list = await this._listModels?.(options);
4701
4345
  if (!list) {
@@ -4939,6 +4583,7 @@ export class Core extends ReadyResource {
4939
4583
  fastForwards: mesh.autobee.stats.fastForwards,
4940
4584
  member: this._memberDiagnostics(mesh),
4941
4585
  orphanedBlobSweep: mesh.orphanedBlobSweepStatus,
4586
+ blindPeering: this._blindPeeringOn,
4942
4587
  blindPeerCount: mesh.blindPeerKeys.length,
4943
4588
  blindPeers: mesh.blindPeerStats()
4944
4589
  }
@@ -5005,9 +4650,6 @@ export class Core extends ReadyResource {
5005
4650
  }
5006
4651
  }
5007
4652
  }
5008
- function after(ms) {
5009
- return new Promise((resolve) => unref(setTimeout(resolve, ms)));
5010
- }
5011
4653
  function unmarkedNotice(losers) {
5012
4654
  if (losers.length === 0)
5013
4655
  return undefined;
@@ -5015,6 +4657,10 @@ function unmarkedNotice(losers) {
5015
4657
  return `another device of yours opened a shared mesh at the same moment, so ${ids} is an ordinary mesh now`;
5016
4658
  }
5017
4659
  function noop() { }
4660
+ function ownedBy(device, member) {
4661
+ const owner = device?.memberId ?? device?.claimedMemberId;
4662
+ return !!owner && !!member && b4a.equals(owner, member);
4663
+ }
5018
4664
  function isPeerConnection(conn) {
5019
4665
  return 'remotePublicKey' in conn;
5020
4666
  }