@qvac/core 0.9.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (201) hide show
  1. package/CHANGELOG.md +94 -1
  2. package/README.md +4 -4
  3. package/dist-lib/index.d.ts +0 -1
  4. package/dist-lib/index.js +0 -1
  5. package/dist-lib/lib/api/agents.d.ts +7 -1
  6. package/dist-lib/lib/api/agents.js +2 -2
  7. package/dist-lib/lib/api/app-config.d.ts +2 -1
  8. package/dist-lib/lib/api/app-config.js +2 -2
  9. package/dist-lib/lib/api/attachments.js +3 -2
  10. package/dist-lib/lib/api/chat-move.d.ts +2 -1
  11. package/dist-lib/lib/api/chat-move.js +2 -2
  12. package/dist-lib/lib/api/chat.d.ts +3 -1
  13. package/dist-lib/lib/api/chat.js +5 -5
  14. package/dist-lib/lib/api/chunks.d.ts +2 -1
  15. package/dist-lib/lib/api/chunks.js +3 -3
  16. package/dist-lib/lib/api/config.d.ts +2 -1
  17. package/dist-lib/lib/api/config.js +2 -2
  18. package/dist-lib/lib/api/current-mesh.d.ts +1 -3
  19. package/dist-lib/lib/api/current-mesh.js +4 -10
  20. package/dist-lib/lib/api/custom-models.d.ts +7 -1
  21. package/dist-lib/lib/api/custom-models.js +10 -8
  22. package/dist-lib/lib/api/device-routes.d.ts +0 -2
  23. package/dist-lib/lib/api/device-routes.js +4 -5
  24. package/dist-lib/lib/api/devices.d.ts +3 -1
  25. package/dist-lib/lib/api/devices.js +9 -8
  26. package/dist-lib/lib/api/diagnostics.d.ts +2 -1
  27. package/dist-lib/lib/api/diagnostics.js +2 -2
  28. package/dist-lib/lib/api/index.d.ts +2 -0
  29. package/dist-lib/lib/api/index.js +39 -21
  30. package/dist-lib/lib/api/instrument.d.ts +2 -2
  31. package/dist-lib/lib/api/instrument.js +10 -11
  32. package/dist-lib/lib/api/knowledge.d.ts +3 -1
  33. package/dist-lib/lib/api/knowledge.js +9 -7
  34. package/dist-lib/lib/api/logs.js +3 -3
  35. package/dist-lib/lib/api/members.d.ts +6 -1
  36. package/dist-lib/lib/api/members.js +11 -10
  37. package/dist-lib/lib/api/mesh.d.ts +2 -2
  38. package/dist-lib/lib/api/mesh.js +6 -6
  39. package/dist-lib/lib/api/model-download.d.ts +2 -1
  40. package/dist-lib/lib/api/model-download.js +2 -2
  41. package/dist-lib/lib/api/network.d.ts +3 -2
  42. package/dist-lib/lib/api/network.js +4 -4
  43. package/dist-lib/lib/api/notifications.d.ts +3 -1
  44. package/dist-lib/lib/api/notifications.js +5 -4
  45. package/dist-lib/lib/api/rag.d.ts +0 -1
  46. package/dist-lib/lib/api/rag.js +3 -4
  47. package/dist-lib/lib/api/sync.d.ts +2 -1
  48. package/dist-lib/lib/api/sync.js +2 -2
  49. package/dist-lib/lib/api/tool-activity.d.ts +2 -1
  50. package/dist-lib/lib/api/tool-activity.js +4 -3
  51. package/dist-lib/lib/api/tool-configs.d.ts +2 -1
  52. package/dist-lib/lib/api/tool-configs.js +4 -3
  53. package/dist-lib/lib/api/upload-frames.d.ts +0 -1
  54. package/dist-lib/lib/api/upload-frames.js +0 -1
  55. package/dist-lib/lib/api/watchable.d.ts +3 -2
  56. package/dist-lib/lib/api/watchable.js +2 -3
  57. package/dist-lib/lib/assistant/fake-models.d.ts +2 -4
  58. package/dist-lib/lib/assistant/harness.js +8 -6
  59. package/dist-lib/lib/assistant-client.d.ts +2 -0
  60. package/dist-lib/lib/assistant-client.js +4 -3
  61. package/dist-lib/lib/assistant-tree.d.ts +3 -3
  62. package/dist-lib/lib/assistant.d.ts +2 -2
  63. package/dist-lib/lib/assistant.js +11 -11
  64. package/dist-lib/lib/client.d.ts +2 -2
  65. package/dist-lib/lib/core/agent.js +5 -4
  66. package/dist-lib/lib/core/archived-mesh.d.ts +2 -3
  67. package/dist-lib/lib/core/archived-mesh.js +3 -3
  68. package/dist-lib/lib/core/chat-activity.js +5 -3
  69. package/dist-lib/lib/core/chat-copy.d.ts +0 -1
  70. package/dist-lib/lib/core/chat-copy.js +2 -2
  71. package/dist-lib/lib/core/chat-fork.d.ts +0 -6
  72. package/dist-lib/lib/core/chat-fork.js +11 -17
  73. package/dist-lib/lib/core/chat-group.js +8 -7
  74. package/dist-lib/lib/core/chat-move.d.ts +4 -6
  75. package/dist-lib/lib/core/chat-move.js +14 -15
  76. package/dist-lib/lib/core/chat-order.d.ts +2 -1
  77. package/dist-lib/lib/core/chat-order.js +15 -2
  78. package/dist-lib/lib/core/chat.js +8 -7
  79. package/dist-lib/lib/core/chunk.js +13 -8
  80. package/dist-lib/lib/core/collections.d.ts +1 -0
  81. package/dist-lib/lib/core/collections.js +1 -0
  82. package/dist-lib/lib/core/contract/compute.d.ts +0 -4
  83. package/dist-lib/lib/core/contract/compute.js +2 -9
  84. package/dist-lib/lib/core/contract/types.d.ts +2 -2
  85. package/dist-lib/lib/core/departures.d.ts +2 -2
  86. package/dist-lib/lib/core/device-control.d.ts +0 -2
  87. package/dist-lib/lib/core/device-control.js +5 -6
  88. package/dist-lib/lib/core/files/attachments.js +5 -4
  89. package/dist-lib/lib/core/files/index.d.ts +2 -1
  90. package/dist-lib/lib/core/files/index.js +20 -8
  91. package/dist-lib/lib/core/files/processing/claims.js +3 -3
  92. package/dist-lib/lib/core/files/processing/document.d.ts +2 -0
  93. package/dist-lib/lib/core/files/processing/document.js +3 -0
  94. package/dist-lib/lib/core/files/processing/index.js +4 -2
  95. package/dist-lib/lib/core/files/processing/source.d.ts +3 -0
  96. package/dist-lib/lib/core/files/processing/source.js +5 -3
  97. package/dist-lib/lib/core/files/processing/state.js +1 -0
  98. package/dist-lib/lib/core/files/rag/index.d.ts +3 -0
  99. package/dist-lib/lib/core/files/rag/index.js +3 -1
  100. package/dist-lib/lib/core/files/rag/load.js +6 -3
  101. package/dist-lib/lib/core/files/rag/produce.js +3 -3
  102. package/dist-lib/lib/core/files/rag/retrieval/full-text.js +1 -1
  103. package/dist-lib/lib/core/files/rag/similarity.d.ts +2 -2
  104. package/dist-lib/lib/core/files/rag/similarity.js +2 -3
  105. package/dist-lib/lib/core/files/records/chunks.js +1 -1
  106. package/dist-lib/lib/core/files/records/knowledge.js +2 -1
  107. package/dist-lib/lib/core/files/records/purge.js +9 -5
  108. package/dist-lib/lib/core/files/records/reclaim.js +20 -48
  109. package/dist-lib/lib/core/files/records/sweep.d.ts +3 -0
  110. package/dist-lib/lib/core/files/records/sweep.js +9 -3
  111. package/dist-lib/lib/core/files/store/blob.d.ts +2 -0
  112. package/dist-lib/lib/core/files/store/blob.js +2 -2
  113. package/dist-lib/lib/core/files/store/index.d.ts +1 -0
  114. package/dist-lib/lib/core/files/store/index.js +2 -0
  115. package/dist-lib/lib/core/files/store/reference.js +4 -2
  116. package/dist-lib/lib/core/files/upload.d.ts +1 -1
  117. package/dist-lib/lib/core/files/upload.js +10 -9
  118. package/dist-lib/lib/core/handoff-channel.d.ts +6 -4
  119. package/dist-lib/lib/core/handoff-channel.js +8 -9
  120. package/dist-lib/lib/core/harness-loan.d.ts +0 -2
  121. package/dist-lib/lib/core/harness-loan.js +3 -4
  122. package/dist-lib/lib/core/hosted-agents.d.ts +2 -2
  123. package/dist-lib/lib/core/inference-calls-handoff.d.ts +2 -2
  124. package/dist-lib/lib/core/inference-calls-handoff.js +6 -5
  125. package/dist-lib/lib/core/inference-calls-receiver.d.ts +0 -2
  126. package/dist-lib/lib/core/inference-calls-receiver.js +5 -7
  127. package/dist-lib/lib/core/local/index.d.ts +4 -1
  128. package/dist-lib/lib/core/local/index.js +23 -2
  129. package/dist-lib/lib/core/membership.d.ts +2 -2
  130. package/dist-lib/lib/core/mesh.d.ts +13 -7
  131. package/dist-lib/lib/core/mesh.js +55 -47
  132. package/dist-lib/lib/core/migrations/file-index.js +2 -4
  133. package/dist-lib/lib/core/next-frame.js +2 -1
  134. package/dist-lib/lib/core/pdf/rasterize.d.ts +2 -1
  135. package/dist-lib/lib/core/pdf/rasterize.js +5 -5
  136. package/dist-lib/lib/core/peer-duplex.d.ts +3 -3
  137. package/dist-lib/lib/core/peer-duplex.js +4 -4
  138. package/dist-lib/lib/core/secret-handoff.d.ts +2 -2
  139. package/dist-lib/lib/core/secret-handoff.js +7 -4
  140. package/dist-lib/lib/core/send.js +3 -2
  141. package/dist-lib/lib/core/skill-overlay-handoff.d.ts +2 -2
  142. package/dist-lib/lib/core/skill-overlay-handoff.js +6 -5
  143. package/dist-lib/lib/core/skill-overlay-receiver.js +5 -5
  144. package/dist-lib/lib/core/skill-secrets.d.ts +0 -5
  145. package/dist-lib/lib/core/skill-secrets.js +5 -9
  146. package/dist-lib/lib/core/sync/changed.d.ts +20 -0
  147. package/dist-lib/lib/core/sync/changed.js +7 -0
  148. package/dist-lib/lib/core/sync/devices.js +1 -1
  149. package/dist-lib/lib/core/sync/files.d.ts +1 -0
  150. package/dist-lib/lib/core/sync/files.js +50 -72
  151. package/dist-lib/lib/core/sync/index.d.ts +1 -2
  152. package/dist-lib/lib/core/sync/index.js +40 -62
  153. package/dist-lib/lib/core/sync/view-updates.d.ts +33 -0
  154. package/dist-lib/lib/core/sync/view-updates.js +133 -0
  155. package/dist-lib/lib/core/system-info.d.ts +2 -2
  156. package/dist-lib/lib/core/system-info.js +10 -11
  157. package/dist-lib/lib/core/tool-configs.js +6 -6
  158. package/dist-lib/lib/core.d.ts +1 -7
  159. package/dist-lib/lib/core.js +147 -131
  160. package/dist-lib/lib/custom-model-pusher.d.ts +2 -4
  161. package/dist-lib/lib/custom-model-task.d.ts +2 -1
  162. package/dist-lib/lib/custom-model-task.js +2 -2
  163. package/dist-lib/lib/downloads-allowed-pusher.d.ts +2 -4
  164. package/dist-lib/lib/errors.d.ts +603 -0
  165. package/dist-lib/lib/errors.js +552 -0
  166. package/dist-lib/lib/log.d.ts +7 -6
  167. package/dist-lib/lib/log.js +10 -6
  168. package/dist-lib/lib/logger-type.d.ts +2 -5
  169. package/dist-lib/lib/model-download-status.d.ts +2 -2
  170. package/dist-lib/lib/model-download.d.ts +2 -2
  171. package/dist-lib/lib/model-download.js +3 -2
  172. package/dist-lib/lib/model-idle-pusher.d.ts +2 -4
  173. package/dist-lib/lib/model-memory-cap.d.ts +2 -4
  174. package/dist-lib/lib/oauth/browser-connect.js +3 -2
  175. package/dist-lib/lib/oauth/flow-core.js +3 -3
  176. package/dist-lib/lib/oauth/loopback.js +10 -9
  177. package/dist-lib/lib/oauth/providers.js +4 -2
  178. package/dist-lib/lib/oauth/register.js +3 -2
  179. package/dist-lib/lib/oauth/token-exchange-core.js +5 -4
  180. package/dist-lib/lib/run-transcript.d.ts +2 -2
  181. package/dist-lib/lib/task.js +2 -1
  182. package/dist-lib/schema/engine.js +6 -3
  183. package/dist-lib/schema/field-order.js +1 -3
  184. package/dist-lib/schema/local.js +11 -0
  185. package/dist-lib/spec/hrpc/messages.js +20 -9
  186. package/dist-lib/spec/hyperschema/index.js +20 -9
  187. package/dist-lib/spec/hyperschema/schema.json +15 -0
  188. package/dist-lib/spec/hyperschema/types.d.ts +3 -0
  189. package/dist-lib/spec/local/hyperdb/db.json +16 -1
  190. package/dist-lib/spec/local/hyperdb/index.js +71 -2
  191. package/dist-lib/spec/local/hyperdb/messages.js +300 -143
  192. package/dist-lib/spec/local/hyperschema/index.js +82 -2
  193. package/dist-lib/spec/local/hyperschema/schema.json +43 -1
  194. package/dist-lib/spec/local/hyperschema/types.d.ts +12 -1
  195. package/package.json +9 -2
  196. package/tui/app.mjs +1 -1
  197. package/worker/assistant-sidecar.mjs +7 -0
  198. package/dist-lib/lib/log-levels.d.ts +0 -7
  199. package/dist-lib/lib/log-levels.js +0 -1
  200. package/dist-lib/lib/logger.d.ts +0 -5
  201. package/dist-lib/lib/logger.js +0 -18
@@ -14,13 +14,14 @@ import ScopeLock from 'scope-lock';
14
14
  import { isAndroid } from 'which-runtime';
15
15
  import { createApi } from './api/index.js';
16
16
  import { writeAutoDownloadModels } from './api/app-config.js';
17
- import { NO_SUCH_MESH, requireMesh } from './api/current-mesh.js';
17
+ import { requireMesh } from './api/current-mesh.js';
18
+ import { CoreError } from './errors.js';
18
19
  import { ChatMoves } from './core/chat-move.js';
19
20
  import { asEncoded, watchModels } from './models-watch.js';
20
21
  import { BLIND_PEER_KEYS, BLIND_PEERING_OFF_KEY, BLIND_RELAY_KEYS } from './core/blind-peers.js';
21
22
  import { CoreBlobServer, MemoryVectorIndex } from './core/files/index.js';
22
23
  import { getChat, removeChatAttachments, removeChatKnowledge } from './core/chat.js';
23
- import { rebuildChatOrder, refreshChatOrder } from './core/chat-order.js';
24
+ import { rebuildChatOrder, refreshChatOrder, retireGoneChats } from './core/chat-order.js';
24
25
  import { createFsDriveHost } from './core/fs-drives.js';
25
26
  import { sweepDriveStaging } from './fs-drive-bridge.js';
26
27
  import { decodeMeshInvite, encodeMeshInvite } from './core/invite.js';
@@ -34,10 +35,10 @@ import { HarnessLoan } from './core/harness-loan.js';
34
35
  import { duplexPair } from '@qvac/harness/transport';
35
36
  import { SecretHandoff } from './core/secret-handoff.js';
36
37
  import { InferenceCallsHandoff } from './core/inference-calls-handoff.js';
37
- import { createInferenceCallsReceiver, NO_RECORD_STORE } from './core/inference-calls-receiver.js';
38
+ import { createInferenceCallsReceiver } from './core/inference-calls-receiver.js';
38
39
  import { SkillOverlayHandoff } from './core/skill-overlay-handoff.js';
39
40
  import { createSkillOverlayReceiver } from './core/skill-overlay-receiver.js';
40
- import { createSkillSecretReceiver, HOST_REVOKED, NO_SUCH_AGENT, NOT_THE_HOST } from './core/skill-secrets.js';
41
+ import { createSkillSecretReceiver } from './core/skill-secrets.js';
41
42
  import { attestDeviceProof, attestMemberClaim, claimAttestation, delegationFits, openIdentity } from './core/identity.js';
42
43
  import { openRecoveryCore, publishRecovery, readRecovery, recoveryKeys } from './core/account-recovery.js';
43
44
  import { claimOwnMembership, legacyNameStamp, namedAt, renameFoundedMember } from './core/membership.js';
@@ -52,7 +53,6 @@ import { PendingDepartures } from './core/departures.js';
52
53
  import { Mesh } from './core/mesh.js';
53
54
  import { raceResource } from './race-resource.js';
54
55
  import { createSystemInfoSampler } from './core/system-info.js';
55
- import { setLogger } from './logger.js';
56
56
  import { settleOnClose } from '@qvac/harness/rpc';
57
57
  import { openLocalSpec } from './spec-loader.js';
58
58
  import { unref } from './unref.js';
@@ -65,26 +65,9 @@ const top = new Top();
65
65
  const FILE_GC_INTERVAL_MS = 24 * 60 * 60 * 1000;
66
66
  const SWARM_KEYPAIR_NAME = 'qvac-swarm-keypair';
67
67
  const QVAC_CORE_NAMESPACE = 'qvac-core';
68
- const MESH_JOIN_CANCELED = 'Mesh join canceled';
69
68
  const MESH_ID = /^[0-9a-f]{64}$/;
70
- const MESH_BELOW_FLOOR = 'Build is below the mesh floor';
71
- const MESH_WITHOUT_ACCOUNT = 'Recording a mesh needs the account mesh';
72
- export const ACCOUNT_MEMBER_UNKNOWN = 'This device is not part of its account yet';
73
69
  export const DEVICE_UNKNOWN = 'This device has no device details: pass `device` when creating Core';
74
- const MESH_WITHOUT_MEMBER = 'A mesh needs this device to belong to its account member';
75
- const NO_WRITABLE_ACCOUNT = 'The account mesh is not writable';
76
- const NOT_AN_ARCHIVED_ACCOUNT = 'Not an account this device left';
77
- const NO_ACCOUNT_TO_RECOVER = 'This device has no account to make recoverable';
78
- const PHRASE_NOT_OURS = 'The phrase does not derive the identity this device holds';
79
- 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";
80
- const NO_RECOVERY_RECORD = 'No recovery record was found for this phrase';
81
70
  const RECOVERY_WAIT_MS = 30_000;
82
- export const MEMBER_INVITE_HOME_REFUSED = 'An account mesh cannot be invited into';
83
- export const DEVICE_INVITE_MESH_REFUSED = 'Only an account mesh can take a device invite';
84
- export const MESH_NAME_ACCOUNT_REFUSED = 'An account mesh has no shared name';
85
- export const INVITE_ACCOUNT_UNREADABLE = 'This device cannot reach its account to check';
86
- export const DEVICE_INVITE_CHAIN_TOO_LONG = 'This device cannot invite another one: invite from another of your devices, or recover from your phrase';
87
- export const MESH_HELD_BY_SIBLING = 'Another of your devices holds this mesh: bring that device online, or invite from it instead';
88
71
  const MESH_DIED = 'the local database died';
89
72
  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';
90
73
  const INVITE_DHT_NODES_LIMIT = 3;
@@ -217,9 +200,6 @@ export class Core extends ReadyResource {
217
200
  resume: () => this.resume()
218
201
  };
219
202
  this._log = logger;
220
- if (logger) {
221
- setLogger(logger);
222
- }
223
203
  if (swarm && (!store || !wakeup)) {
224
204
  throw new Error('swarm may only be passed alongside store and wakeup');
225
205
  }
@@ -394,17 +374,18 @@ export class Core extends ReadyResource {
394
374
  void sweepDriveStaging();
395
375
  this._blobServer = new CoreBlobServer(this.store, (coreKey, blockOffset, meshKey) => this._meshForBlobCore(coreKey, blockOffset, meshKey), { onRead: (mesh, coreKey, blockOffset) => this._servedFile(mesh, coreKey, blockOffset) });
396
376
  this._blobServer.on('rebound', (port) => {
397
- logger.warn('core', 'blob server rebound, links minted before now are stale', { port });
377
+ this._log?.warn('core', 'blob server rebound, links minted before now are stale', { port });
398
378
  });
399
379
  await this._blobServer.ready();
400
380
  await this._openOtaConfig();
401
381
  await this._openBootMesh();
402
- this._systemInfo = createSystemInfoSampler();
382
+ this._systemInfo = createSystemInfoSampler(this._log);
403
383
  const meshStatus = this._meshStatusSource();
404
384
  const deviceId = this.devicePublicKey;
405
385
  this._chatMoves = deviceId
406
386
  ? new ChatMoves((meshId) => requireMesh(this._meshPool(), meshId), this.local, {
407
- deviceId
387
+ deviceId,
388
+ log: this._log
408
389
  })
409
390
  : null;
410
391
  const api = createApi({
@@ -419,6 +400,7 @@ export class Core extends ReadyResource {
419
400
  forget: async (chatId) => void (await this._inferenceCalls?.forget(chatId))
420
401
  },
421
402
  logs: this._logSource(),
403
+ log: this._log,
422
404
  chatMoves: this._chatMoves,
423
405
  deviceId: this.devicePublicKey,
424
406
  lifecycle: this._apiLifecycle,
@@ -436,14 +418,14 @@ export class Core extends ReadyResource {
436
418
  : null,
437
419
  loadModel: this._hostsModelSource
438
420
  ? ({ name }) => this._modelResidency?.load(name) ??
439
- Promise.reject(new Error('no model runtime is bound'))
421
+ Promise.reject(new CoreError('MODEL_RUNTIME_UNBOUND'))
440
422
  : null,
441
423
  unloadModel: this._hostsModelSource
442
424
  ? ({ name }) => this._modelResidency?.unload(name) ??
443
- Promise.reject(new Error('no model runtime is bound'))
425
+ Promise.reject(new CoreError('MODEL_RUNTIME_UNBOUND'))
444
426
  : null,
445
427
  modelUsers: this._hostsModelSource
446
- ? ({ name }) => this._modelUsers?.(name) ?? Promise.reject(new Error('no model runtime is bound'))
428
+ ? ({ name }) => this._modelUsers?.(name) ?? Promise.reject(new CoreError('MODEL_RUNTIME_UNBOUND'))
447
429
  : null,
448
430
  clearKvCache: () => this._clearKvCache?.() ??
449
431
  Promise.resolve({
@@ -465,25 +447,27 @@ export class Core extends ReadyResource {
465
447
  : null,
466
448
  retryModelDownload: this._hostsModelSource || this._retryModelDownload
467
449
  ? ({ name }) => this._retryModelDownload?.(name) ??
468
- Promise.reject(new Error('no model download source bound'))
450
+ Promise.reject(new CoreError('CAPABILITY_UNBOUND', ['model download source']))
469
451
  : null,
470
452
  transcribe: this._hostsModelSource || this._transcribe
471
453
  ? (request) => this._transcribe?.(request) ??
472
- Promise.reject(new Error('no transcription source bound'))
454
+ Promise.reject(new CoreError('CAPABILITY_UNBOUND', ['transcription source']))
473
455
  : null,
474
456
  detectLanguage: this._hostsModelSource || this._detectLanguage
475
457
  ? (request) => this._detectLanguage?.(request) ??
476
- Promise.reject(new Error('no language detector bound'))
458
+ Promise.reject(new CoreError('CAPABILITY_UNBOUND', ['language detector']))
477
459
  : null,
478
460
  previewModelSource: (request) => this._previewModelSource?.(request) ??
479
- Promise.reject(new Error('no model source preview bound')),
461
+ Promise.reject(new CoreError('CAPABILITY_UNBOUND', ['model source preview'])),
480
462
  speak: this._hostsModelSource || this._speak
481
- ? (request) => this._speak?.(request) ?? Promise.reject(new Error('no tts source bound'))
463
+ ? (request) => this._speak?.(request) ??
464
+ Promise.reject(new CoreError('CAPABILITY_UNBOUND', ['tts source']))
482
465
  : null,
483
466
  speakStream: this._hostsModelSource || this._speakStream
484
467
  ? (text) => {
485
- if (!this._speakStream)
486
- throw new Error('no tts source bound');
468
+ if (!this._speakStream) {
469
+ throw new CoreError('CAPABILITY_UNBOUND', ['tts source']);
470
+ }
487
471
  return this._speakStream(text);
488
472
  }
489
473
  : null,
@@ -560,7 +544,7 @@ export class Core extends ReadyResource {
560
544
  void this._pushIdentityPhrase();
561
545
  this._chatMoves
562
546
  ?.resume()
563
- .catch((err) => logger.warn('chat-move', 'unfinished moves not resumed', { err }));
547
+ .catch((err) => this._log?.warn('chat-move', 'unfinished moves not resumed', { err }));
564
548
  return;
565
549
  }
566
550
  catch (err) {
@@ -575,15 +559,15 @@ export class Core extends ReadyResource {
575
559
  this._blobServer = null;
576
560
  await this._departures?.close();
577
561
  this._departures = null;
578
- logger.info('core', 'closing swarm');
562
+ this._log?.info('core', 'closing swarm');
579
563
  await this._closeSwarm();
580
- logger.info('core', 'closing local');
564
+ this._log?.info('core', 'closing local');
581
565
  await this.local?.close();
582
- logger.info('core', 'closing mesh');
566
+ this._log?.info('core', 'closing mesh');
583
567
  for (const { mesh } of this._meshes.values()) {
584
568
  await mesh.close();
585
569
  }
586
- logger.info('core', 'closing storage');
570
+ this._log?.info('core', 'closing storage');
587
571
  await this._closeStore();
588
572
  }
589
573
  finally {
@@ -801,7 +785,7 @@ export class Core extends ReadyResource {
801
785
  if (!this.opened)
802
786
  await this.ready();
803
787
  if (this.closing || !this._api) {
804
- throw new Error('core is closing');
788
+ throw new CoreError('CORE_CLOSING');
805
789
  }
806
790
  const { bindApi, HRPC } = await this._spec();
807
791
  const rpc = settleOnClose(new HRPC(stream));
@@ -818,7 +802,7 @@ export class Core extends ReadyResource {
818
802
  if (this.closing)
819
803
  return;
820
804
  this.state = QvacState.WAKEUP;
821
- logger.info('core', 'wakeup');
805
+ this._log?.info('core', 'wakeup');
822
806
  if (!this.opened) {
823
807
  await this.ready();
824
808
  }
@@ -828,13 +812,13 @@ export class Core extends ReadyResource {
828
812
  if (this.closing)
829
813
  return;
830
814
  this.state = QvacState.PRESUSPEND;
831
- logger.info('core', 'presuspend');
815
+ this._log?.info('core', 'presuspend');
832
816
  if (!this.opened) {
833
817
  await this.ready();
834
818
  }
835
819
  }
836
820
  async suspend() {
837
- logger.info('core', 'suspend called');
821
+ this._log?.info('core', 'suspend called');
838
822
  if (this.state === QvacState.SUSPEND) {
839
823
  return;
840
824
  }
@@ -845,7 +829,7 @@ export class Core extends ReadyResource {
845
829
  if (!this.opened) {
846
830
  await this.ready();
847
831
  }
848
- logger.info('core', 'suspending');
832
+ this._log?.info('core', 'suspending');
849
833
  await this._stopFileGc();
850
834
  for (const { mesh } of this._meshes.values()) {
851
835
  await mesh.suspend();
@@ -866,7 +850,7 @@ export class Core extends ReadyResource {
866
850
  this._log?.info('lifecycle', 'core suspend');
867
851
  }
868
852
  async resume() {
869
- logger.info('core', 'resume called');
853
+ this._log?.info('core', 'resume called');
870
854
  if (this.state === QvacState.RESUME) {
871
855
  return;
872
856
  }
@@ -881,7 +865,7 @@ export class Core extends ReadyResource {
881
865
  if (!this.opened) {
882
866
  await this.ready();
883
867
  }
884
- logger.info('core', 'resuming');
868
+ this._log?.info('core', 'resuming');
885
869
  await this.storage.resume();
886
870
  if (this._blobServer) {
887
871
  await this._blobServer.resume();
@@ -933,16 +917,16 @@ export class Core extends ReadyResource {
933
917
  .filter((discovery) => discovery !== null);
934
918
  }
935
919
  async _close() {
936
- logger.info('core', 'closing');
920
+ this._log?.info('core', 'closing');
937
921
  if (this.state === QvacState.SUSPEND) {
938
922
  await this.storage.resume();
939
923
  }
940
924
  this._servedToSiblings = null;
941
925
  this._deviceControl.destroy();
942
- this._catalogHooks.destroy(new Error('core is closing'));
943
- this._modelCatalogWatchSource.destroy(new Error('core is closing'));
944
- this._offlineBoot.destroy(new Error('core is closing'));
945
- this.onlineBoot.destroy(new Error('core is closing'));
926
+ this._catalogHooks.destroy(new CoreError('CORE_CLOSING'));
927
+ this._modelCatalogWatchSource.destroy(new CoreError('CORE_CLOSING'));
928
+ this._offlineBoot.destroy(new CoreError('CORE_CLOSING'));
929
+ this.onlineBoot.destroy(new CoreError('CORE_CLOSING'));
946
930
  this._signalClose();
947
931
  this._notifyStatus();
948
932
  await this._stopFileGc();
@@ -973,15 +957,15 @@ export class Core extends ReadyResource {
973
957
  await this._closeOtaConfig();
974
958
  await this._fsDrives?.close();
975
959
  this._fsDrives = null;
976
- logger.info('core', 'closing swarm');
960
+ this._log?.info('core', 'closing swarm');
977
961
  await this._closeSwarm();
978
962
  this._meshes.clear();
979
963
  this._accountId = null;
980
- logger.info('core', 'closing local');
964
+ this._log?.info('core', 'closing local');
981
965
  await this.local?.close();
982
966
  this._systemInfo?.destroy();
983
967
  this._systemInfo = null;
984
- logger.info('core', 'closing storage');
968
+ this._log?.info('core', 'closing storage');
985
969
  await this._closeStore();
986
970
  this._log?.info('lifecycle', 'core closed');
987
971
  }
@@ -1030,9 +1014,9 @@ export class Core extends ReadyResource {
1030
1014
  }
1031
1015
  async _switchToMesh(opts = {}) {
1032
1016
  if (this.closing)
1033
- throw new Error('core is closing');
1017
+ throw new CoreError('CORE_CLOSING');
1034
1018
  if (this._blockedByMinVersion())
1035
- throw new Error(MESH_BELOW_FLOOR);
1019
+ throw new CoreError('MESH_BELOW_FLOOR');
1036
1020
  const previous = this._accountMesh();
1037
1021
  if (previous) {
1038
1022
  await this._retirePooledMesh(previous);
@@ -1040,7 +1024,7 @@ export class Core extends ReadyResource {
1040
1024
  const mesh = await this._openMesh(opts);
1041
1025
  if (this.closing) {
1042
1026
  await this._retireMesh(mesh);
1043
- throw new Error('core is closing');
1027
+ throw new CoreError('CORE_CLOSING');
1044
1028
  }
1045
1029
  const halted = mesh.interrupted;
1046
1030
  if (halted) {
@@ -1067,7 +1051,8 @@ export class Core extends ReadyResource {
1067
1051
  trustedDevice: (deviceId) => this._isOurMemberDevice(deviceId),
1068
1052
  wakeup: this._wakeup,
1069
1053
  attachmentUrl: (attachment) => this._linkAttachment(attachment, mesh),
1070
- fileIndex: this._fileIndexFor(() => mesh)
1054
+ fileIndex: this._fileIndexFor(() => mesh),
1055
+ pendingUpdates: this._pendingUpdatesFor(() => mesh)
1071
1056
  });
1072
1057
  try {
1073
1058
  await mesh.ready();
@@ -1175,7 +1160,7 @@ export class Core extends ReadyResource {
1175
1160
  const account = await this.local.accountMesh(this._requireDeviceKeyPair().publicKey);
1176
1161
  const mesh = this._accountMesh();
1177
1162
  if (!account || !(mesh?.key && b4a.equals(mesh?.key, account.key))) {
1178
- throw new Error(NO_ACCOUNT_TO_RECOVER);
1163
+ throw new CoreError('RECOVERY_NO_ACCOUNT');
1179
1164
  }
1180
1165
  const record = { meshKey: account.key, seed: account.seed, createdAt: Date.now() };
1181
1166
  return this._publishRecovery(mnemonic, record, mesh);
@@ -1194,14 +1179,14 @@ export class Core extends ReadyResource {
1194
1179
  const identity = await openIdentity(mnemonic);
1195
1180
  if (!this._identityPublicKey ||
1196
1181
  !b4a.equals(this._identityPublicKey, identity.identityPublicKey)) {
1197
- throw new Error(PHRASE_NOT_OURS);
1182
+ throw new CoreError('RECOVERY_PHRASE_NOT_OURS');
1198
1183
  }
1199
1184
  if (account) {
1200
1185
  const member = await account.view
1201
1186
  .get(MEMBERS, { id: identity.identityPublicKey })
1202
1187
  .catch(noop);
1203
1188
  if (!member) {
1204
- throw new Error(PHRASE_NOT_A_MEMBER);
1189
+ throw new CoreError('RECOVERY_PHRASE_NOT_A_MEMBER');
1205
1190
  }
1206
1191
  }
1207
1192
  const core = await this._openOwnRecoveryCore(recoveryKeys(identity));
@@ -1254,10 +1239,10 @@ export class Core extends ReadyResource {
1254
1239
  let ownsTransition = false;
1255
1240
  try {
1256
1241
  if (this.closing) {
1257
- throw new Error('Core is closing');
1242
+ throw new CoreError('CORE_CLOSING', ['Core']);
1258
1243
  }
1259
1244
  if (this._leaving || this._minting) {
1260
- throw new Error('Mesh transition already in progress');
1245
+ throw new CoreError('MESH_TRANSITION_IN_PROGRESS');
1261
1246
  }
1262
1247
  ownsTransition = true;
1263
1248
  this._minting = true;
@@ -1300,7 +1285,7 @@ export class Core extends ReadyResource {
1300
1285
  const deviceId = this._requireDeviceKeyPair().publicKey;
1301
1286
  const mesh = this._accountMesh();
1302
1287
  if (!mesh?.key || !b4a.equals(mesh.key, record.meshKey)) {
1303
- throw new Error('the recovered account did not adopt');
1288
+ throw new CoreError('RECOVERY_NOT_ADOPTED');
1304
1289
  }
1305
1290
  if (this._identityPublicKey &&
1306
1291
  b4a.equals(this._identityPublicKey, identity.identityPublicKey)) {
@@ -1335,7 +1320,7 @@ export class Core extends ReadyResource {
1335
1320
  catch (err) {
1336
1321
  this._log?.warn(['p2p', 'mesh'], 'no blind peer served a recovery record', err);
1337
1322
  }
1338
- throw new Error(NO_RECOVERY_RECORD);
1323
+ throw new CoreError('RECOVERY_RECORD_NOT_FOUND');
1339
1324
  }
1340
1325
  _peerRecoveryCore(core) {
1341
1326
  if (!this._swarm || this.closing || !this._blindPeeringOn)
@@ -1923,7 +1908,7 @@ export class Core extends ReadyResource {
1923
1908
  async joinMesh(rawInvite) {
1924
1909
  const invite = decodeMeshInvite(rawInvite);
1925
1910
  if (!invite) {
1926
- throw new Error('Invalid mesh invite');
1911
+ throw new CoreError('INVITE_INVALID');
1927
1912
  }
1928
1913
  await this._awaitMesh();
1929
1914
  this._requireLocalDevice();
@@ -1931,10 +1916,10 @@ export class Core extends ReadyResource {
1931
1916
  let ownsTransition = false;
1932
1917
  try {
1933
1918
  if (this.closing) {
1934
- throw new Error('Core is closing');
1919
+ throw new CoreError('CORE_CLOSING', ['Core']);
1935
1920
  }
1936
1921
  if (this._leaving || this._minting) {
1937
- throw new Error('Mesh transition already in progress');
1922
+ throw new CoreError('MESH_TRANSITION_IN_PROGRESS');
1938
1923
  }
1939
1924
  await this._opening.get(b4a.toHex(invite.baseKey))?.catch(noop);
1940
1925
  const pooled = this._pooled(b4a.toHex(invite.baseKey));
@@ -1965,8 +1950,9 @@ export class Core extends ReadyResource {
1965
1950
  this._cancelCandidateJoin?.();
1966
1951
  }
1967
1952
  _throwIfJoinCanceled() {
1968
- if (this._joinCancellation?.canceled)
1969
- throw new Error(MESH_JOIN_CANCELED);
1953
+ if (this._joinCancellation?.canceled) {
1954
+ throw new CoreError('MESH_JOIN_CANCELED');
1955
+ }
1970
1956
  }
1971
1957
  async _joinMesh(invite, retire = null) {
1972
1958
  const cancellation = { canceled: false, accepting: true, account: !invite.memberInvite };
@@ -2083,7 +2069,7 @@ export class Core extends ReadyResource {
2083
2069
  claim.resolve(mesh);
2084
2070
  }
2085
2071
  else {
2086
- claim.reject(new Error(MESH_BELOW_FLOOR));
2072
+ claim.reject(new CoreError('MESH_BELOW_FLOOR'));
2087
2073
  }
2088
2074
  }
2089
2075
  async _joinCandidate(invite, source, deviceId) {
@@ -2316,7 +2302,8 @@ export class Core extends ReadyResource {
2316
2302
  trustedDevice: (deviceId) => this._isOurMemberDevice(deviceId),
2317
2303
  wakeup: this._wakeup,
2318
2304
  attachmentUrl: (attachment) => this._linkAttachment(attachment, candidate),
2319
- fileIndex: this._fileIndexFor(() => candidate)
2305
+ fileIndex: this._fileIndexFor(() => candidate),
2306
+ pendingUpdates: this._pendingUpdatesFor(() => candidate)
2320
2307
  });
2321
2308
  this._candidateMesh = candidate;
2322
2309
  if (this._swarm) {
@@ -2339,23 +2326,23 @@ export class Core extends ReadyResource {
2339
2326
  await this._swarm.leave(candidate.discoveryKey).catch(noop);
2340
2327
  }
2341
2328
  await candidate.close().catch(noop);
2342
- throw this._joinCancellation?.canceled ? new Error(MESH_JOIN_CANCELED) : error;
2329
+ throw this._joinCancellation?.canceled ? new CoreError('MESH_JOIN_CANCELED') : error;
2343
2330
  }
2344
2331
  this._startBlindPeering(candidate);
2345
2332
  try {
2346
2333
  this._throwIfJoinCanceled();
2347
2334
  if (!b4a.equals(candidate.key, invite.baseKey)) {
2348
- throw new Error('Device is not permitted to join the invited mesh');
2335
+ throw new CoreError('JOIN_NOT_PERMITTED');
2349
2336
  }
2350
2337
  if ((await candidate.checkResumedRevocation()) === 'kicked') {
2351
- throw new Error('Device is not permitted to join the invited mesh');
2338
+ throw new CoreError('JOIN_NOT_PERMITTED');
2352
2339
  }
2353
2340
  this._throwIfJoinCanceled();
2354
2341
  return candidate;
2355
2342
  }
2356
2343
  catch (error) {
2357
2344
  await this._discardCandidateMesh();
2358
- throw this._joinCancellation?.canceled ? new Error(MESH_JOIN_CANCELED) : error;
2345
+ throw this._joinCancellation?.canceled ? new CoreError('MESH_JOIN_CANCELED') : error;
2359
2346
  }
2360
2347
  finally {
2361
2348
  disarm();
@@ -2537,6 +2524,28 @@ export class Core extends ReadyResource {
2537
2524
  const indexed = await this.local.files.meshOf(b4a.from(coreKey, 'hex'), blockOffset);
2538
2525
  return indexed ? this._pooled(b4a.toHex(indexed)) : null;
2539
2526
  }
2527
+ _pendingUpdatesFor(mesh) {
2528
+ const logged = (what) => (err) => this._log?.warn(['p2p', 'mesh'], `${what} the pending view update failed`, err);
2529
+ return {
2530
+ read: async () => {
2531
+ const row = await this.local.readMeshUpdate(mesh().key);
2532
+ if (!row) {
2533
+ return null;
2534
+ }
2535
+ const from = row.fromKey ? { key: row.fromKey, length: row.fromLength ?? 0 } : null;
2536
+ return { from, to: 0, incremental: !!row.incremental };
2537
+ },
2538
+ write: ({ from, incremental }) => this.local
2539
+ .writeMeshUpdate({
2540
+ meshKey: mesh().key,
2541
+ fromKey: from?.key,
2542
+ fromLength: from?.length,
2543
+ incremental
2544
+ })
2545
+ .catch(logged('writing')),
2546
+ clear: () => this.local.clearMeshUpdate(mesh().key).catch(logged('clearing'))
2547
+ };
2548
+ }
2540
2549
  _fileIndexFor(mesh) {
2541
2550
  const owner = () => ({ meshKey: mesh().key, ownCoreKey: mesh().blobs.core.key });
2542
2551
  const known = (chunks) => chunks.map((chunk) => knownFile(chunk, owner())).filter((file) => file !== null);
@@ -2565,6 +2574,10 @@ export class Core extends ReadyResource {
2565
2574
  }
2566
2575
  async _indexMeshFiles(mesh) {
2567
2576
  try {
2577
+ if (mesh.openedEmpty) {
2578
+ await this.local.files.markBackfilled(mesh.key);
2579
+ return;
2580
+ }
2568
2581
  await mesh.autobee.update();
2569
2582
  if (await this.local.files.backfilled(mesh.key)) {
2570
2583
  return;
@@ -2681,14 +2694,16 @@ export class Core extends ReadyResource {
2681
2694
  const { mesh } = entry;
2682
2695
  const deviceId = this._requireDeviceKeyPair().publicKey;
2683
2696
  mesh.on('notification', (event) => this.emit('notification', event));
2684
- const build = () => rebuildChatOrder(mesh, this.local).catch((error) => this._log?.warn(['p2p', 'mesh'], 'building the chat order failed', error));
2697
+ const build = () => rebuildChatOrder(mesh, this.local)
2698
+ .then((gone) => {
2699
+ void retireGoneChats(mesh, this.local, gone).catch((error) => this._log?.warn(['p2p', 'mesh'], 'retiring gone chats failed', error));
2700
+ })
2701
+ .catch((error) => this._log?.warn(['p2p', 'mesh'], 'building the chat order failed', error));
2685
2702
  let built = build();
2686
2703
  mesh.chatOrderBuilt = built;
2687
2704
  mesh.syncChatOrder = async (chatIds) => {
2688
2705
  await built;
2689
- await (chatIds
2690
- ? refreshChatOrder(mesh, this.local, chatIds)
2691
- : rebuildChatOrder(mesh, this.local));
2706
+ await refreshChatOrder(mesh, this.local, chatIds);
2692
2707
  };
2693
2708
  mesh.on('landed', () => {
2694
2709
  built = built.then(build);
@@ -2728,6 +2743,7 @@ export class Core extends ReadyResource {
2728
2743
  }
2729
2744
  };
2730
2745
  mesh.on('chat-purged', () => void reclaim());
2746
+ mesh.on('landed', () => void reclaim());
2731
2747
  const sweepLock = new ScopeLock({ debounce: true });
2732
2748
  const sweep = async () => {
2733
2749
  if (!(await sweepLock.lock()))
@@ -2744,8 +2760,7 @@ export class Core extends ReadyResource {
2744
2760
  sweepLock.unlock();
2745
2761
  }
2746
2762
  };
2747
- void sweep();
2748
- void this._indexMeshFiles(mesh).then(reclaim);
2763
+ void this._indexMeshFiles(mesh).then(() => Promise.all([reclaim(), sweep()]));
2749
2764
  mesh.autobee.on('move-to', () => void sweep());
2750
2765
  entry.hostedAgents = new HostedAgents({
2751
2766
  mesh,
@@ -2828,7 +2843,7 @@ export class Core extends ReadyResource {
2828
2843
  return;
2829
2844
  }
2830
2845
  if (this._leaving || this._minting) {
2831
- throw new Error('Mesh transition already in progress');
2846
+ throw new CoreError('MESH_TRANSITION_IN_PROGRESS');
2832
2847
  }
2833
2848
  const departing = await this._openPooledMesh(meshId);
2834
2849
  const leavingAccount = await this._isStoredAccount(departing);
@@ -2943,7 +2958,7 @@ export class Core extends ReadyResource {
2943
2958
  await this._meshTransitionLock.lock();
2944
2959
  try {
2945
2960
  if (this.closing) {
2946
- throw new Error('Core is closing');
2961
+ throw new CoreError('CORE_CLOSING', ['Core']);
2947
2962
  }
2948
2963
  const row = await this._archivedRow(meshId);
2949
2964
  if (row.migratedAt) {
@@ -2952,7 +2967,7 @@ export class Core extends ReadyResource {
2952
2967
  }
2953
2968
  const account = await this._writableAccount();
2954
2969
  if (!account) {
2955
- throw new Error(NO_WRITABLE_ACCOUNT);
2970
+ throw new CoreError('ACCOUNT_NOT_WRITABLE');
2956
2971
  }
2957
2972
  const counts = await this._withArchivedMesh(row, (mesh) => migrateContents(mesh, account));
2958
2973
  const migratedAt = Date.now();
@@ -2967,10 +2982,10 @@ export class Core extends ReadyResource {
2967
2982
  const key = this._meshKeyOf(meshId);
2968
2983
  const row = (await this.local.listArchivedMeshes()).find((row) => b4a.equals(row.key, key));
2969
2984
  if (!row) {
2970
- throw new Error(`${NO_SUCH_MESH}: ${meshId}`);
2985
+ throw new CoreError('MESH_NOT_FOUND', [meshId]);
2971
2986
  }
2972
2987
  if (!row.wasHome) {
2973
- throw new Error(`${NOT_AN_ARCHIVED_ACCOUNT}: ${meshId}`);
2988
+ throw new CoreError('ARCHIVE_NOT_ACCOUNT', [meshId]);
2974
2989
  }
2975
2990
  return row;
2976
2991
  }
@@ -3013,12 +3028,12 @@ export class Core extends ReadyResource {
3013
3028
  const meshKey = this._meshKeyOf(meshId);
3014
3029
  const registry = await this._writableAccount();
3015
3030
  if (!registry) {
3016
- throw new Error(MESH_WITHOUT_ACCOUNT);
3031
+ throw new CoreError('ACCOUNT_MESH_MISSING');
3017
3032
  }
3018
3033
  const ours = registry.ourMemberId;
3019
3034
  const joined = await registry.view.get(JOINED_MESHES, { meshKey });
3020
3035
  if (!ours || !joined || !b4a.equals(joined.memberId, ours)) {
3021
- throw new Error(`${NO_SUCH_MESH}: ${meshId}`);
3036
+ throw new CoreError('MESH_NOT_FOUND', [meshId]);
3022
3037
  }
3023
3038
  await registry.dispatch('put-joined-mesh', {
3024
3039
  room: {
@@ -3039,11 +3054,11 @@ export class Core extends ReadyResource {
3039
3054
  await this.ready();
3040
3055
  const trimmed = name.trim();
3041
3056
  if (!meshNameWithinBounds(trimmed)) {
3042
- throw new Error(`mesh name must be at most ${MAX_MESH_NAME_CHARS} characters`);
3057
+ throw new CoreError('MESH_NAME_TOO_LONG', [MAX_MESH_NAME_CHARS]);
3043
3058
  }
3044
3059
  const mesh = await requireMesh(this._meshPool(), meshId);
3045
3060
  if (this._isAccount(mesh)) {
3046
- throw new Error(MESH_NAME_ACCOUNT_REFUSED);
3061
+ throw new CoreError('MESH_NAME_ACCOUNT_REFUSED');
3047
3062
  }
3048
3063
  await mesh.setName(trimmed);
3049
3064
  }
@@ -3065,7 +3080,7 @@ export class Core extends ReadyResource {
3065
3080
  try {
3066
3081
  const registry = await this._writableAccount();
3067
3082
  if (!registry) {
3068
- throw new Error(MESH_WITHOUT_ACCOUNT);
3083
+ throw new CoreError('ACCOUNT_MESH_MISSING');
3069
3084
  }
3070
3085
  const resolved = await this._resolveSharedMesh(registry);
3071
3086
  return resolved ?? { meshId: await this._mintMesh({ shared: true, registry }) };
@@ -3129,20 +3144,20 @@ export class Core extends ReadyResource {
3129
3144
  async _mintMesh(opts) {
3130
3145
  const { shared, compute = false } = opts;
3131
3146
  if (this.closing) {
3132
- throw new Error('Core is closing');
3147
+ throw new CoreError('CORE_CLOSING', ['Core']);
3133
3148
  }
3134
3149
  if (this._blockedByMinVersion()) {
3135
- throw new Error(MESH_BELOW_FLOOR);
3150
+ throw new CoreError('MESH_BELOW_FLOOR');
3136
3151
  }
3137
3152
  if (this._leaving || this._minting) {
3138
- throw new Error('Mesh transition already in progress');
3153
+ throw new CoreError('MESH_TRANSITION_IN_PROGRESS');
3139
3154
  }
3140
3155
  const registry = opts.registry ?? (await this._writableAccount());
3141
3156
  if (shared && !registry) {
3142
- throw new Error(MESH_WITHOUT_ACCOUNT);
3157
+ throw new CoreError('ACCOUNT_MESH_MISSING');
3143
3158
  }
3144
3159
  if (registry && !registry.ourMemberId) {
3145
- throw new Error(ACCOUNT_MEMBER_UNKNOWN);
3160
+ throw new CoreError('ACCOUNT_MEMBER_UNKNOWN');
3146
3161
  }
3147
3162
  const mesh = await this._openMesh({ new: true, sharing: true, compute });
3148
3163
  this._wireMesh(this._poolEntry(mesh));
@@ -3150,7 +3165,7 @@ export class Core extends ReadyResource {
3150
3165
  try {
3151
3166
  await mesh.refreshOurMember();
3152
3167
  if (!mesh.ourMemberId) {
3153
- throw new Error(MESH_WITHOUT_MEMBER);
3168
+ throw new CoreError('MESH_WITHOUT_MEMBER');
3154
3169
  }
3155
3170
  if (registry) {
3156
3171
  await this._registerMesh(mesh.key, { shared, registry });
@@ -3181,13 +3196,13 @@ export class Core extends ReadyResource {
3181
3196
  await this._meshTransitionLock.lock();
3182
3197
  try {
3183
3198
  if (meshId === this._accountId) {
3184
- throw new Error('The account mesh cannot be closed');
3199
+ throw new CoreError('ACCOUNT_MESH_CLOSE_REFUSED');
3185
3200
  }
3186
3201
  const key = this._meshKeyOf(meshId);
3187
3202
  const mesh = this._pooled(meshId) ?? (await this._opening.get(meshId)?.catch(noop)) ?? null;
3188
3203
  if (!mesh) {
3189
3204
  if (!(await this.local.readMeshSession(key))) {
3190
- throw new Error(`${NO_SUCH_MESH}: ${meshId}`);
3205
+ throw new CoreError('MESH_NOT_FOUND', [meshId]);
3191
3206
  }
3192
3207
  return;
3193
3208
  }
@@ -3202,10 +3217,10 @@ export class Core extends ReadyResource {
3202
3217
  if (!this.opened)
3203
3218
  await this.ready();
3204
3219
  if (this.closing) {
3205
- throw new Error('Core is closing');
3220
+ throw new CoreError('CORE_CLOSING', ['Core']);
3206
3221
  }
3207
3222
  if (this._blockedByMinVersion()) {
3208
- throw new Error(MESH_BELOW_FLOOR);
3223
+ throw new CoreError('MESH_BELOW_FLOOR');
3209
3224
  }
3210
3225
  const pooled = this._pooled(meshId);
3211
3226
  if (pooled) {
@@ -3236,7 +3251,7 @@ export class Core extends ReadyResource {
3236
3251
  const key = this._meshKeyOf(meshId);
3237
3252
  const session = await this.local.readMeshSession(key);
3238
3253
  if (!session) {
3239
- throw new Error(`${NO_SUCH_MESH}: ${meshId}`);
3254
+ throw new CoreError('MESH_NOT_FOUND', [meshId]);
3240
3255
  }
3241
3256
  const stored = await this._storedAccountKey();
3242
3257
  const sharing = !stored || !b4a.equals(stored, key);
@@ -3298,8 +3313,9 @@ export class Core extends ReadyResource {
3298
3313
  return mesh.writable ? 'joined' : 'joining';
3299
3314
  }
3300
3315
  _meshKeyOf(meshId) {
3301
- if (!MESH_ID.test(meshId))
3302
- throw new Error(`${NO_SUCH_MESH}: ${meshId}`);
3316
+ if (!MESH_ID.test(meshId)) {
3317
+ throw new CoreError('MESH_NOT_FOUND', [meshId]);
3318
+ }
3303
3319
  return b4a.from(meshId, 'hex');
3304
3320
  }
3305
3321
  async removeDeviceWriter(writerKey, deviceId, mesh) {
@@ -3503,7 +3519,7 @@ export class Core extends ReadyResource {
3503
3519
  }
3504
3520
  const registry = await this._writableAccount();
3505
3521
  if (registry && (await this._isOwnMeshIn(registry, meshKey))) {
3506
- throw new Error(MESH_HELD_BY_SIBLING);
3522
+ throw new CoreError('MESH_HELD_BY_SIBLING');
3507
3523
  }
3508
3524
  }
3509
3525
  async getMeshInvite(opts = {}) {
@@ -3524,24 +3540,24 @@ export class Core extends ReadyResource {
3524
3540
  }
3525
3541
  const registry = await this._writableAccount();
3526
3542
  if (!registry?.ourMemberId) {
3527
- throw new Error(INVITE_ACCOUNT_UNREADABLE);
3543
+ throw new CoreError('INVITE_ACCOUNT_UNREADABLE');
3528
3544
  }
3529
3545
  const isOwnMesh = await this._isOwnMeshIn(registry, mesh.key);
3530
3546
  if (opts.memberInvite) {
3531
3547
  if (!isOwnMesh) {
3532
- throw new Error(MEMBER_INVITE_HOME_REFUSED);
3548
+ throw new CoreError('MEMBER_INVITE_ACCOUNT_REFUSED');
3533
3549
  }
3534
3550
  }
3535
3551
  else {
3536
3552
  if (isOwnMesh) {
3537
- throw new Error(DEVICE_INVITE_MESH_REFUSED);
3553
+ throw new CoreError('DEVICE_INVITE_MESH_REFUSED');
3538
3554
  }
3539
3555
  const key = await this._storedAccountKey();
3540
3556
  if (!key) {
3541
- throw new Error(INVITE_ACCOUNT_UNREADABLE);
3557
+ throw new CoreError('INVITE_ACCOUNT_UNREADABLE');
3542
3558
  }
3543
3559
  if (!b4a.equals(key, mesh.key)) {
3544
- throw new Error(DEVICE_INVITE_MESH_REFUSED);
3560
+ throw new CoreError('DEVICE_INVITE_MESH_REFUSED');
3545
3561
  }
3546
3562
  }
3547
3563
  const auto = mesh.autobee;
@@ -3591,14 +3607,14 @@ export class Core extends ReadyResource {
3591
3607
  const delegate = crypto.keyPair(deviceSeed);
3592
3608
  const deviceProof = await attestDeviceProof(delegate.publicKey, keyPair, own.memberProof);
3593
3609
  if (!(await delegationFits(deviceProof, delegate))) {
3594
- throw new Error(DEVICE_INVITE_CHAIN_TOO_LONG);
3610
+ throw new CoreError('DEVICE_INVITE_CHAIN_TOO_LONG');
3595
3611
  }
3596
3612
  return { deviceSeed, deviceProof };
3597
3613
  }
3598
3614
  previewMeshInvite(invite) {
3599
3615
  const decoded = decodeMeshInvite(invite);
3600
3616
  if (!decoded) {
3601
- throw new Error('Invalid mesh invite');
3617
+ throw new CoreError('INVITE_INVALID');
3602
3618
  }
3603
3619
  return { memberInvite: decoded.memberInvite ?? false, inviterName: decoded.inviterName };
3604
3620
  }
@@ -3636,21 +3652,21 @@ export class Core extends ReadyResource {
3636
3652
  const mesh = this._accountMesh();
3637
3653
  const deviceId = this.devicePublicKey;
3638
3654
  if (!mesh || !deviceId) {
3639
- throw new Error(NOT_THE_HOST);
3655
+ throw new CoreError('AGENT_NOT_HOSTED_HERE');
3640
3656
  }
3641
3657
  const agent = await getAgent(mesh, request.agentId);
3642
3658
  if (!agent) {
3643
- throw new Error(NO_SUCH_AGENT);
3659
+ throw new CoreError('AGENT_HOST_AGENT_UNKNOWN');
3644
3660
  }
3645
3661
  if (!agent.providerDeviceId) {
3646
- throw new Error(NOT_THE_HOST);
3662
+ throw new CoreError('AGENT_NOT_HOSTED_HERE');
3647
3663
  }
3648
3664
  if (b4a.equals(agent.providerDeviceId, deviceId)) {
3649
3665
  return this._storeHandedOffSecret(request, deviceId);
3650
3666
  }
3651
3667
  const host = await mesh.view.get(DEVICES, { id: agent.providerDeviceId });
3652
3668
  if (!host || host.revoked) {
3653
- throw new Error(HOST_REVOKED);
3669
+ throw new CoreError('AGENT_HOST_REVOKED');
3654
3670
  }
3655
3671
  return this._secretHandoff.send(agent.providerDeviceId, request);
3656
3672
  }
@@ -3658,7 +3674,7 @@ export class Core extends ReadyResource {
3658
3674
  const mesh = this._accountMesh();
3659
3675
  const deviceId = this.devicePublicKey;
3660
3676
  if (!mesh || !deviceId) {
3661
- throw new Error(NOT_THE_HOST);
3677
+ throw new CoreError('AGENT_NOT_HOSTED_HERE');
3662
3678
  }
3663
3679
  const receive = createSkillSecretReceiver({
3664
3680
  mesh,
@@ -3917,21 +3933,21 @@ export class Core extends ReadyResource {
3917
3933
  await this.ready();
3918
3934
  const deviceId = this.devicePublicKey;
3919
3935
  if (!mesh || !deviceId) {
3920
- throw new Error(NOT_THE_HOST);
3936
+ throw new CoreError('AGENT_NOT_HOSTED_HERE');
3921
3937
  }
3922
3938
  const agent = await getAgent(mesh, agentId);
3923
3939
  if (!agent) {
3924
- throw new Error(NO_SUCH_AGENT);
3940
+ throw new CoreError('AGENT_HOST_AGENT_UNKNOWN');
3925
3941
  }
3926
3942
  if (!agent.providerDeviceId) {
3927
- throw new Error(NOT_THE_HOST);
3943
+ throw new CoreError('AGENT_NOT_HOSTED_HERE');
3928
3944
  }
3929
3945
  if (b4a.equals(agent.providerDeviceId, deviceId)) {
3930
3946
  return null;
3931
3947
  }
3932
3948
  const host = await mesh.view.get(DEVICES, { id: agent.providerDeviceId });
3933
3949
  if (!host || host.revoked) {
3934
- throw new Error(HOST_REVOKED);
3950
+ throw new CoreError('AGENT_HOST_REVOKED');
3935
3951
  }
3936
3952
  return agent.providerDeviceId;
3937
3953
  }
@@ -3955,7 +3971,7 @@ export class Core extends ReadyResource {
3955
3971
  });
3956
3972
  if (!host) {
3957
3973
  if (!store) {
3958
- throw new Error(NO_RECORD_STORE);
3974
+ throw new CoreError('INFERENCE_RECORDING_OFF');
3959
3975
  }
3960
3976
  return own;
3961
3977
  }
@@ -3977,11 +3993,11 @@ export class Core extends ReadyResource {
3977
3993
  const mesh = (await this._meshHoldingChat(request.chatId)) ?? this._accountMesh();
3978
3994
  const deviceId = this.devicePublicKey;
3979
3995
  if (!mesh || !deviceId) {
3980
- throw new Error(NOT_THE_HOST);
3996
+ throw new CoreError('AGENT_NOT_HOSTED_HERE');
3981
3997
  }
3982
3998
  const store = this._inferenceCalls;
3983
3999
  if (!store) {
3984
- throw new Error(NO_RECORD_STORE);
4000
+ throw new CoreError('INFERENCE_RECORDING_OFF');
3985
4001
  }
3986
4002
  const receive = createInferenceCallsReceiver({
3987
4003
  mesh,
@@ -4001,7 +4017,7 @@ export class Core extends ReadyResource {
4001
4017
  const mesh = this._accountMesh();
4002
4018
  const deviceId = this.devicePublicKey;
4003
4019
  if (!mesh || !deviceId) {
4004
- throw new Error(NOT_THE_HOST);
4020
+ throw new CoreError('AGENT_NOT_HOSTED_HERE');
4005
4021
  }
4006
4022
  const receive = createSkillOverlayReceiver({
4007
4023
  mesh,
@@ -4032,7 +4048,7 @@ export class Core extends ReadyResource {
4032
4048
  removed: ((await mgmt?.deleteOverlaySkill(op.name ?? '')) ?? { removed: false }).removed
4033
4049
  };
4034
4050
  default:
4035
- throw new Error(`unknown skill overlay op: ${op.op}`);
4051
+ throw new CoreError('SKILL_OVERLAY_OP_UNKNOWN', [op.op]);
4036
4052
  }
4037
4053
  }
4038
4054
  async _setupFields(skillName) {