@qvac/core 1.0.0-rc.8 → 1.0.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 (226) hide show
  1. package/CHANGELOG.md +234 -3
  2. package/README.md +1 -0
  3. package/dist-lib/lib/api/attachments.d.ts +1 -1
  4. package/dist-lib/lib/api/attachments.js +11 -0
  5. package/dist-lib/lib/api/chunks.d.ts +4 -2
  6. package/dist-lib/lib/api/chunks.js +6 -3
  7. package/dist-lib/lib/api/devices.js +6 -18
  8. package/dist-lib/lib/api/execution.d.ts +6 -2
  9. package/dist-lib/lib/api/execution.js +29 -3
  10. package/dist-lib/lib/api/host-run-log.d.ts +7 -1
  11. package/dist-lib/lib/api/host-run-log.js +3 -1
  12. package/dist-lib/lib/api/index.d.ts +3 -1
  13. package/dist-lib/lib/api/index.js +17 -6
  14. package/dist-lib/lib/api/members.js +13 -29
  15. package/dist-lib/lib/api/mesh.d.ts +2 -1
  16. package/dist-lib/lib/api/projects.d.ts +1 -1
  17. package/dist-lib/lib/api/projects.js +9 -5
  18. package/dist-lib/lib/api/rag-label.d.ts +4 -0
  19. package/dist-lib/lib/api/rag-label.js +21 -0
  20. package/dist-lib/lib/api/rag-lexical.d.ts +22 -0
  21. package/dist-lib/lib/api/rag-lexical.js +1058 -0
  22. package/dist-lib/lib/api/rag-manifest.d.ts +13 -0
  23. package/dist-lib/lib/api/rag-manifest.js +46 -0
  24. package/dist-lib/lib/api/rag-outline.d.ts +36 -0
  25. package/dist-lib/lib/api/rag-outline.js +426 -0
  26. package/dist-lib/lib/api/rag-plugin.d.ts +20 -0
  27. package/dist-lib/lib/api/rag-plugin.js +70 -0
  28. package/dist-lib/lib/api/rag-synthesis.d.ts +7 -0
  29. package/dist-lib/lib/api/rag-synthesis.js +102 -0
  30. package/dist-lib/lib/api/rag-targets.d.ts +23 -0
  31. package/dist-lib/lib/api/rag-targets.js +215 -0
  32. package/dist-lib/lib/api/rag-window.d.ts +5 -0
  33. package/dist-lib/lib/api/rag-window.js +46 -0
  34. package/dist-lib/lib/api/rag.d.ts +3 -2
  35. package/dist-lib/lib/api/rag.js +421 -181
  36. package/dist-lib/lib/api/served.js +8 -14
  37. package/dist-lib/lib/api/workspace.d.ts +8 -1
  38. package/dist-lib/lib/api/workspace.js +27 -13
  39. package/dist-lib/lib/assistant/harness.d.ts +9 -1
  40. package/dist-lib/lib/assistant/history.d.ts +11 -2
  41. package/dist-lib/lib/assistant/history.js +74 -4
  42. package/dist-lib/lib/assistant/media-round.d.ts +61 -0
  43. package/dist-lib/lib/assistant/media-round.js +111 -0
  44. package/dist-lib/lib/assistant/message.d.ts +7 -0
  45. package/dist-lib/lib/assistant/model-round.d.ts +12 -0
  46. package/dist-lib/lib/assistant/model-round.js +130 -17
  47. package/dist-lib/lib/assistant/pdf-pages.d.ts +16 -1
  48. package/dist-lib/lib/assistant/pdf-pages.js +67 -3
  49. package/dist-lib/lib/assistant/round-host.d.ts +22 -0
  50. package/dist-lib/lib/assistant/round-host.js +142 -8
  51. package/dist-lib/lib/assistant/upload.d.ts +12 -0
  52. package/dist-lib/lib/assistant/upload.js +36 -0
  53. package/dist-lib/lib/assistant-client.d.ts +1 -0
  54. package/dist-lib/lib/assistant-client.js +40 -19
  55. package/dist-lib/lib/assistant-tree.d.ts +2 -0
  56. package/dist-lib/lib/assistant-tree.js +41 -1
  57. package/dist-lib/lib/assistant.d.ts +15 -0
  58. package/dist-lib/lib/assistant.js +474 -89
  59. package/dist-lib/lib/client.d.ts +1 -0
  60. package/dist-lib/lib/client.js +4 -1
  61. package/dist-lib/lib/compaction.js +2 -2
  62. package/dist-lib/lib/core/agent.js +12 -45
  63. package/dist-lib/lib/core/approval-rules.js +1 -8
  64. package/dist-lib/lib/core/archived-mesh.d.ts +2 -1
  65. package/dist-lib/lib/core/archived-mesh.js +12 -8
  66. package/dist-lib/lib/core/chat-copy.d.ts +18 -3
  67. package/dist-lib/lib/core/chat-copy.js +74 -25
  68. package/dist-lib/lib/core/chat-fork.js +15 -6
  69. package/dist-lib/lib/core/chat-group.js +16 -24
  70. package/dist-lib/lib/core/chat-move.d.ts +9 -0
  71. package/dist-lib/lib/core/chat-move.js +94 -3
  72. package/dist-lib/lib/core/chat-order.js +12 -18
  73. package/dist-lib/lib/core/chat.js +38 -31
  74. package/dist-lib/lib/core/chunk.d.ts +2 -2
  75. package/dist-lib/lib/core/chunk.js +18 -25
  76. package/dist-lib/lib/core/collections.d.ts +2 -0
  77. package/dist-lib/lib/core/collections.js +2 -0
  78. package/dist-lib/lib/core/contract/activity.d.ts +4 -0
  79. package/dist-lib/lib/core/contract/activity.js +34 -4
  80. package/dist-lib/lib/core/contract/chat-group.d.ts +1 -0
  81. package/dist-lib/lib/core/contract/chat-group.js +21 -1
  82. package/dist-lib/lib/core/contract/chat.d.ts +9 -0
  83. package/dist-lib/lib/core/contract/chat.js +127 -5
  84. package/dist-lib/lib/core/contract/chunks.d.ts +1 -0
  85. package/dist-lib/lib/core/contract/chunks.js +37 -6
  86. package/dist-lib/lib/core/contract/features.d.ts +21 -0
  87. package/dist-lib/lib/core/contract/features.js +22 -0
  88. package/dist-lib/lib/core/contract/index.js +2 -0
  89. package/dist-lib/lib/core/contract/indexing.js +4 -2
  90. package/dist-lib/lib/core/contract/knowledge.js +5 -4
  91. package/dist-lib/lib/core/contract/members.js +16 -10
  92. package/dist-lib/lib/core/contract/mesh-kind.d.ts +1 -0
  93. package/dist-lib/lib/core/contract/mesh-kind.js +3 -1
  94. package/dist-lib/lib/core/contract/projects.d.ts +5 -0
  95. package/dist-lib/lib/core/contract/projects.js +13 -5
  96. package/dist-lib/lib/core/contract/roles.js +32 -4
  97. package/dist-lib/lib/core/contract/run-requests.d.ts +1 -0
  98. package/dist-lib/lib/core/contract/run-requests.js +35 -2
  99. package/dist-lib/lib/core/contract/seed-defaults.js +2 -2
  100. package/dist-lib/lib/core/contract/tool-configs.js +3 -2
  101. package/dist-lib/lib/core/contract/workspace.d.ts +2 -0
  102. package/dist-lib/lib/core/contract/workspace.js +40 -7
  103. package/dist-lib/lib/core/contract/writers.js +2 -2
  104. package/dist-lib/lib/core/departures.d.ts +3 -1
  105. package/dist-lib/lib/core/departures.js +3 -0
  106. package/dist-lib/lib/core/devices.js +1 -8
  107. package/dist-lib/lib/core/files/attachments.d.ts +2 -0
  108. package/dist-lib/lib/core/files/attachments.js +19 -1
  109. package/dist-lib/lib/core/files/index.d.ts +20 -4
  110. package/dist-lib/lib/core/files/index.js +132 -21
  111. package/dist-lib/lib/core/files/processing/document.d.ts +4 -0
  112. package/dist-lib/lib/core/files/processing/document.js +7 -0
  113. package/dist-lib/lib/core/files/processing/index.d.ts +13 -0
  114. package/dist-lib/lib/core/files/processing/index.js +144 -12
  115. package/dist-lib/lib/core/files/processing/protocol.js +13 -34
  116. package/dist-lib/lib/core/files/processing/state.d.ts +2 -0
  117. package/dist-lib/lib/core/files/rag/build.d.ts +19 -2
  118. package/dist-lib/lib/core/files/rag/build.js +123 -19
  119. package/dist-lib/lib/core/files/rag/failure.js +2 -1
  120. package/dist-lib/lib/core/files/rag/index.d.ts +11 -3
  121. package/dist-lib/lib/core/files/rag/index.js +13 -1
  122. package/dist-lib/lib/core/files/rag/load.d.ts +6 -3
  123. package/dist-lib/lib/core/files/rag/load.js +27 -6
  124. package/dist-lib/lib/core/files/rag/plugin-index.d.ts +32 -0
  125. package/dist-lib/lib/core/files/rag/plugin-index.js +127 -0
  126. package/dist-lib/lib/core/files/rag/produce.d.ts +5 -3
  127. package/dist-lib/lib/core/files/rag/produce.js +104 -16
  128. package/dist-lib/lib/core/files/rag/retrieval/full-text.d.ts +3 -1
  129. package/dist-lib/lib/core/files/rag/retrieval/full-text.js +62 -13
  130. package/dist-lib/lib/core/files/rag/text-cache.d.ts +28 -0
  131. package/dist-lib/lib/core/files/rag/text-cache.js +141 -0
  132. package/dist-lib/lib/core/files/records/knowledge.d.ts +1 -3
  133. package/dist-lib/lib/core/files/records/knowledge.js +7 -40
  134. package/dist-lib/lib/core/files/records/reclaim.js +5 -10
  135. package/dist-lib/lib/core/files/upload.js +68 -41
  136. package/dist-lib/lib/core/fs-drives.js +14 -26
  137. package/dist-lib/lib/core/invite.d.ts +1 -0
  138. package/dist-lib/lib/core/invite.js +4 -2
  139. package/dist-lib/lib/core/local/files.d.ts +1 -1
  140. package/dist-lib/lib/core/local/files.js +5 -5
  141. package/dist-lib/lib/core/local/index.d.ts +13 -6
  142. package/dist-lib/lib/core/local/index.js +103 -69
  143. package/dist-lib/lib/core/local/run-logs.d.ts +1 -1
  144. package/dist-lib/lib/core/local/run-logs.js +5 -5
  145. package/dist-lib/lib/core/mesh-processor.d.ts +19 -0
  146. package/dist-lib/lib/core/mesh-processor.js +87 -0
  147. package/dist-lib/lib/core/mesh.d.ts +48 -5
  148. package/dist-lib/lib/core/mesh.js +318 -129
  149. package/dist-lib/lib/core/migrations/agent-catalog.js +2 -8
  150. package/dist-lib/lib/core/migrations/agent-tools-scope.js +5 -11
  151. package/dist-lib/lib/core/migrations/workspace-from-0-21/index.d.ts +88 -0
  152. package/dist-lib/lib/core/migrations/workspace-from-0-21/index.js +731 -0
  153. package/dist-lib/lib/core/migrations/writer-links.d.ts +2 -0
  154. package/dist-lib/lib/core/migrations/writer-links.js +32 -0
  155. package/dist-lib/lib/core/project-keys.d.ts +6 -0
  156. package/dist-lib/lib/core/project-keys.js +11 -0
  157. package/dist-lib/lib/core/public-mesh.d.ts +3 -0
  158. package/dist-lib/lib/core/public-mesh.js +10 -8
  159. package/dist-lib/lib/core/run-requests.d.ts +3 -0
  160. package/dist-lib/lib/core/run-requests.js +21 -1
  161. package/dist-lib/lib/core/skill-availability.d.ts +33 -0
  162. package/dist-lib/lib/core/skill-availability.js +67 -0
  163. package/dist-lib/lib/core/sync/background.d.ts +0 -1
  164. package/dist-lib/lib/core/sync/background.js +2 -1
  165. package/dist-lib/lib/core/sync/changed.d.ts +4 -2
  166. package/dist-lib/lib/core/sync/changed.js +1 -1
  167. package/dist-lib/lib/core/sync/index.js +52 -10
  168. package/dist-lib/lib/core/sync/view-updates.d.ts +18 -12
  169. package/dist-lib/lib/core/sync/view-updates.js +82 -91
  170. package/dist-lib/lib/core/sync-status.js +97 -41
  171. package/dist-lib/lib/core/tool-activity.js +6 -12
  172. package/dist-lib/lib/core/tool-configs.js +17 -29
  173. package/dist-lib/lib/core/upgrade-summary.d.ts +19 -0
  174. package/dist-lib/lib/core/upgrade-summary.js +67 -0
  175. package/dist-lib/lib/core/view-read.d.ts +4 -0
  176. package/dist-lib/lib/core/view-read.js +23 -0
  177. package/dist-lib/lib/core/workspace-upgrade.d.ts +7 -0
  178. package/dist-lib/lib/core/workspace-upgrade.js +27 -16
  179. package/dist-lib/lib/core.d.ts +39 -42
  180. package/dist-lib/lib/core.js +661 -639
  181. package/dist-lib/lib/errors.d.ts +20 -0
  182. package/dist-lib/lib/errors.js +20 -0
  183. package/dist-lib/lib/llm-model-names.d.ts +1 -1
  184. package/dist-lib/lib/llm-model-names.js +1 -1
  185. package/dist-lib/lib/mesh-scoped-engine.js +4 -0
  186. package/dist-lib/lib/model-context.js +1 -2
  187. package/dist-lib/lib/probe-rounds.d.ts +7 -2
  188. package/dist-lib/lib/probe-rounds.js +6 -1
  189. package/dist-lib/lib/run-transcript.d.ts +4 -1
  190. package/dist-lib/lib/run-transcript.js +15 -6
  191. package/dist-lib/schema/engine.js +83 -6
  192. package/dist-lib/schema/indexes.d.ts +3 -0
  193. package/dist-lib/schema/indexes.js +5 -0
  194. package/dist-lib/schema/local.js +11 -2
  195. package/dist-lib/schema/qvac.js +49 -5
  196. package/dist-lib/spec/bind.js +34 -0
  197. package/dist-lib/spec/calls.js +5 -0
  198. package/dist-lib/spec/capabilities.d.ts +17 -0
  199. package/dist-lib/spec/hrpc/hrpc.json +65 -0
  200. package/dist-lib/spec/hrpc/index.js +68 -5
  201. package/dist-lib/spec/hrpc/messages.js +260 -23
  202. package/dist-lib/spec/hyperschema/index.js +260 -23
  203. package/dist-lib/spec/hyperschema/schema.json +181 -0
  204. package/dist-lib/spec/hyperschema/types.d.ts +48 -0
  205. package/dist-lib/spec/local/hyperdb/messages.js +270 -224
  206. package/dist-lib/spec/local/hyperschema/index.js +73 -38
  207. package/dist-lib/spec/local/hyperschema/schema.json +31 -0
  208. package/dist-lib/spec/local/hyperschema/types.d.ts +8 -0
  209. package/dist-lib/spec/mesh/hyperdb/db.json +39 -2
  210. package/dist-lib/spec/mesh/hyperdb/index.js +120 -3
  211. package/dist-lib/spec/mesh/hyperdb/messages.js +1328 -838
  212. package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +30 -0
  213. package/dist-lib/spec/mesh/hyperdispatch/index.js +86 -1
  214. package/dist-lib/spec/mesh/hyperdispatch/messages.js +682 -287
  215. package/dist-lib/spec/mesh/hyperschema/index.js +682 -287
  216. package/dist-lib/spec/mesh/hyperschema/schema.json +201 -0
  217. package/dist-lib/spec/mesh/hyperschema/types.d.ts +52 -0
  218. package/package.json +75 -70
  219. package/tui/README.md +2 -1
  220. package/tui/app.mjs +28 -7
  221. package/tui/boot.mjs +5 -1
  222. package/tui/lib/attachment-files.mjs +82 -0
  223. package/tui/lib/reopen-meshes.mjs +15 -0
  224. package/tui/main.mjs +4 -43
  225. package/worker/assistant-sidecar.mjs +10 -3
  226. package/worker/stow-entry.mjs +3 -52
@@ -1,9 +1,11 @@
1
+ import HypercoreModule from 'hypercore';
1
2
  import b4a from 'b4a';
2
3
  import crypto from 'hypercore-crypto';
3
4
  import BlindPeering from 'blind-peering';
4
5
  import Corestore from 'corestore';
5
6
  import CorestoreStorage from 'hypercore-storage';
6
7
  import { constants as RocksConstants } from 'rocksdb-native';
8
+ import fs from 'fs';
7
9
  import path from 'path';
8
10
  import Rache from 'rache';
9
11
  import Hyperswarm from 'hyperswarm';
@@ -19,32 +21,33 @@ import { requireOurAdmin, requireWorkspace } from './api/members.js';
19
21
  import { projectName } from './api/projects.js';
20
22
  import { createServedApi } from './api/served.js';
21
23
  import { firstEvent } from './core/first-event.js';
22
- import { departedMembers, isWorkspace, lastAdminAmongOthers, liveMemberIds, readMemberKey, upgradeMembers } from './core/contract/workspace.js';
24
+ import { departedMembers, isWorkspace, lastAdminAmongOthers, readMemberKey } from './core/contract/workspace.js';
23
25
  import { readPublicSeed } from './core/contract/public-seeds.js';
24
26
  import { openSealedToDevice, sealToDevice } from './core/device-seal.js';
25
27
  import { removedBy } from './core/contract/invites.js';
26
- import { meshKind } from './core/contract/mesh-kind.js';
27
- import { WORKSPACES_RULES, bornUnderRules, rulesVersion } from './core/contract/mesh-rules.js';
28
- import { inProject, listedProject, listsMember, liveMember, projectMember, projectOfGroup, projectKeys, readProject, UNGROUPED } from './core/contract/projects.js';
28
+ import { isSharedKind, meshKind } from './core/contract/mesh-kind.js';
29
+ import { bornUnderRules } from './core/contract/mesh-rules.js';
30
+ import { inProject, listedProject, listsMember, liveMember, projectMember, projectKeys, readProject } from './core/contract/projects.js';
29
31
  import { requireMesh } from './api/current-mesh.js';
30
- import { CoreError } from './errors.js';
32
+ import { CoreError, isCoreError } from './errors.js';
31
33
  import { ChatMoves } from './core/chat-move.js';
32
34
  import { purgeCores } from './core/purge.js';
33
35
  import { copyContent } from './core/chat-copy.js';
36
+ import { isRecord } from './guards.js';
34
37
  import { followAgentCopies, keepCopyChats } from './core/agent-copies.js';
35
38
  import { cancelDeletedAgentRuns } from './core/deleted-agent-runs.js';
36
39
  import { devicesOfMember } from './core/contract/activity.js';
37
40
  import { followPickedAgents, followSharedAgents, ownerOf, liveSharedAgent, pickAgent, shareAgent, unpickAgent } from './core/shared-agents.js';
38
41
  import { inRole, publicRoute } from './core/contract/roles.js';
39
- import { freshPublicRecord, isPublicRecord, leftPublicRecordKey, derivedSeed, oldChats, ourRows, publicMeshKey, publicRecordKey, publicSeed, servesAgents, servesChat, servesChunk } from './core/public-mesh.js';
42
+ import { freshPublicRecord, isPublicRecord, leftPublicRecordKey, derivedSeed, firstWorkspace, oldChats, ourRows, publicMeshKey, publicRecordKey, publicRecordsOf, publicSeed, servesAgents, servesChat, servesChunk } from './core/public-mesh.js';
40
43
  import { memberBoxKeyPair, openSealedToMember } from './core/member-key.js';
41
- import { dueProjectDeviceKeys, dueProjectHostKey, dueProjectKey, seedFingerprint, seedOpens } from './core/project-keys.js';
44
+ import { dueProjectHostKey, sealMembers, seedFingerprint, seedOpens } from './core/project-keys.js';
42
45
  import { projectDeviceKeys } from './core/contract/project-device-keys.js';
43
46
  import { agentJoin, freshHostRecord, hostRecordKey, hostState, hostTargets, isHostRecord } from './core/agent-hosting.js';
44
47
  import { hostForWriter } from './core/contract/model-requests.js';
45
48
  import { asEncoded, watchModels } from './models-watch.js';
46
49
  import { BLIND_PEER_KEYS, BLIND_PEERING_OFF_KEY, BLIND_RELAY_KEYS } from './core/blind-peers.js';
47
- import { CoreBlobServer, MemoryVectorIndex } from './core/files/index.js';
50
+ import { CoreBlobServer, MemoryVectorIndex, PluginVectorIndex } from './core/files/index.js';
48
51
  import { removeChatAttachments, removeChatKnowledge } from './core/chat.js';
49
52
  import { rebuildChatOrder, refreshChatOrder, retireGoneChats } from './core/chat-order.js';
50
53
  import { createFsDriveHost } from './core/fs-drives.js';
@@ -53,11 +56,14 @@ import { decodeMeshInvite, encodeMeshInvite } from './core/invite.js';
53
56
  import { fileKeyOf, knownFile, referencedFile } from './core/files/local-index.js';
54
57
  import { openLocalStore, recordedRole } from './core/local/index.js';
55
58
  import { backgroundSyncKey } from './core/local/files.js';
56
- import { FOLD_PENDING, GROUPS_PENDING, agentsFoldKey, groupBuckets, groupRecordKey, isGroupRecord, liveGroups, projectsByGroup, upgradeCheckedKey } from './core/workspace-upgrade.js';
57
- import { foldAgents, ownCopies } from './core/upgrade-agents.js';
59
+ import { UPGRADE_RECORDS } from './core/workspace-upgrade.js';
60
+ import { convertedHere, groupRecordsByWorkspace, ifClosing, migratedWorkspace, oldMessages, recordsGroup, upgradedWorkspace } from './core/upgrade-summary.js';
61
+ import { WorkspaceUpgrade } from './core/migrations/workspace-from-0-21/index.js';
62
+ import { MeshProcessor } from './core/mesh-processor.js';
58
63
  import { MAX_MESH_NAME_CHARS, meshNameWithinBounds } from '../schema/qvac.js';
59
64
  import { getDefaultAgent, mustGetActiveAgent, rehomeAgents } from './core/agent.js';
60
65
  import { pickRunner } from './core/run-requests.js';
66
+ import { readView } from './core/view-read.js';
61
67
  import { countContents, migrateContents, withArchivedMesh } from './core/archived-mesh.js';
62
68
  import { isMobilePlatform } from '@qvac/harness/lib/models/model-catalog.ts';
63
69
  import { storeSkillSecret, syncSkillSecrets } from './core/skill-secrets.js';
@@ -69,13 +75,14 @@ import { claimOwnMembership, legacyNameStamp, namedAt, renameFoundedMember } fro
69
75
  import { runMigrations } from './core/migrations/index.js';
70
76
  import { backfillFileIndex } from './core/migrations/file-index.js';
71
77
  import { REINDEX_FROM_APP_VERSION, backfillChatChunks, floorExcludesBefore } from './core/migrations/chat-chunks.js';
78
+ import { backfillWriterLinks } from './core/migrations/writer-links.js';
72
79
  import { advertiseDeviceVersion, retractInventory } from './core/devices.js';
73
80
  import { DeviceControl } from './core/device-control.js';
74
81
  import { forwardToDevices, servedToSiblings } from './api/device-routes.js';
75
82
  import { OtaConfigReader, configCoreFor, resolveConfigChannel, vendoredMinSupportedVersion } from './core/ota-config.js';
76
83
  import { updateRequired } from './core/min-version.js';
77
84
  import { PendingDepartures, waitForDelivery } from './core/departures.js';
78
- import { Mesh, SCHEMA_VERSION } from './core/mesh.js';
85
+ import { Mesh, SCHEMA_VERSION, logErrors } from './core/mesh.js';
79
86
  import { raceResource } from './race-resource.js';
80
87
  import { createSystemInfoSampler } from './core/system-info.js';
81
88
  import { projectSenders } from './core/senders.js';
@@ -93,7 +100,6 @@ const QVAC_CORE_NAMESPACE = 'qvac-core';
93
100
  const MESH_ID = /^[0-9a-f]{64}$/;
94
101
  export const DEVICE_UNKNOWN = 'This device has no device details: pass `device` when creating Core';
95
102
  const RECOVERY_WAIT_MS = 30_000;
96
- const WINNER_ADMISSION_WAIT_MS = 60_000;
97
103
  const TOPIC_RETRY_MS = [1_000, 5_000, 15_000];
98
104
  const MESH_DIED = 'the local database died';
99
105
  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';
@@ -101,7 +107,40 @@ const INVITE_DHT_NODES_LIMIT = 3;
101
107
  const INVITE_BLIND_PEERS_LIMIT = 3;
102
108
  const INVITE_PUBLIC_HEADS_LIMIT = 3;
103
109
  const DIAGNOSTICS_SAMPLE_MS = 2000;
110
+ export const PEER_STREAM_TIMEOUT_MS = 10_000;
111
+ const PEER_KEEP_ALIVE_MS = 3_000;
112
+ const MESH_WRITER = {
113
+ role: 'role',
114
+ sharedAgents: 'shared-agents',
115
+ projectKeys: 'project-keys',
116
+ hosting: 'hosting',
117
+ joinedMeshes: 'joined-meshes',
118
+ projects: 'projects',
119
+ memberProof: 'member-proof',
120
+ memberKey: 'member-key',
121
+ upgrade: 'upgrade'
122
+ };
123
+ const PASS_ORDER = [
124
+ MESH_WRITER.role,
125
+ MESH_WRITER.sharedAgents,
126
+ MESH_WRITER.projectKeys,
127
+ MESH_WRITER.hosting,
128
+ MESH_WRITER.joinedMeshes,
129
+ MESH_WRITER.projects,
130
+ MESH_WRITER.memberProof,
131
+ MESH_WRITER.memberKey,
132
+ MESH_WRITER.upgrade
133
+ ];
134
+ const STARTED = new Set([
135
+ MESH_WRITER.sharedAgents,
136
+ MESH_WRITER.projectKeys,
137
+ MESH_WRITER.hosting,
138
+ MESH_WRITER.joinedMeshes,
139
+ MESH_WRITER.projects
140
+ ]);
104
141
  const PUBLIC_FOLLOW_ATTEMPTS = 3;
142
+ const recoveryPeers = HypercoreModule;
143
+ recoveryPeers.setRecoveryPeers([]);
105
144
  export class Core extends ReadyResource {
106
145
  _storagePath;
107
146
  store;
@@ -115,6 +154,7 @@ export class Core extends ReadyResource {
115
154
  _transport;
116
155
  _apiLifecycle;
117
156
  _log;
157
+ _onerror;
118
158
  _spec;
119
159
  _bootstrapNodes;
120
160
  _network;
@@ -158,6 +198,7 @@ export class Core extends ReadyResource {
158
198
  _speakStream;
159
199
  _embedder;
160
200
  _ragExtractors;
201
+ _knowledge = null;
161
202
  _indexedEmbedder = null;
162
203
  _indexingLock = new ScopeLock();
163
204
  _embedBatches = new ScopeLock();
@@ -167,6 +208,7 @@ export class Core extends ReadyResource {
167
208
  };
168
209
  _meshTransitionLock = new ScopeLock();
169
210
  _opening = new Map();
211
+ _openingMeshSessions = new Set();
170
212
  _retiring = new Map();
171
213
  _tearingDown = new Map();
172
214
  _debugBeforeMeshOpen;
@@ -186,6 +228,7 @@ export class Core extends ReadyResource {
186
228
  _accountId;
187
229
  _offlineBoot = new ReadyGuard();
188
230
  onlineBoot = new ReadyGuard();
231
+ _upgradeBoot = null;
189
232
  _candidateMesh;
190
233
  _cancelCandidateJoin;
191
234
  _joinCancellation;
@@ -206,9 +249,7 @@ export class Core extends ReadyResource {
206
249
  _wakeup;
207
250
  _following = new Map();
208
251
  _kickedProjects = new Map();
209
- _upgrading = new Set();
210
- _groupPasses = new WeakSet();
211
- _groupPassesDue = new WeakSet();
252
+ _upgrade = new WorkspaceUpgrade(this._upgradeHost());
212
253
  _purging = new Map();
213
254
  _publicIds = new Map();
214
255
  _publicBoots = new Map();
@@ -221,12 +262,17 @@ export class Core extends ReadyResource {
221
262
  _publicFollow = new ScopeLock({ debounce: true });
222
263
  _publicMerges = new Map();
223
264
  _publicDrops = new Map();
224
- _publicMigration = null;
225
- _copyAgain = false;
226
- _accountCopied = null;
265
+ _publicSettles = new Map();
227
266
  _agentCopies = new ScopeLock({ debounce: true });
228
267
  _sharedAgents = new ScopeLock({ debounce: true });
229
- _memberKeyWrites = new WeakMap();
268
+ _passFailed = (err) => {
269
+ if (!this.closing) {
270
+ this._log?.warn(['p2p', 'mesh'], 'a mesh pass failed', err);
271
+ }
272
+ };
273
+ _processor = new MeshProcessor((mesh, due) => this._reconcile(mesh, due), this._passFailed);
274
+ _roleSettles = new Set();
275
+ _recordingJoins = new WeakSet();
230
276
  _projectKeySeals = new ScopeLock({ debounce: true });
231
277
  _hosting = new ScopeLock({ debounce: true });
232
278
  _skillSecrets = new ScopeLock({ debounce: true });
@@ -263,7 +309,7 @@ export class Core extends ReadyResource {
263
309
  _systemInfo;
264
310
  _statusError;
265
311
  _upgradeRequired;
266
- constructor(transport, { storagePath, skillSecrets = undefined, 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, network = undefined }) {
312
+ constructor(transport, { storagePath, skillSecrets = undefined, store = undefined, swarm = undefined, wakeup = undefined, logger = null, onerror = undefined, 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, network = undefined }) {
267
313
  super();
268
314
  this._transport = transport;
269
315
  this._apiLifecycle = apiLifecycle ?? {
@@ -271,6 +317,7 @@ export class Core extends ReadyResource {
271
317
  resume: () => this.resume()
272
318
  };
273
319
  this._log = logger;
320
+ this._onerror = onerror ?? logErrors(logger);
274
321
  if (swarm && (!store || !wakeup)) {
275
322
  throw new Error('swarm may only be passed alongside store and wakeup');
276
323
  }
@@ -285,7 +332,7 @@ export class Core extends ReadyResource {
285
332
  });
286
333
  this.rootCorestore = store ?? new Corestore(this.storage, { globalCache: new Rache() });
287
334
  this.store = this.rootCorestore.namespace(QVAC_CORE_NAMESPACE);
288
- this.local = openLocalStore(this.storage);
335
+ this.local = openLocalStore(this.storage, this._log);
289
336
  this.state = QvacState.RESUME;
290
337
  this._spec = spec;
291
338
  this._bootstrapNodes = bootstrapNodes;
@@ -443,11 +490,13 @@ export class Core extends ReadyResource {
443
490
  await writeAutoDownloadModels(this.local, this._autoDownloadModels);
444
491
  }
445
492
  const swarm = this._openSwarm(this._deviceKeyPair);
493
+ recoveryPeers.setRecoveryPeers([...this._resolveBlindPeerKeys()]);
446
494
  this._departures = new PendingDepartures(this.store, this.local, {
447
495
  swarm,
448
496
  isPooled: (key) => this._pooled(b4a.toHex(key)) !== null,
449
497
  deviceId: () => this._deviceKeyPair?.publicKey ?? null,
450
498
  logger: this._log,
499
+ onerror: this._onerror,
451
500
  blindPeerKeys: this._resolveBlindPeerKeys(),
452
501
  blindPeering: () => this._blindPeeringClient(),
453
502
  wakeup: this._wakeup,
@@ -521,11 +570,12 @@ export class Core extends ReadyResource {
521
570
  lifecycle: this._apiLifecycle,
522
571
  ragIndex: (meshId) => this._meshes.get(this._publicIdOf(meshId) ?? meshId)?.vectorIndex ?? null,
523
572
  ragEmbed: () => this._indexedEmbedder,
573
+ ragExtractors: () => this._ragExtractors,
524
574
  platform: () => this._localDevice?.platform ?? null,
525
575
  deletedProject: (projectId) => this._projectGone(projectId),
526
576
  projectOf: (workspaceKey, projectKey) => this._isProjectOf(workspaceKey, projectKey),
527
- nameOf: (projectKey) => {
528
- const id = b4a.toHex(projectKey);
577
+ nameOf: (meshKey) => {
578
+ const id = b4a.toHex(meshKey);
529
579
  return this._pooled(id)?.name ?? this._meshNames.get(id) ?? null;
530
580
  },
531
581
  pushSkillSecret: (request) => this.pushSkillSecret(request),
@@ -736,7 +786,8 @@ export class Core extends ReadyResource {
736
786
  const mesh = await this._switchToMesh({
737
787
  seed: this._meshSeed,
738
788
  baseKey: this._meshKey,
739
- account: true
789
+ account: true,
790
+ boot: true
740
791
  });
741
792
  }
742
793
  catch (err) {
@@ -761,7 +812,11 @@ export class Core extends ReadyResource {
761
812
  if (mesh) {
762
813
  this._bringMeshOnline(mesh);
763
814
  }
764
- void this._openUpgradable();
815
+ this._upgradeBoot = Promise.race([this._upgrade.boot(), this._closeSignal]).catch((err) => {
816
+ if (!this.closing) {
817
+ this._log?.error(['p2p', 'mesh'], 'opening shared meshes to upgrade failed', err);
818
+ }
819
+ });
765
820
  }
766
821
  catch (err) {
767
822
  if (this.closing) {
@@ -1131,6 +1186,8 @@ export class Core extends ReadyResource {
1131
1186
  this._offlineBoot.destroy(new CoreError('CORE_CLOSING'));
1132
1187
  this.onlineBoot.destroy(new CoreError('CORE_CLOSING'));
1133
1188
  this._signalClose();
1189
+ this._processor.close();
1190
+ await this._upgradeBoot;
1134
1191
  this._notifyStatus();
1135
1192
  await this._stopFileGc();
1136
1193
  this._stopTopicRetry();
@@ -1142,6 +1199,7 @@ export class Core extends ReadyResource {
1142
1199
  }
1143
1200
  this._clients.clear();
1144
1201
  await this._stopIndexing();
1202
+ await this._closeOpeningMeshes();
1145
1203
  await this._blobServer?.close();
1146
1204
  this._blobServer = null;
1147
1205
  await this._chatMoves?.close();
@@ -1150,6 +1208,7 @@ export class Core extends ReadyResource {
1150
1208
  this._unwireMesh(entry);
1151
1209
  await entry.mesh.close();
1152
1210
  }
1211
+ await this._processor.settled();
1153
1212
  await this._discardCandidateMesh();
1154
1213
  await this._departures?.close();
1155
1214
  this._departures = null;
@@ -1170,6 +1229,18 @@ export class Core extends ReadyResource {
1170
1229
  await this._closeStore();
1171
1230
  this._log?.info('lifecycle', 'core closed');
1172
1231
  }
1232
+ _refuseClosing() {
1233
+ if (this.closing) {
1234
+ throw new CoreError('CORE_CLOSING', ['Core']);
1235
+ }
1236
+ }
1237
+ async _closeOpeningMeshes() {
1238
+ const meshes = [...this._openingMeshSessions];
1239
+ for (const mesh of meshes) {
1240
+ mesh.interruptBoot();
1241
+ }
1242
+ await Promise.all(meshes.map((mesh) => mesh.close()));
1243
+ }
1173
1244
  async _closeStore() {
1174
1245
  await this.store?.close();
1175
1246
  if (this._ownsStore) {
@@ -1237,17 +1308,25 @@ export class Core extends ReadyResource {
1237
1308
  throw halted;
1238
1309
  }
1239
1310
  await this._adoptAccount(mesh);
1311
+ const late = mesh.interrupted;
1312
+ if (late) {
1313
+ this._withdrawFromMesh(mesh, late);
1314
+ throw late;
1315
+ }
1240
1316
  return mesh;
1241
1317
  }
1242
1318
  async _openMesh(opts = {}) {
1243
1319
  const session = await this.local.resolveSession(this._requireDeviceKeyPair().publicKey, opts);
1244
1320
  return this._openMeshSession(session, opts);
1245
1321
  }
1246
- async _openMeshSession(session, { blindPeerKeys, account, initial, sharing, workspace, project } = {}) {
1322
+ async _openMeshSession(session, { blindPeerKeys, account, initial, sharing, workspace, project, boot } = {}) {
1323
+ this._refuseClosing();
1247
1324
  const deviceId = this._requireDeviceKeyPair().publicKey;
1248
1325
  const mesh = this._newMesh(session, session.role ?? (account ? 'account' : 'project'), blindPeerKeys);
1326
+ this._openingMeshSessions.add(mesh);
1249
1327
  try {
1250
1328
  await mesh.ready();
1329
+ this._refuseClosing();
1251
1330
  if (session.baseKey && mesh.localLength === 0) {
1252
1331
  this._requireLocalDevice();
1253
1332
  await this.local.recordMesh(mesh.key, session, { deviceId, account });
@@ -1265,16 +1344,21 @@ export class Core extends ReadyResource {
1265
1344
  initial,
1266
1345
  sharing,
1267
1346
  workspace,
1268
- project
1347
+ project,
1348
+ boot
1269
1349
  });
1270
1350
  }
1351
+ this._refuseClosing();
1271
1352
  }
1272
1353
  catch (err) {
1273
1354
  if (mesh.opened && mesh.interrupted) {
1274
1355
  this._markHalted(mesh);
1275
1356
  }
1276
1357
  await mesh.close().catch(noop);
1277
- throw err;
1358
+ throw this.closing ? new CoreError('CORE_CLOSING', ['Core']) : err;
1359
+ }
1360
+ finally {
1361
+ this._openingMeshSessions.delete(mesh);
1278
1362
  }
1279
1363
  this._poolEntry(mesh);
1280
1364
  return mesh;
@@ -1287,17 +1371,25 @@ export class Core extends ReadyResource {
1287
1371
  deviceId: this._requireDeviceKeyPair().publicKey,
1288
1372
  writerSeed: session.writerSeed,
1289
1373
  logger: this._log,
1374
+ onerror: this._onerror,
1290
1375
  blindPeerKeys: this._resolveBlindPeerKeys(blindPeerKeys),
1291
- trustedDevice: (deviceId) => this._isOurMemberDevice(deviceId),
1292
1376
  wakeup: this._wakeup,
1293
1377
  attachmentUrl: (attachment) => this._linkAttachment(attachment, mesh),
1294
1378
  fileIndex: this._fileIndexFor(() => mesh),
1379
+ history: this._upgrade.history(() => mesh),
1295
1380
  pendingUpdates: this._pendingUpdatesFor(() => mesh),
1296
- bootFrom: session.bootFrom
1381
+ sidebarChats: (limit) => this.local.listChatOrder(mesh.key, { limit }),
1382
+ bootFrom: session.bootFrom,
1383
+ joinHeads: session.joinHeads
1297
1384
  });
1298
1385
  return mesh;
1299
1386
  }
1300
- async _appendOwnBirth(mesh, session, { deviceId, account, initial, sharing, workspace, project }) {
1387
+ async _appendOwnBirth(mesh, session, { deviceId, account, initial, sharing, workspace, project, boot }) {
1388
+ if (boot && mesh.localLength > 0) {
1389
+ await this.local.recordMesh(mesh.key, session, { deviceId, account, initial });
1390
+ void this._appendJoinLater(mesh, () => Promise.resolve(undefined), sharing);
1391
+ return;
1392
+ }
1301
1393
  await raceResource(mesh.autobee, (async () => {
1302
1394
  const birth = sharing
1303
1395
  ? await this._accountBirthMember(mesh, session)
@@ -1334,6 +1426,17 @@ export class Core extends ReadyResource {
1334
1426
  this._markHalted(mesh);
1335
1427
  }
1336
1428
  this._log?.warn(['p2p', 'mesh'], 'appending the join failed', err);
1429
+ if (mesh.interrupted) {
1430
+ return;
1431
+ }
1432
+ void firstEvent([
1433
+ [mesh, 'meshes-dirty'],
1434
+ [mesh, 'close']
1435
+ ]).then(() => {
1436
+ if (!this.closing && !mesh.closing) {
1437
+ void this._appendJoinLater(mesh, member, sharing);
1438
+ }
1439
+ });
1337
1440
  });
1338
1441
  }
1339
1442
  _joinProjectWhenListed(mesh, deviceId) {
@@ -1433,9 +1536,7 @@ export class Core extends ReadyResource {
1433
1536
  throw new CoreError('RECOVERY_PHRASE_NOT_OURS');
1434
1537
  }
1435
1538
  if (account) {
1436
- const member = await account.view
1437
- .get(MEMBERS, { id: identity.identityPublicKey })
1438
- .catch(noop);
1539
+ const member = await readView(account, (snap) => snap.get(MEMBERS, { id: identity.identityPublicKey }));
1439
1540
  if (!member) {
1440
1541
  throw new CoreError('RECOVERY_PHRASE_NOT_A_MEMBER');
1441
1542
  }
@@ -1830,14 +1931,7 @@ export class Core extends ReadyResource {
1830
1931
  if (!this._isPooled(mesh)) {
1831
1932
  return;
1832
1933
  }
1833
- const snap = mesh.view.snapshot();
1834
- let existing;
1835
- try {
1836
- existing = await snap.get(DEVICES, { id: deviceId });
1837
- }
1838
- finally {
1839
- await snap.close();
1840
- }
1934
+ const existing = await mesh.view.get(DEVICES, { id: deviceId });
1841
1935
  if (existing?.platform === local.platform &&
1842
1936
  existing.capabilityProfile === local.capabilityProfile &&
1843
1937
  existing.recommendedModelName === local.recommendedModelName) {
@@ -1864,14 +1958,7 @@ export class Core extends ReadyResource {
1864
1958
  if (!this._isPooled(mesh) || !this._isAccount(mesh)) {
1865
1959
  return;
1866
1960
  }
1867
- const snap = mesh.view.snapshot();
1868
- let existing;
1869
- try {
1870
- existing = await snap.get(DEVICES, { id: deviceId });
1871
- }
1872
- finally {
1873
- await snap.close();
1874
- }
1961
+ const existing = await mesh.view.get(DEVICES, { id: deviceId });
1875
1962
  if (existing?.appVersion === app.version &&
1876
1963
  (existing.appBuild || undefined) === app.build &&
1877
1964
  existing.appChannel === app.channel) {
@@ -1986,36 +2073,51 @@ export class Core extends ReadyResource {
1986
2073
  async _knownMember(exclude) {
1987
2074
  const account = this._accountMesh();
1988
2075
  if (account && account !== exclude && account.ourMemberId) {
1989
- const known = await this._memberIn(account).catch(noop);
2076
+ const known = await this._memberIn(account);
1990
2077
  if (known) {
1991
2078
  return known;
1992
2079
  }
1993
2080
  }
2081
+ let unread = null;
1994
2082
  for (const { mesh } of this._meshes.values()) {
1995
2083
  if (mesh === exclude ||
1996
2084
  !mesh.ourMemberId ||
1997
2085
  this._isHidden(this._meshIdOf(mesh), mesh.workspaceKey)) {
1998
2086
  continue;
1999
2087
  }
2000
- const known = await this._memberIn(mesh).catch(noop);
2088
+ const known = await this._memberIn(mesh).catch((err) => {
2089
+ unread = err;
2090
+ return null;
2091
+ });
2001
2092
  if (known) {
2002
2093
  return known;
2003
2094
  }
2004
2095
  }
2005
- return this._recordedMember();
2096
+ const recorded = await this._recordedMember();
2097
+ if (!recorded && unread) {
2098
+ throw unread;
2099
+ }
2100
+ return recorded;
2006
2101
  }
2007
- async _memberIn(mesh) {
2008
- const own = await mesh.view.get(DEVICES, {
2009
- id: this._requireDeviceKeyPair().publicKey
2102
+ _memberIn(mesh) {
2103
+ return readView(mesh, async (snap) => {
2104
+ const own = await snap.get(DEVICES, {
2105
+ id: this._requireDeviceKeyPair().publicKey
2106
+ });
2107
+ if (!own?.memberId || !own.memberProof) {
2108
+ return null;
2109
+ }
2110
+ const member = await snap.get(MEMBERS, { id: own.memberId });
2111
+ if (!member) {
2112
+ return null;
2113
+ }
2114
+ return {
2115
+ id: member.id,
2116
+ name: member.name,
2117
+ updatedAt: member.updatedAt,
2118
+ proof: own.memberProof
2119
+ };
2010
2120
  });
2011
- if (!own?.memberId || !own.memberProof) {
2012
- return null;
2013
- }
2014
- const member = await mesh.view.get(MEMBERS, { id: own.memberId });
2015
- if (!member) {
2016
- return null;
2017
- }
2018
- return { id: member.id, name: member.name, updatedAt: member.updatedAt, proof: own.memberProof };
2019
2121
  }
2020
2122
  async _recordedMember() {
2021
2123
  const account = await this._storedAccountKey();
@@ -2045,12 +2147,22 @@ export class Core extends ReadyResource {
2045
2147
  }
2046
2148
  };
2047
2149
  }
2048
- async _recordMemberProof(mesh, membership) {
2150
+ async _recordMemberProof(mesh) {
2151
+ const membership = mesh.ourMembership;
2152
+ if (!membership || !this._isPooled(mesh)) {
2153
+ return;
2154
+ }
2049
2155
  try {
2156
+ const kept = await this.local.readMemberProof(mesh.key);
2157
+ if (kept && kept.attestedAt >= membership.attestedAt) {
2158
+ return;
2159
+ }
2050
2160
  await this.local.recordMemberProof({ meshKey: mesh.key, ...membership });
2051
2161
  }
2052
2162
  catch (err) {
2053
- this._log?.warn(['p2p', 'mesh'], 'keeping our membership proof failed', err);
2163
+ if (!this.closing) {
2164
+ this._log?.warn(['p2p', 'mesh'], 'keeping our membership proof failed', err);
2165
+ }
2054
2166
  }
2055
2167
  }
2056
2168
  async _ensureNotRevoked(mesh) {
@@ -2139,10 +2251,14 @@ export class Core extends ReadyResource {
2139
2251
  this._haltedMeshes.delete(b4a.toHex(key));
2140
2252
  this._notifyStatus();
2141
2253
  }
2254
+ await fs.promises.rm(this._textDir(key), { recursive: true, force: true });
2142
2255
  if (cores) {
2143
2256
  await this._finishPurge(key);
2144
2257
  }
2145
2258
  }
2259
+ _textDir(key) {
2260
+ return path.join(path.dirname(this._storagePath), 'knowledge-text', b4a.toHex(key));
2261
+ }
2146
2262
  async _stopMeshServices(entry) {
2147
2263
  if (!entry)
2148
2264
  return;
@@ -2155,7 +2271,7 @@ export class Core extends ReadyResource {
2155
2271
  return;
2156
2272
  }
2157
2273
  entry.processingFiles = null;
2158
- if (!(await files.stopProcessing())) {
2274
+ if (!(await files.stopProcessing()) || entry.vectorIndex instanceof PluginVectorIndex) {
2159
2275
  entry.vectorIndex = null;
2160
2276
  }
2161
2277
  }
@@ -2357,31 +2473,35 @@ export class Core extends ReadyResource {
2357
2473
  this._retiredAccount !== this._meshIdOf(account)) {
2358
2474
  return;
2359
2475
  }
2476
+ const minted = this._ownWorkspaceId;
2360
2477
  this._retiredAccount = null;
2361
2478
  this._retiredMeshes.clear();
2362
2479
  await this.local.restoreMesh(account.key);
2480
+ if (fresh && fresh !== account) {
2481
+ await this.local.archiveMesh(fresh.key, { homeTo: account.key });
2482
+ }
2363
2483
  await this._restorePublicWriters(account);
2364
2484
  await this._adoptAccount(account);
2365
2485
  if (fresh && fresh !== account) {
2366
- await this._dropOwnMint(fresh);
2367
- await this._teardownMesh(fresh, { purge: true });
2368
- }
2369
- }
2370
- async _dropOwnMint(fresh) {
2371
- const minted = new Set((await this.local.listMeshes()).filter((row) => row.initial).map((row) => b4a.toHex(row.key)));
2372
- for (const joined of await fresh.view.find(JOINED_MESHES).toArray()) {
2373
- const id = b4a.toHex(joined.meshKey);
2374
- if (!minted.has(id)) {
2375
- continue;
2376
- }
2377
- const own = this._publicMesh(id);
2378
- if (own) {
2379
- await this._teardownMesh(own, { purge: true });
2486
+ const own = minted ? this._publicMesh(minted) : null;
2487
+ const kept = [
2488
+ fresh.key,
2489
+ ...(minted ? [b4a.from(minted, 'hex')] : []),
2490
+ ...(own ? [own.key] : [])
2491
+ ];
2492
+ await this._keepVoided(fresh.key, kept);
2493
+ }
2494
+ }
2495
+ async _keepVoided(fresh, keys) {
2496
+ await this.local.markRetired(keys, fresh);
2497
+ for (const key of keys) {
2498
+ const pooled = this._pooled(b4a.toHex(key));
2499
+ if (pooled) {
2500
+ await this._retirePooledMesh(pooled);
2380
2501
  }
2381
- await this._teardownMesh(this._pooled(id) ?? (await this._openPooledMesh(id, 'leave')), {
2382
- purge: true
2383
- });
2502
+ this._retiredMeshes.add(b4a.toHex(key));
2384
2503
  }
2504
+ this._notifyStatus();
2385
2505
  }
2386
2506
  async _replaceRevokedAccount(mesh) {
2387
2507
  this._minting = true;
@@ -2445,9 +2565,7 @@ export class Core extends ReadyResource {
2445
2565
  }
2446
2566
  this._kickedProjects.set(b4a.toHex(row.key), null);
2447
2567
  const project = await this._heldMesh(row.key, 'leave');
2448
- await (project
2449
- ? this._departRevokedMesh(project, { purge: true })
2450
- : this._teardownMesh(row.key));
2568
+ await (project ? this._departRevokedMesh(project) : this._teardownMesh(row.key));
2451
2569
  }
2452
2570
  }
2453
2571
  async _departRevokedMesh(mesh, opts = {}) {
@@ -2545,7 +2663,7 @@ export class Core extends ReadyResource {
2545
2663
  let pooled = this._pooled(meshId);
2546
2664
  if (this._isHidden(meshId, pooled?.workspaceKey)) {
2547
2665
  if (pooled) {
2548
- await this._teardownMesh(pooled, { depart: true, purge: true });
2666
+ await this._teardownMesh(pooled, { depart: true });
2549
2667
  }
2550
2668
  else {
2551
2669
  await this._dropKickedCopy(meshId);
@@ -2617,7 +2735,7 @@ export class Core extends ReadyResource {
2617
2735
  return false;
2618
2736
  }
2619
2737
  const account = await this._openPooledMesh(b4a.toHex(invite.baseKey));
2620
- if (!(await this._keepsWriter(account))) {
2738
+ if (!(await this._keepsJoin(account, invite))) {
2621
2739
  return false;
2622
2740
  }
2623
2741
  if (!this._isAccount(account)) {
@@ -2669,18 +2787,25 @@ export class Core extends ReadyResource {
2669
2787
  async _reopenRecordedMesh(invite) {
2670
2788
  if (!(await this.local.readMeshSession(invite.baseKey)))
2671
2789
  return false;
2672
- return this._keepsWriter(await this._openPooledMesh(b4a.toHex(invite.baseKey)));
2790
+ return this._keepsJoin(await this._openPooledMesh(b4a.toHex(invite.baseKey)), invite);
2673
2791
  }
2674
- async _keepsWriter(mesh) {
2675
- if (mesh.writable)
2676
- return true;
2677
- if (await mesh.ownWriterRefused()) {
2678
- return false;
2792
+ async _keepsJoin(mesh, { inviterHeads = [] }) {
2793
+ if (!mesh.writable) {
2794
+ if (await mesh.ownWriterRefused()) {
2795
+ return false;
2796
+ }
2797
+ const own = await mesh.view.get(DEVICES, {
2798
+ id: this._requireDeviceKeyPair().publicKey
2799
+ });
2800
+ if (own && isOwnRevokedRow(mesh, own)) {
2801
+ return false;
2802
+ }
2679
2803
  }
2680
- const own = await mesh.view.get(DEVICES, {
2681
- id: this._requireDeviceKeyPair().publicKey
2682
- });
2683
- return !(own && isOwnRevokedRow(mesh, own));
2804
+ if (mesh.localLength === 0) {
2805
+ await this.local.recordJoinHeads(mesh.key, inviterHeads);
2806
+ mesh.joinAfter(inviterHeads);
2807
+ }
2808
+ return true;
2684
2809
  }
2685
2810
  async _runJoinMesh(invite, claim) {
2686
2811
  const deviceId = this._requireDeviceKeyPair().publicKey;
@@ -2708,9 +2833,18 @@ export class Core extends ReadyResource {
2708
2833
  claim.reject(new CoreError('MESH_BELOW_FLOOR'));
2709
2834
  }
2710
2835
  }
2711
- _candidateSession({ seed, baseKey, workspace, fastForward }) {
2836
+ _candidateSession({ seed, baseKey, workspace, memberInvite, fastForward, inviterHeads }) {
2712
2837
  const role = workspace ? 'workspace' : undefined;
2713
- return { ...this.local.createCandidateSession(seed, baseKey, { role }), bootFrom: fastForward };
2838
+ this._log?.info(['p2p', 'mesh'], 'mesh role chosen', {
2839
+ mesh: b4a.toHex(baseKey).slice(0, 8),
2840
+ role: role ?? (memberInvite ? 'project' : 'account'),
2841
+ source: 'invite'
2842
+ });
2843
+ return {
2844
+ ...this.local.createCandidateSession(seed, baseKey, { role }),
2845
+ bootFrom: fastForward,
2846
+ joinHeads: inviterHeads
2847
+ };
2714
2848
  }
2715
2849
  async _joinCandidate(invite, source, deviceId) {
2716
2850
  const session = this._candidateSession(invite);
@@ -2726,14 +2860,15 @@ export class Core extends ReadyResource {
2726
2860
  if (this._joinCancellation) {
2727
2861
  this._joinCancellation.accepting = false;
2728
2862
  }
2863
+ let member;
2729
2864
  try {
2865
+ member = await this._candidateBirthMember(candidate, invite);
2730
2866
  await this.local.recordMesh(candidate.key, session, { deviceId });
2731
2867
  }
2732
2868
  catch (error) {
2733
2869
  await this._discardCandidateMesh();
2734
2870
  throw error;
2735
2871
  }
2736
- const member = await this._candidateBirthMember(candidate, invite);
2737
2872
  const appended = this._appendJoinLater(candidate, async () => member);
2738
2873
  return this._landJoinedMesh(candidate, {
2739
2874
  source,
@@ -2758,13 +2893,20 @@ export class Core extends ReadyResource {
2758
2893
  }
2759
2894
  if (memberInvite) {
2760
2895
  this._candidateMesh = null;
2761
- if (!candidate.writable) {
2762
- await this.local.markAwaitingAdmission(candidate.key, { awaitingAdmission: true });
2896
+ this._recordingJoins.add(candidate);
2897
+ try {
2898
+ if (!candidate.writable) {
2899
+ await this.local.markAwaitingAdmission(candidate.key, { awaitingAdmission: true });
2900
+ }
2901
+ this._wireMesh(this._poolEntry(candidate));
2902
+ this._bringMeshOnline(candidate);
2903
+ if (candidate.writable) {
2904
+ await this.local.markAwaitingAdmission(candidate.key, { awaitingAdmission: false });
2905
+ }
2763
2906
  }
2764
- this._wireMesh(this._poolEntry(candidate));
2765
- this._bringMeshOnline(candidate);
2766
- if (candidate.writable) {
2767
- await this.local.markAwaitingAdmission(candidate.key, { awaitingAdmission: false });
2907
+ catch (err) {
2908
+ this._recordingJoins.delete(candidate);
2909
+ throw err;
2768
2910
  }
2769
2911
  this._notifyStatus();
2770
2912
  }
@@ -2820,333 +2962,31 @@ export class Core extends ReadyResource {
2820
2962
  }
2821
2963
  async _recordJoinedMesh(mesh, appended) {
2822
2964
  try {
2965
+ await this._registerMesh(mesh.key, { shared: false });
2823
2966
  await appended;
2824
2967
  const kind = await meshKind(mesh.view);
2825
2968
  const workspace = kind === 'workspace';
2826
2969
  if (kind && workspace !== (mesh.role === 'workspace')) {
2827
- await this._reopenInRole(mesh, workspace ? 'workspace' : 'project');
2970
+ await this._reopenInRole(mesh, workspace ? 'workspace' : 'project', `its kind is ${kind}`);
2971
+ }
2972
+ if (workspace) {
2973
+ await this._upgrade.markJoinedWorkspace(this._meshIdOf(mesh));
2828
2974
  }
2829
- await this._registerMesh(mesh.key, { shared: false, workspace });
2830
2975
  }
2831
2976
  catch (error) {
2832
2977
  this._log?.warn(['p2p', 'mesh'], 'recording the joined mesh failed', error);
2833
2978
  }
2834
- }
2835
- async _followUpgrade(mesh) {
2836
- const meshId = this._meshIdOf(mesh);
2837
- if (this.closing || this._upgrading.has(meshId)) {
2838
- return;
2839
- }
2840
- this._upgrading.add(meshId);
2841
- try {
2842
- let kind = await meshKind(mesh.view);
2843
- if (kind === 'shared' && (await this._upgradable(mesh))) {
2844
- await mesh.dispatch('upgrade-to-workspace', { upgradedAt: Date.now() });
2845
- kind = await meshKind(mesh.view);
2846
- }
2847
- if (kind === 'workspace' && mesh.writable && this._isPooled(mesh)) {
2848
- await this.local.set(upgradeCheckedKey(mesh.key), GROUPS_PENDING);
2849
- await this._markFoldDue(mesh.key);
2850
- await this._reopenInRole(mesh, 'workspace');
2851
- await this._markJoinedWorkspace(mesh.key);
2852
- }
2853
- else if (kind !== 'project' && (kind !== 'shared' || (await bornUnderRules(mesh.view)))) {
2854
- await this._markUpgradeChecked(mesh.key);
2855
- }
2856
- }
2857
- catch (err) {
2858
- if (!this.closing) {
2859
- this._log?.warn(['p2p', 'mesh'], 'upgrading a shared mesh to a workspace failed', err);
2860
- }
2861
- }
2862
2979
  finally {
2863
- this._upgrading.delete(meshId);
2864
- }
2865
- }
2866
- _followGroups(workspace) {
2867
- if (this.closing || !workspace.writable) {
2868
- return;
2869
- }
2870
- if (this._groupPasses.has(workspace)) {
2871
- this._groupPassesDue.add(workspace);
2872
- return;
2873
- }
2874
- this._groupPasses.add(workspace);
2875
- void this._passGroups(workspace)
2876
- .catch((err) => {
2877
- if (!this.closing && this._isPooled(workspace)) {
2878
- this._log?.warn(['p2p', 'mesh'], 'moving chat-groups into projects failed', err);
2879
- }
2880
- })
2881
- .finally(() => {
2882
- this._groupPasses.delete(workspace);
2883
- if (this._groupPassesDue.delete(workspace)) {
2884
- this._followGroups(workspace);
2885
- }
2886
- });
2887
- }
2888
- async _passGroups(workspace) {
2889
- const checked = await this.local.get(upgradeCheckedKey(workspace.key));
2890
- if (checked === true) {
2891
- return this._passAgents(workspace);
2892
- }
2893
- if (checked !== GROUPS_PENDING) {
2894
- return;
2895
- }
2896
- const ours = workspace.ourMemberId ?? (await workspace.refreshOurMember())?.memberId;
2897
- if (!ours || !(await isWorkspace(workspace.view))) {
2898
- return;
2899
- }
2900
- const buckets = await this._groupBuckets(workspace);
2901
- const rows = await projectsByGroup(workspace.view);
2902
- const passes = await Promise.allSettled(buckets.map((bucket) => this._passGroup(workspace, ours, { bucket, row: rows.get(bucket.groupId) ?? null })));
2903
- const settled = [];
2904
- for (const pass of passes) {
2905
- if (pass.status === 'rejected') {
2906
- throw pass.reason;
2907
- }
2908
- settled.push(pass.value);
2909
- }
2910
- if (settled.every((state) => state === 'done') && this._isPooled(workspace)) {
2911
- await this.local.set(upgradeCheckedKey(workspace.key), true);
2912
- }
2913
- if (!settled.includes('pending')) {
2914
- await this._passAgents(workspace);
2915
- }
2916
- }
2917
- async _groupBuckets(workspace) {
2918
- const named = workspace.name?.name?.trim() || 'Workspace';
2919
- const groups = await liveGroups(workspace.view);
2920
- const ids = [...groups.map((group) => group.id), UNGROUPED];
2921
- const records = await Promise.all(ids.map((id) => this._groupRecord(workspace, id)));
2922
- if (!records.every((record) => record.copied || record.done)) {
2923
- return this._scanBuckets(workspace, named);
2924
- }
2925
- return ids.map((groupId, idx) => ({
2926
- groupId,
2927
- name: groups[idx]?.name ?? named,
2928
- chats: new Set()
2929
- }));
2930
- }
2931
- async _markFoldDue(workspaceKey) {
2932
- const key = agentsFoldKey(workspaceKey);
2933
- if ((await this.local.get(key)) !== true) {
2934
- await this.local.set(key, FOLD_PENDING);
2935
- }
2936
- }
2937
- async _passAgents(workspace) {
2938
- if ((await this.local.get(agentsFoldKey(workspace.key))) !== FOLD_PENDING) {
2939
- return;
2940
- }
2941
- const copies = await ownCopies(workspace, this._requireDeviceKeyPair().publicKey);
2942
- if (copies.length > 0) {
2943
- const account = await this._writableAccount();
2944
- const projects = await this._heldProjects(workspace);
2945
- if (!account || !projects) {
2946
- return;
2947
- }
2948
- await foldAgents({ account, workspace, projects, picks: this._agentPicks() }, copies);
2949
- }
2950
- if (this._isPooled(workspace)) {
2951
- await this.local.set(agentsFoldKey(workspace.key), true);
2952
- }
2953
- }
2954
- async _heldProjects(workspace) {
2955
- const ours = workspace.ourMemberId;
2956
- const held = [];
2957
- for (const row of await workspace.view.find(PROJECTS).toArray()) {
2958
- if (row.deletedAt || !ours || !listedProject(row, ours))
2959
- continue;
2960
- const project = await this._heldMesh(row.id);
2961
- if (!project?.writable) {
2962
- return null;
2963
- }
2964
- held.push(project);
2965
- }
2966
- return held;
2967
- }
2968
- async _groupRecord(workspace, groupId) {
2969
- const stored = await this.local.get(groupRecordKey(workspace.key, groupId));
2970
- return isGroupRecord(stored) ? stored : {};
2971
- }
2972
- async _scanBuckets(workspace, named) {
2973
- const buckets = await groupBuckets(workspace.view, named);
2974
- const ungrouped = buckets.some((bucket) => bucket.groupId === UNGROUPED);
2975
- if (!ungrouped && !(await this._groupRecord(workspace, UNGROUPED)).key) {
2976
- await this.local.set(groupRecordKey(workspace.key, UNGROUPED), { done: true });
2977
- }
2978
- return buckets;
2979
- }
2980
- async _passGroup(workspace, ours, { bucket, row: listed }) {
2981
- const recordKey = groupRecordKey(workspace.key, bucket.groupId);
2982
- let record = await this._groupRecord(workspace, bucket.groupId);
2983
- if (record.done) {
2984
- return 'done';
2985
- }
2986
- let row = listed;
2987
- if (!row) {
2988
- record = await this._foundGroupProject(workspace, bucket, { recordKey, record });
2989
- row = await projectOfGroup(workspace.view, bucket.groupId);
2990
- }
2991
- const live = !!row && !row.deletedAt;
2992
- if (row && live && record.key === b4a.toHex(row.id)) {
2993
- await this._listNewMembers(workspace, { row, recordKey, record });
2994
- return 'won';
2995
- }
2996
- if (row && live && !(await this._mergeGroupInto(workspace, ours, { row, bucket, record }))) {
2997
- return 'pending';
2998
- }
2999
- if (record.key) {
3000
- await this._purgeProject(b4a.from(record.key, 'hex'));
3001
- }
3002
- await this.local.set(recordKey, { ...record, done: true });
3003
- return 'done';
3004
- }
3005
- async _foundGroupProject(workspace, bucket, { recordKey, record }) {
3006
- const held = record.key ? await this._heldMesh(b4a.from(record.key, 'hex')) : null;
3007
- const name = projectName(bucket.name.trim() || 'Project');
3008
- const project = held ?? (await this._openNewProject(workspace, { name, upgraded: true }));
3009
- let next = { ...record, key: this._meshIdOf(project) };
3010
- await this.local.set(recordKey, next);
3011
- await this._addWorkspaceMembers(workspace, project);
3012
- if (!next.copied) {
3013
- await this._copyGroup(workspace, project, bucket);
3014
- next = { ...next, copied: true };
3015
- await this.local.set(recordKey, next);
3016
- }
3017
- const now = Date.now();
3018
- await this._listProject(workspace, project, {
3019
- createdAt: now,
3020
- updatedAt: now,
3021
- groupId: bucket.groupId
3022
- });
3023
- this._notifyStatus();
3024
- return next;
3025
- }
3026
- async _mergeGroupInto(workspace, ours, { row, bucket, record }) {
3027
- const winner = listsMember(row, ours) ? await this._winnerOf(workspace, row) : null;
3028
- if (!winner) {
3029
- return false;
3030
- }
3031
- await this._addWorkspaceMembers(workspace, winner);
3032
- await this._listWorkspaceMembers(workspace, row);
3033
- const own = record.key ? await this._heldMesh(b4a.from(record.key, 'hex')) : null;
3034
- if (own) {
3035
- await copyContent({
3036
- from: own,
3037
- into: winner,
3038
- deviceId: this._requireDeviceKeyPair().publicKey
3039
- });
3040
- }
3041
- await this._copyGroup(workspace, winner, bucket);
3042
- this._log?.info(['p2p', 'mesh'], 'merged a chat-group into the project that won it', {
3043
- project: b4a.toHex(row.id).slice(0, 8)
3044
- });
3045
- return true;
3046
- }
3047
- async _winnerOf(workspace, row) {
3048
- const projectId = b4a.toHex(row.id);
3049
- if (!this._pooled(projectId) && !(await this.local.readMeshSession(row.id))) {
3050
- await this._followProjects(workspace);
3051
- await this._following.get(projectId);
3052
- }
3053
- const winner = this._pooled(projectId) ?? (await this._heldMesh(row.id));
3054
- await winner?.waitForWritable(WINNER_ADMISSION_WAIT_MS);
3055
- return winner?.writable ? winner : null;
3056
- }
3057
- async _copyGroup(workspace, project, { chats }) {
3058
- const deviceId = this._requireDeviceKeyPair().publicKey;
3059
- await copyContent({ from: workspace, into: project, deviceId, chats, endsOpenRuns: true });
3060
- }
3061
- async _addWorkspaceMembers(workspace, project) {
3062
- for (const memberId of await this._workspaceMembers(workspace)) {
3063
- if (!(await inProject(project.view, memberId))) {
3064
- await project.dispatch('put-project-member', { memberId });
3065
- }
2980
+ this._recordingJoins.delete(mesh);
3066
2981
  }
3067
2982
  }
3068
- async _listNewMembers(workspace, { row, recordKey, record }) {
3069
- const listed = new Set(record.listedMembers ?? []);
3070
- const members = await this._workspaceMembers(workspace);
3071
- const added = members.filter((id) => !listed.has(b4a.toHex(id)));
3072
- const project = added.length > 0 ? await this._heldMesh(row.id) : null;
3073
- for (const memberId of added) {
3074
- if (project && !(await inProject(project.view, memberId))) {
3075
- await project.dispatch('put-project-member', { memberId });
3076
- }
3077
- if (!listsMember(row, memberId)) {
3078
- await workspace.dispatch('set-project-listing', { projectId: row.id, memberId });
3079
- }
3080
- }
3081
- if (added.length > 0 && this._isPooled(workspace)) {
3082
- const listedMembers = [...listed, ...added.map((id) => b4a.toHex(id))];
3083
- await this.local.set(recordKey, { ...record, listedMembers });
3084
- }
3085
- }
3086
- async _listWorkspaceMembers(workspace, row) {
3087
- for (const memberId of await this._workspaceMembers(workspace)) {
3088
- if (!listsMember(row, memberId)) {
3089
- await workspace.dispatch('set-project-listing', { projectId: row.id, memberId });
3090
- }
3091
- }
3092
- }
3093
- async _workspaceMembers(workspace) {
3094
- const live = await liveMemberIds(workspace.view);
3095
- return (await upgradeMembers(workspace.view)).filter((id) => live.has(b4a.toHex(id)));
3096
- }
3097
- async _openUpgradable() {
3098
- const account = await this._storedAccountKey();
3099
- for (const row of await this.local.listMeshes()) {
3100
- const skip = row.workspaceKey || (account && b4a.equals(row.key, account));
3101
- const role = recordedRole(row);
3102
- if (this.closing || skip || (role !== 'project' && role !== 'workspace')) {
3103
- continue;
3104
- }
3105
- const checked = await this.local.get(upgradeCheckedKey(row.key));
3106
- const due = role === 'workspace'
3107
- ? checked === GROUPS_PENDING ||
3108
- (await this.local.get(agentsFoldKey(row.key))) === FOLD_PENDING
3109
- : checked !== true;
3110
- if (!due) {
3111
- continue;
3112
- }
3113
- await this._openPooledMesh(b4a.toHex(row.key)).catch((err) => {
3114
- this._log?.warn(['p2p', 'mesh'], 'opening a shared mesh to upgrade it failed', err);
3115
- });
3116
- }
3117
- }
3118
- async _markUpgradeChecked(meshKey) {
3119
- const key = upgradeCheckedKey(meshKey);
3120
- if ((await this.local.get(key)) !== true) {
3121
- await this.local.set(key, true);
3122
- }
3123
- }
3124
- async _upgradable(mesh) {
3125
- if (!mesh.writable || (await rulesVersion(mesh.view)) < WORKSPACES_RULES) {
3126
- return false;
3127
- }
3128
- return !(await bornUnderRules(mesh.view));
3129
- }
3130
- async _markJoinedWorkspace(meshKey) {
3131
- const registry = await this._writableAccount();
3132
- const joined = await registry?.view.get(JOINED_MESHES, { meshKey });
3133
- if (!registry || !joined || joined.workspace || joined.leftAt) {
3134
- return;
3135
- }
3136
- await registry.dispatch('put-joined-mesh', {
3137
- room: {
3138
- meshKey,
3139
- memberId: joined.memberId,
3140
- joinedAt: joined.joinedAt,
3141
- label: joined.label,
3142
- labeledAt: joined.labeledAt,
3143
- shared: joined.shared,
3144
- sharedAt: joined.sharedAt,
3145
- workspace: true
3146
- }
2983
+ async _reopenInRole(mesh, role, reason) {
2984
+ this._log?.info(['p2p', 'mesh'], 'reopening a mesh in its role', {
2985
+ mesh: this._meshIdOf(mesh).slice(0, 8),
2986
+ from: mesh.role,
2987
+ to: role,
2988
+ reason
3147
2989
  });
3148
- }
3149
- async _reopenInRole(mesh, role) {
3150
2990
  await this.local.setMeshRole(mesh.key, role);
3151
2991
  await this._retirePooledMesh(mesh);
3152
2992
  const reopened = await this._openPooledMesh(this._meshIdOf(mesh));
@@ -3212,6 +3052,7 @@ export class Core extends ReadyResource {
3212
3052
  continue;
3213
3053
  }
3214
3054
  const pooled = this._pooled(b4a.toHex(row.key));
3055
+ const kind = pooled ? await meshKind(pooled.view) : null;
3215
3056
  batch.dispatch('put-joined-mesh', {
3216
3057
  room: {
3217
3058
  meshKey: row.key,
@@ -3219,7 +3060,7 @@ export class Core extends ReadyResource {
3219
3060
  joinedAt: now,
3220
3061
  shared: false,
3221
3062
  sharedAt: 0,
3222
- workspace: row.role === 'workspace' || (!!pooled && (await isWorkspace(pooled.view)))
3063
+ workspace: kind === null ? row.role === 'workspace' : kind === 'workspace'
3223
3064
  }
3224
3065
  });
3225
3066
  await this.local.markJoinedUnder(row.key, now);
@@ -3297,13 +3138,15 @@ export class Core extends ReadyResource {
3297
3138
  deviceId: this._requireDeviceKeyPair().publicKey,
3298
3139
  writerSeed: session.writerSeed,
3299
3140
  logger: this._log,
3141
+ onerror: this._onerror,
3300
3142
  blindPeerKeys: this._resolveBlindPeerKeys(invite.blindPeers),
3301
3143
  bootFrom: session.bootFrom,
3302
- trustedDevice: (deviceId) => this._isOurMemberDevice(deviceId),
3144
+ joinHeads: session.joinHeads,
3303
3145
  wakeup: this._wakeup,
3304
3146
  attachmentUrl: (attachment) => this._linkAttachment(attachment, candidate),
3305
3147
  fileIndex: this._fileIndexFor(() => candidate),
3306
- pendingUpdates: this._pendingUpdatesFor(() => candidate)
3148
+ pendingUpdates: this._pendingUpdatesFor(() => candidate),
3149
+ sidebarChats: (limit) => this.local.listChatOrder(candidate.key, { limit })
3307
3150
  });
3308
3151
  this._candidateMesh = candidate;
3309
3152
  if (this._swarm) {
@@ -3387,17 +3230,18 @@ export class Core extends ReadyResource {
3387
3230
  }
3388
3231
  _onSharedWritable(mesh) {
3389
3232
  if (mesh.role === 'project') {
3390
- void this._followUpgrade(mesh);
3391
- void this._followSharedAgents();
3233
+ this._processor.enqueue(mesh, [MESH_WRITER.upgrade, MESH_WRITER.sharedAgents]);
3392
3234
  void this._workspaceOf(mesh)
3393
- .then((workspace) => workspace && this._followGroups(workspace))
3235
+ .then((workspace) => workspace && this._upgrade.follow(workspace))
3394
3236
  .catch(noop);
3395
3237
  }
3396
3238
  else if (mesh.role === 'workspace') {
3397
- this._followGroups(mesh);
3398
- void this._followSharedAgents();
3399
- this._shareMemberKey(mesh);
3400
- void this._followHosting();
3239
+ this._upgrade.follow(mesh);
3240
+ this._processor.enqueue(mesh, [
3241
+ MESH_WRITER.memberKey,
3242
+ MESH_WRITER.sharedAgents,
3243
+ MESH_WRITER.hosting
3244
+ ]);
3401
3245
  }
3402
3246
  }
3403
3247
  _onMeshWritable(mesh) {
@@ -3411,15 +3255,17 @@ export class Core extends ReadyResource {
3411
3255
  void this._followPublicMesh(mesh);
3412
3256
  void this._followSkillSecrets();
3413
3257
  }
3414
- if (this._isOwnPublic(mesh)) {
3258
+ const account = this._accountMesh();
3259
+ if (this._isOwnPublic(mesh) && account) {
3415
3260
  void this._followAgentCopies();
3261
+ this._upgrade.copyAccount(account);
3416
3262
  }
3417
3263
  this._notifyStatus();
3418
3264
  }
3419
3265
  _onMeshDead(mesh, err) {
3420
3266
  if (this.closing || !this._isPooled(mesh))
3421
3267
  return;
3422
- this._log?.error(['p2p', 'mesh'], 'mesh autobee died - retiring it', err);
3268
+ this._log?.info(['p2p', 'mesh'], 'mesh autobee died - retiring it');
3423
3269
  if (this._isAccount(mesh)) {
3424
3270
  this._statusError = statusErrorMessage(MESH_DIED, err);
3425
3271
  }
@@ -3453,6 +3299,9 @@ export class Core extends ReadyResource {
3453
3299
  entry.handlers = handlers;
3454
3300
  mesh.on('kick-dirty', () => this._reviewKick(mesh).catch((err) => this._log?.warn(['p2p', 'mesh'], 'reviewing a kick failed', err)));
3455
3301
  mesh.on('unattested-device', () => void this._settleMembership(mesh));
3302
+ this._chatMoves
3303
+ ?.followSource(mesh)
3304
+ .catch((err) => this._log?.warn('chat-move', 'following a moved chat source failed', { err }));
3456
3305
  if (mesh.writable) {
3457
3306
  this._onMeshSeedDue(mesh);
3458
3307
  this._onProjectWritable(mesh);
@@ -3474,20 +3323,21 @@ export class Core extends ReadyResource {
3474
3323
  this._dropUnpairedSiblings(mesh).catch(warn('unpairing a kicked device'));
3475
3324
  });
3476
3325
  mesh.on('agents-dirty', () => {
3326
+ const due = [];
3477
3327
  if (mesh.role === 'workspace' || mesh.role === 'project' || this._isOwnPublic(mesh)) {
3478
- void this._followSharedAgents();
3328
+ due.push(MESH_WRITER.sharedAgents);
3479
3329
  }
3480
3330
  if (mesh.role === 'workspace') {
3481
- void this._sealProjectKeys();
3482
- void this._followHosting();
3331
+ due.push(MESH_WRITER.projectKeys, MESH_WRITER.hosting);
3483
3332
  }
3484
3333
  if (this._isAccount(mesh)) {
3485
3334
  void this._followAgentCopies();
3486
- void this._followSharedAgents();
3335
+ due.push(MESH_WRITER.sharedAgents);
3487
3336
  for (const listener of [...this._agentListeners]) {
3488
3337
  listener();
3489
3338
  }
3490
3339
  }
3340
+ this._processor.enqueue(mesh, due);
3491
3341
  });
3492
3342
  mesh.on('members-dirty', () => {
3493
3343
  if (!this._isAccount(mesh))
@@ -3495,6 +3345,8 @@ export class Core extends ReadyResource {
3495
3345
  void this._followAccountName(mesh);
3496
3346
  });
3497
3347
  mesh.on('meshes-dirty', (changed, groupsChanged = false, chatsChanged = true) => this._onMeshesDirty(entry, { changed, groupsChanged, chatsChanged }));
3348
+ mesh.on('files-pending', () => void this._upgrade.refill(mesh).catch(noop));
3349
+ mesh.on('landed', () => void this._upgrade.refill(mesh).catch(noop));
3498
3350
  mesh.on('joined', () => {
3499
3351
  if (entry.meshesPending) {
3500
3352
  void this._followJoinedMeshes(mesh);
@@ -3508,16 +3360,14 @@ export class Core extends ReadyResource {
3508
3360
  void this._keepName(mesh);
3509
3361
  this._notifyStatus();
3510
3362
  });
3511
- mesh.on('membership', (membership) => {
3363
+ mesh.on('membership', () => {
3512
3364
  if (this._isHidden(this._meshIdOf(mesh), mesh.workspaceKey))
3513
3365
  return;
3514
- void this._recordMemberProof(mesh, membership);
3515
- void this._followJoinedMeshes(mesh);
3366
+ const due = [MESH_WRITER.memberProof, MESH_WRITER.joinedMeshes];
3516
3367
  if (mesh.role === 'workspace') {
3517
- void this._followProjects(mesh);
3518
- this._shareMemberKey(mesh);
3519
- void this._followHosting();
3368
+ due.push(MESH_WRITER.memberKey, MESH_WRITER.hosting, MESH_WRITER.projects);
3520
3369
  }
3370
+ this._processor.enqueue(mesh, due);
3521
3371
  void this._followPublicMesh(mesh);
3522
3372
  });
3523
3373
  this._listenMesh(mesh, handlers);
@@ -3531,26 +3381,91 @@ export class Core extends ReadyResource {
3531
3381
  if (this._isHidden(this._meshIdOf(mesh), mesh.workspaceKey)) {
3532
3382
  return;
3533
3383
  }
3384
+ const due = [];
3534
3385
  if (changed || entry.meshesPending) {
3535
- void this._followJoinedMeshes(mesh);
3386
+ due.push(MESH_WRITER.joinedMeshes);
3536
3387
  }
3537
3388
  if (changed && mesh.role === 'workspace') {
3538
- void this._followProjects(mesh);
3539
- void this._followHosting();
3389
+ due.push(MESH_WRITER.hosting, MESH_WRITER.projects);
3540
3390
  }
3541
3391
  if (mesh.role === 'project') {
3542
- void this._followUpgrade(mesh);
3392
+ due.push(MESH_WRITER.upgrade);
3543
3393
  }
3544
- if ((changed || groupsChanged) && mesh.role === 'workspace') {
3545
- this._followGroups(mesh);
3394
+ if (mesh.role === 'workspace' || mesh.role === 'project') {
3395
+ due.push(MESH_WRITER.role);
3396
+ }
3397
+ this._processor.enqueue(mesh, due);
3398
+ if ((changed || groupsChanged || chatsChanged) && mesh.role === 'workspace') {
3399
+ this._upgrade.follow(mesh);
3546
3400
  }
3547
3401
  const opening = this._ownWorkspaceId !== null && !this._publicMesh();
3548
3402
  if (this._isAccount(mesh) && (changed || entry.publicPending || opening)) {
3549
3403
  void this._followPublicMesh(mesh);
3550
3404
  }
3551
- const copying = chatsChanged || this._accountCopied === null;
3552
- if (copying && this._isAccount(mesh) && this._ownWorkspaceId && this._publicMesh()) {
3553
- this._migrateIntoPublic(mesh, b4a.from(this._ownWorkspaceId, 'hex'));
3405
+ if ((chatsChanged || this._upgrade.accountUnread) && this._isAccount(mesh)) {
3406
+ this._upgrade.copyAccount(mesh);
3407
+ }
3408
+ }
3409
+ async _reconcile(mesh, due) {
3410
+ const writers = {
3411
+ [MESH_WRITER.role]: () => this._settleRole(mesh),
3412
+ [MESH_WRITER.memberProof]: () => this._recordMemberProof(mesh),
3413
+ [MESH_WRITER.memberKey]: async () => {
3414
+ if (await this._dueMemberKey(mesh)) {
3415
+ this._processor.settle(mesh, () => this._writeMemberKey(mesh));
3416
+ }
3417
+ },
3418
+ [MESH_WRITER.upgrade]: async () => {
3419
+ if (await this._upgrade.sharedDue(mesh)) {
3420
+ this._processor.settle(mesh, () => this._upgrade.followShared(mesh));
3421
+ }
3422
+ },
3423
+ [MESH_WRITER.sharedAgents]: () => this._followSharedAgents(),
3424
+ [MESH_WRITER.projectKeys]: () => this._sealProjectKeys(),
3425
+ [MESH_WRITER.hosting]: () => this._followHosting(),
3426
+ [MESH_WRITER.joinedMeshes]: () => this._followJoinedMeshes(mesh),
3427
+ [MESH_WRITER.projects]: () => this._followProjects(mesh)
3428
+ };
3429
+ for (const writer of PASS_ORDER) {
3430
+ if (!due.has(writer))
3431
+ continue;
3432
+ const run = writers[writer]().catch(this._passFailed);
3433
+ if (!STARTED.has(writer)) {
3434
+ await run;
3435
+ }
3436
+ }
3437
+ }
3438
+ async _settleRole(mesh) {
3439
+ const meshId = this._meshIdOf(mesh);
3440
+ if (!this._isPooled(mesh) || this._recordingJoins.has(mesh) || this._roleSettles.has(meshId)) {
3441
+ return;
3442
+ }
3443
+ this._roleSettles.add(meshId);
3444
+ try {
3445
+ const kind = await meshKind(mesh.view);
3446
+ const role = kind === 'workspace' ? 'workspace' : 'project';
3447
+ if (!isSharedKind(kind) || role === mesh.role) {
3448
+ return;
3449
+ }
3450
+ if (kind === 'workspace' &&
3451
+ (!(await this.local.awaitsAdmission(mesh.key)) || mesh.writable)) {
3452
+ return;
3453
+ }
3454
+ if (!this._isPooled(mesh)) {
3455
+ return;
3456
+ }
3457
+ await this._reopenInRole(mesh, role, `its kind is ${kind}`);
3458
+ if (role === 'workspace') {
3459
+ await this._upgrade.markJoinedWorkspace(meshId);
3460
+ }
3461
+ }
3462
+ catch (err) {
3463
+ if (!this.closing) {
3464
+ this._log?.warn(['p2p', 'mesh'], 'settling a mesh role failed', err);
3465
+ }
3466
+ }
3467
+ finally {
3468
+ this._roleSettles.delete(meshId);
3554
3469
  }
3555
3470
  }
3556
3471
  _listenMesh(mesh, handlers) {
@@ -3658,31 +3573,24 @@ export class Core extends ReadyResource {
3658
3573
  batch.dispatch('put-project-host-key', due);
3659
3574
  }
3660
3575
  }
3661
- _shareMemberKey(workspace) {
3662
- const write = (this._memberKeyWrites.get(workspace) ?? Promise.resolve())
3663
- .then(() => this._writeMemberKey(workspace))
3664
- .catch((err) => {
3665
- if (!this.closing) {
3666
- this._log?.warn(['p2p', 'mesh'], 'sharing our member key failed', err);
3667
- }
3668
- });
3669
- this._memberKeyWrites.set(workspace, write);
3670
- }
3671
3576
  async _writeMemberKey(workspace) {
3577
+ const due = await this._dueMemberKey(workspace);
3578
+ if (due) {
3579
+ await workspace.dispatch('put-member-key', due);
3580
+ }
3581
+ }
3582
+ async _dueMemberKey(workspace) {
3672
3583
  const account = this._accountMesh();
3673
3584
  const memberId = workspace.ourMemberId;
3674
3585
  if (workspace.role !== 'workspace' || workspace.closing || !workspace.writable) {
3675
- return;
3586
+ return null;
3676
3587
  }
3677
- if (!memberId || !account) {
3678
- return;
3588
+ if (!memberId || !account || !(await isWorkspace(workspace.view))) {
3589
+ return null;
3679
3590
  }
3680
3591
  const boxKey = memberBoxKeyPair(account.seed).publicKey;
3681
3592
  const held = await readMemberKey(workspace.view, memberId);
3682
- if (held && b4a.equals(held.boxKey, boxKey)) {
3683
- return;
3684
- }
3685
- await workspace.dispatch('put-member-key', { memberId, boxKey });
3593
+ return held && b4a.equals(held.boxKey, boxKey) ? null : { memberId, boxKey };
3686
3594
  }
3687
3595
  _devicesOf(mesh) {
3688
3596
  return this._isOwnPublic(mesh) ? (this._accountMesh() ?? mesh) : mesh;
@@ -3690,14 +3598,33 @@ export class Core extends ReadyResource {
3690
3598
  _hasPublic(mesh) {
3691
3599
  return mesh.role === 'workspace' && this._isPooled(mesh);
3692
3600
  }
3693
- async _publicOf(scope, { follow = false } = {}) {
3601
+ async _publicOf(scope, { follow = false, route } = {}) {
3694
3602
  const workspaceId = this._meshIdOf(scope);
3695
3603
  const held = this._publicMesh(workspaceId);
3696
3604
  if (held) {
3697
3605
  return held;
3698
3606
  }
3607
+ const started = Date.now();
3608
+ const routed = await this._awaitPublic(scope, { follow });
3609
+ this._log?.info(['p2p', 'mesh'], 'route waited on a public mesh', {
3610
+ workspace: workspaceId.slice(0, 8),
3611
+ route: route ?? null,
3612
+ opened: routed !== scope,
3613
+ ms: Date.now() - started
3614
+ });
3615
+ return routed;
3616
+ }
3617
+ async _awaitPublic(scope, { follow = false }) {
3618
+ const workspaceId = this._meshIdOf(scope);
3699
3619
  const account = this._accountMesh();
3700
- if (account) {
3620
+ if (account && (await this._holdsPublic(scope))) {
3621
+ await this._settlePublicMesh(account, scope.key).catch((err) => {
3622
+ if (!this.closing) {
3623
+ this._log?.warn(['p2p', 'mesh'], 'following a public mesh failed', err);
3624
+ }
3625
+ });
3626
+ }
3627
+ else if (account) {
3701
3628
  await this._followPublicMesh(account);
3702
3629
  }
3703
3630
  const opened = this._publicMesh(workspaceId);
@@ -3722,6 +3649,8 @@ export class Core extends ReadyResource {
3722
3649
  if (!(await this._publicFollow.lock())) {
3723
3650
  return;
3724
3651
  }
3652
+ const started = Date.now();
3653
+ const held = this._publicIds.size;
3725
3654
  try {
3726
3655
  const current = this._accountMesh();
3727
3656
  for (let attempt = 1; !this.closing && current; attempt++) {
@@ -3742,6 +3671,13 @@ export class Core extends ReadyResource {
3742
3671
  }
3743
3672
  finally {
3744
3673
  this._publicFollow.unlock();
3674
+ const opened = this._publicIds.size - held;
3675
+ if (opened > 0) {
3676
+ this._log?.info(['p2p', 'mesh'], 'followed the public meshes', {
3677
+ opened,
3678
+ ms: Date.now() - started
3679
+ });
3680
+ }
3745
3681
  }
3746
3682
  }
3747
3683
  async _publicWorkspaces(account) {
@@ -3768,7 +3704,7 @@ export class Core extends ReadyResource {
3768
3704
  async _settlePublicMeshes(account) {
3769
3705
  let settled = true;
3770
3706
  try {
3771
- await this._settleOwnWorkspace(account);
3707
+ settled = await this._settleOwnWorkspace(account);
3772
3708
  }
3773
3709
  catch (err) {
3774
3710
  settled = false;
@@ -3795,7 +3731,7 @@ export class Core extends ReadyResource {
3795
3731
  }
3796
3732
  async _settleOwnWorkspace(account) {
3797
3733
  if (this._localDevice && !account.ourMemberId) {
3798
- return;
3734
+ return true;
3799
3735
  }
3800
3736
  const local = (await this.local.listMeshes()).filter((row) => !b4a.equals(row.key, account.key) &&
3801
3737
  !row.workspaceKey &&
@@ -3805,11 +3741,14 @@ export class Core extends ReadyResource {
3805
3741
  const own = ours
3806
3742
  .filter((row) => row.initial && !row.kicked)
3807
3743
  .sort((a, b) => (b.joinedAt || Infinity) - (a.joinedAt || Infinity))[0];
3744
+ if (!own && !account.joined) {
3745
+ return false;
3746
+ }
3808
3747
  const mints = !own &&
3809
3748
  !(await this._markDerivedInitial(account, rows)) &&
3810
3749
  (await this._mintsOwnWorkspace(account, { rows, local: ours }));
3811
3750
  if (!this._isAccount(account)) {
3812
- return;
3751
+ return true;
3813
3752
  }
3814
3753
  this._setOwnWorkspace(own ? b4a.toHex(own.key) : null);
3815
3754
  if (mints) {
@@ -3819,6 +3758,7 @@ export class Core extends ReadyResource {
3819
3758
  this._ownDecided = account;
3820
3759
  this._notifyStatus();
3821
3760
  }
3761
+ return true;
3822
3762
  }
3823
3763
  async _mintsOwnWorkspace(account, { rows, local }) {
3824
3764
  if (!rows.length && !local.length) {
@@ -3924,7 +3864,19 @@ export class Core extends ReadyResource {
3924
3864
  });
3925
3865
  this._publicDrops.set(workspaceId, drop);
3926
3866
  }
3927
- async _settlePublicMesh(account, workspaceKey) {
3867
+ _settlePublicMesh(account, workspaceKey) {
3868
+ const workspaceId = b4a.toHex(workspaceKey);
3869
+ const running = this._publicSettles.get(workspaceId);
3870
+ if (running) {
3871
+ return running;
3872
+ }
3873
+ const settle = this._settleOnePublicMesh(account, workspaceKey).finally(() => {
3874
+ this._publicSettles.delete(workspaceId);
3875
+ });
3876
+ this._publicSettles.set(workspaceId, settle);
3877
+ return settle;
3878
+ }
3879
+ async _settleOnePublicMesh(account, workspaceKey) {
3928
3880
  const workspaceId = b4a.toHex(workspaceKey);
3929
3881
  const key = await publicMeshKey(account, workspaceKey);
3930
3882
  const recorded = await this._publicRecord(account, workspaceKey);
@@ -3963,9 +3915,7 @@ export class Core extends ReadyResource {
3963
3915
  else if (key && !b4a.equals(key, held.key)) {
3964
3916
  this._mergeLostMint(account, held, key);
3965
3917
  }
3966
- if (workspaceId === this._ownWorkspaceId) {
3967
- this._migrateIntoPublic(account, workspaceKey);
3968
- }
3918
+ this._upgrade.copyAccount(account);
3969
3919
  }
3970
3920
  async _publicRecord(account, workspaceKey) {
3971
3921
  const value = await this.local.get(publicRecordKey(account.key, workspaceKey));
@@ -3986,6 +3936,7 @@ export class Core extends ReadyResource {
3986
3936
  writerSeed: record.writerSeed,
3987
3937
  bootFrom: record.key ? this._takePublicBoot(record.key) : null
3988
3938
  };
3939
+ const started = Date.now();
3989
3940
  const mesh = this._newMesh(session, 'public');
3990
3941
  mesh.workspaceKey = workspaceKey;
3991
3942
  const deviceId = this._requireDeviceKeyPair().publicKey;
@@ -4010,7 +3961,8 @@ export class Core extends ReadyResource {
4010
3961
  mesh: this._meshIdOf(mesh).slice(0, 8),
4011
3962
  workspace: b4a.toHex(workspaceKey).slice(0, 8),
4012
3963
  minted: !session.baseKey,
4013
- writable: mesh.writable
3964
+ writable: mesh.writable,
3965
+ ms: Date.now() - started
4014
3966
  });
4015
3967
  return mesh;
4016
3968
  }
@@ -4045,7 +3997,7 @@ export class Core extends ReadyResource {
4045
3997
  }
4046
3998
  this._publicIds.set(workspaceId, this._meshIdOf(winner));
4047
3999
  this._notifyStatus();
4048
- await this._publicMigration;
4000
+ await this._upgrade.accountCopy;
4049
4001
  await copyContent({
4050
4002
  from: lost,
4051
4003
  into: winner,
@@ -4070,77 +4022,6 @@ export class Core extends ReadyResource {
4070
4022
  });
4071
4023
  this._publicMerges.set(workspaceId, merge);
4072
4024
  }
4073
- _migrateIntoPublic(account, workspaceKey) {
4074
- if (this._publicMigration) {
4075
- this._copyAgain = true;
4076
- return;
4077
- }
4078
- this._publicMigration = (async () => {
4079
- const into = this._publicMesh(b4a.toHex(workspaceKey));
4080
- const workspace = into && (await this._heldMesh(workspaceKey));
4081
- if (!into || !workspace?.minted) {
4082
- return;
4083
- }
4084
- const chats = await oldChats(account);
4085
- const copied = `${this._meshIdOf(account)}:${chats?.mark}`;
4086
- if (this._accountCopied === copied) {
4087
- return;
4088
- }
4089
- if (!chats) {
4090
- await this._recordCopied(account, workspaceKey);
4091
- this._accountCopied = copied;
4092
- return;
4093
- }
4094
- await into.waitForWritable();
4095
- if (!this._isAccount(account) || !this._isPooled(into)) {
4096
- return;
4097
- }
4098
- await copyContent({
4099
- from: account,
4100
- into,
4101
- deviceId: this._requireDeviceKeyPair().publicKey,
4102
- endsOpenRuns: true
4103
- });
4104
- await this._recordCopied(account, workspaceKey);
4105
- if (await this._markCopied(account, workspaceKey)) {
4106
- this._accountCopied = copied;
4107
- }
4108
- this._log?.info(['p2p', 'mesh'], 'copied the account into its public mesh', {
4109
- mesh: this._meshIdOf(into).slice(0, 8)
4110
- });
4111
- })()
4112
- .catch((err) => {
4113
- if (!this.closing) {
4114
- this._log?.warn(['p2p', 'mesh'], 'copying the account into its public mesh failed', err);
4115
- }
4116
- })
4117
- .finally(() => {
4118
- this._publicMigration = null;
4119
- if (this._copyAgain && !this.closing) {
4120
- this._copyAgain = false;
4121
- this._migrateIntoPublic(account, workspaceKey);
4122
- }
4123
- });
4124
- }
4125
- async _recordCopied(account, workspaceKey) {
4126
- const record = await this._publicRecord(account, workspaceKey);
4127
- if (record && !record.migrated) {
4128
- await this._savePublicRecord(account, workspaceKey, { ...record, migrated: true });
4129
- }
4130
- }
4131
- async _markCopied(account, workspaceKey) {
4132
- const row = await account.view.get(JOINED_MESHES, { meshKey: workspaceKey });
4133
- if (!row || !account.writable) {
4134
- return false;
4135
- }
4136
- if (!row.initial) {
4137
- const { meshKey, memberId, joinedAt } = row;
4138
- await account.dispatch('put-joined-mesh', {
4139
- room: { meshKey, memberId, joinedAt, initial: true }
4140
- });
4141
- }
4142
- return true;
4143
- }
4144
4025
  async _followAgentCopies(agentId) {
4145
4026
  if (!agentId && !(await this._agentCopies.lock()))
4146
4027
  return;
@@ -4402,7 +4283,7 @@ export class Core extends ReadyResource {
4402
4283
  await this._rotatePublicWriters(account);
4403
4284
  }
4404
4285
  async _dropKickedCopy(meshId) {
4405
- await this._teardownMesh(await this._openPooledMesh(meshId, 'leave'), { purge: true });
4286
+ await this._teardownMesh(this._pooled(meshId) ?? b4a.from(meshId, 'hex'));
4406
4287
  }
4407
4288
  async _rotatePublicWriters(account) {
4408
4289
  for (const workspaceKey of await this._publicWorkspaces(account)) {
@@ -4429,14 +4310,62 @@ export class Core extends ReadyResource {
4429
4310
  return null;
4430
4311
  return this._meshes.get(meshKey)?.mesh ?? null;
4431
4312
  }
4432
- async _isOurMemberDevice(deviceId) {
4433
- const account = this._accountMesh();
4434
- const ours = account?.ourMemberId;
4435
- if (!account || !ours || account.closing) {
4436
- return false;
4313
+ async _backfillWriterLinks(mesh) {
4314
+ try {
4315
+ await mesh.waitForWritable();
4316
+ await mesh.autobee.update();
4317
+ const marked = await backfillWriterLinks(mesh);
4318
+ if (marked > 0) {
4319
+ this._log?.info(['p2p', 'mesh'], 'marked linked writers', {
4320
+ mesh: b4a.toHex(mesh.key).slice(0, 8),
4321
+ marked
4322
+ });
4323
+ }
4324
+ }
4325
+ catch (err) {
4326
+ if (!mesh.closing) {
4327
+ mesh.reportError(err);
4328
+ }
4437
4329
  }
4438
- const row = await account.view.get(DEVICES, { id: deviceId });
4439
- return !!row && !row.revoked && !!row.memberId && b4a.equals(row.memberId, ours);
4330
+ }
4331
+ _upgradeHost() {
4332
+ const core = this;
4333
+ return {
4334
+ get closing() {
4335
+ return core.closing;
4336
+ },
4337
+ get local() {
4338
+ return core.local;
4339
+ },
4340
+ get log() {
4341
+ return core._log;
4342
+ },
4343
+ get ownWorkspaceId() {
4344
+ return core._ownWorkspaceId;
4345
+ },
4346
+ accountMesh: () => this._accountMesh(),
4347
+ agentPicks: () => this._agentPicks(),
4348
+ followProjects: (workspace) => this._followProjects(workspace),
4349
+ following: (meshId) => this._following.get(meshId),
4350
+ heldMesh: (key) => this._heldMesh(key),
4351
+ isAccount: (mesh) => this._isAccount(mesh),
4352
+ isPooled: (mesh) => this._isPooled(mesh),
4353
+ listProject: (workspace, project, listing) => this._listProject(workspace, project, listing),
4354
+ meshIdOf: (mesh) => this._meshIdOf(mesh),
4355
+ notifyStatus: () => this._notifyStatus(),
4356
+ openNewProject: (workspace, project) => this._openNewProject(workspace, project),
4357
+ openPooledMesh: (meshId) => this._openPooledMesh(meshId),
4358
+ pooled: (meshId) => this._pooled(meshId),
4359
+ publicMesh: (workspaceId) => this._publicMesh(workspaceId),
4360
+ publicRecord: (account, workspaceKey) => this._publicRecord(account, workspaceKey),
4361
+ publicWriter: (mesh) => this._publicWriters.has(mesh),
4362
+ purgeProject: (key) => this._purgeProject(key),
4363
+ reopenInRole: (mesh, role, reason) => this._reopenInRole(mesh, role, reason),
4364
+ requireDeviceKeyPair: () => this._requireDeviceKeyPair(),
4365
+ savePublicRecord: (account, workspaceKey, record) => this._savePublicRecord(account, workspaceKey, record),
4366
+ storedAccountKey: () => this._storedAccountKey(),
4367
+ writableAccount: () => this._writableAccount()
4368
+ };
4440
4369
  }
4441
4370
  _accountMesh() {
4442
4371
  return this._pooled(this._accountId);
@@ -4480,25 +4409,9 @@ export class Core extends ReadyResource {
4480
4409
  return indexed ? this._pooled(b4a.toHex(indexed)) : null;
4481
4410
  }
4482
4411
  _pendingUpdatesFor(mesh) {
4483
- const logged = (what) => (err) => this._log?.warn(['p2p', 'mesh'], `${what} the pending view update failed`, err);
4484
4412
  return {
4485
- read: async () => {
4486
- const row = await this.local.readMeshUpdate(mesh().key);
4487
- if (!row) {
4488
- return null;
4489
- }
4490
- const from = row.fromKey ? { key: row.fromKey, length: row.fromLength ?? 0 } : null;
4491
- return { from, to: 0, incremental: !!row.incremental };
4492
- },
4493
- write: ({ from, incremental }) => this.local
4494
- .writeMeshUpdate({
4495
- meshKey: mesh().key,
4496
- fromKey: from?.key,
4497
- fromLength: from?.length,
4498
- incremental
4499
- })
4500
- .catch(logged('writing')),
4501
- clear: () => this.local.clearMeshUpdate(mesh().key).catch(logged('clearing'))
4413
+ read: async () => viewUpdateOf(await this.local.readMeshUpdate(mesh().key)),
4414
+ update: (change) => this.local.updateMeshUpdate(mesh().key, (row) => meshUpdateRow(mesh().key, change(viewUpdateOf(row))))
4502
4415
  };
4503
4416
  }
4504
4417
  _fileIndexFor(mesh) {
@@ -4704,13 +4617,18 @@ export class Core extends ReadyResource {
4704
4617
  void mesh.refreshMembership().catch((error) => {
4705
4618
  this._log?.warn(['p2p', 'mesh'], 'tallying membership failed', error);
4706
4619
  });
4707
- void mesh
4708
- .refreshName()
4709
- .then(() => {
4710
- void this._keepName(mesh);
4711
- this._notifyStatus();
4712
- })
4713
- .catch((error) => this._log?.warn(['p2p', 'mesh'], 'reading the mesh name failed', error));
4620
+ const readName = () => {
4621
+ void mesh.refreshName().then(() => {
4622
+ void this._keepName(mesh);
4623
+ this._notifyStatus();
4624
+ }, (error) => {
4625
+ if (mesh.closing)
4626
+ return;
4627
+ mesh.reportError(error);
4628
+ mesh.once('peer-add', readName);
4629
+ });
4630
+ };
4631
+ readName();
4714
4632
  this._syncMeshInBackground(mesh);
4715
4633
  if (inRole(mesh.role, '@qvac/put-chat') && !this._isForeign(mesh)) {
4716
4634
  this._startContentServices(entry, deviceId);
@@ -4721,9 +4639,16 @@ export class Core extends ReadyResource {
4721
4639
  mesh.on('notification', (event) => this.emit('notification', event));
4722
4640
  const build = () => rebuildChatOrder(mesh, this.local)
4723
4641
  .then((gone) => {
4642
+ mesh.chatOrderStale = false;
4724
4643
  void retireGoneChats(mesh, this.local, gone).catch((error) => this._log?.warn(['p2p', 'mesh'], 'retiring gone chats failed', error));
4725
4644
  })
4726
- .catch((error) => this._log?.warn(['p2p', 'mesh'], 'building the chat order failed', error));
4645
+ .catch((error) => {
4646
+ if (isCoreError(error, 'MESH_VIEW_UNAVAILABLE')) {
4647
+ mesh.chatOrderStale = true;
4648
+ return;
4649
+ }
4650
+ this._log?.warn(['p2p', 'mesh'], 'building the chat order failed', error);
4651
+ });
4727
4652
  let built = build();
4728
4653
  mesh.chatOrderBuilt = built;
4729
4654
  mesh.syncChatOrder = async (chatIds) => {
@@ -4734,6 +4659,12 @@ export class Core extends ReadyResource {
4734
4659
  built = built.then(build);
4735
4660
  mesh.chatOrderBuilt = built;
4736
4661
  });
4662
+ mesh.on('background-synced', () => {
4663
+ if (!mesh.chatOrderStale)
4664
+ return;
4665
+ built = built.then(build);
4666
+ mesh.chatOrderBuilt = built;
4667
+ });
4737
4668
  mesh.on('chat-tombstone', (chatId, deletedAt) => {
4738
4669
  void this.local
4739
4670
  .clearChatNotify(chatId)
@@ -4799,6 +4730,7 @@ export class Core extends ReadyResource {
4799
4730
  };
4800
4731
  void this._indexMeshFiles(mesh).then(() => Promise.all([reclaim(), sweep()]));
4801
4732
  void this._backfillChatChunks(mesh);
4733
+ void this._backfillWriterLinks(mesh);
4802
4734
  mesh.autobee.on('move-to', () => void sweep());
4803
4735
  }
4804
4736
  async _discardCandidateMesh() {
@@ -4823,17 +4755,11 @@ export class Core extends ReadyResource {
4823
4755
  if (!mesh.writable)
4824
4756
  return false;
4825
4757
  const deviceId = this._requireDeviceKeyPair().publicKey;
4826
- const snap = mesh.view.snapshot();
4827
- try {
4828
- for await (const device of snap.find(DEVICES)) {
4829
- if (!device.revoked && !b4a.equals(device.id, deviceId))
4830
- return true;
4831
- }
4832
- return false;
4833
- }
4834
- finally {
4835
- await snap.close();
4758
+ for await (const device of mesh.view.find(DEVICES)) {
4759
+ if (!device.revoked && !b4a.equals(device.id, deviceId))
4760
+ return true;
4836
4761
  }
4762
+ return false;
4837
4763
  }
4838
4764
  async _announceDeparture(mesh = this._accountMesh(), strict = false) {
4839
4765
  if (!mesh?.writable) {
@@ -5101,6 +5027,61 @@ export class Core extends ReadyResource {
5101
5027
  }))
5102
5028
  };
5103
5029
  }
5030
+ async workspaceUpgradeSummary() {
5031
+ if (!this.opened)
5032
+ await this.ready();
5033
+ const held = new Set((await this.local.listMeshes())
5034
+ .filter((row) => !this._isHidden(b4a.toHex(row.key), row.workspaceKey))
5035
+ .map((row) => b4a.toHex(row.key)));
5036
+ const recorded = groupRecordsByWorkspace(await this.local.listPrefix(UPGRADE_RECORDS));
5037
+ const workspaces = await Promise.all([...recorded]
5038
+ .filter(([meshId]) => held.has(meshId))
5039
+ .map(([meshId, records]) => this._upgradedWorkspace(meshId, records)));
5040
+ const listed = workspaces.filter((workspace) => workspace !== null);
5041
+ const account = this._accountMesh();
5042
+ if (!account || account.closing) {
5043
+ return { workspaces: listed, privateChatsPending: false };
5044
+ }
5045
+ const messages = oldMessages(account);
5046
+ return {
5047
+ workspaces: listed,
5048
+ privateChatsWorkspaceId: await this._privateChatsCopiedInto(account, held, messages),
5049
+ privateChatsPending: await this._privateChatsCopying(account, held, messages)
5050
+ };
5051
+ }
5052
+ async _upgradedWorkspace(meshId, records) {
5053
+ const unread = convertedHere(records)
5054
+ ? { meshId, pending: true, hasGroups: recordsGroup(records) }
5055
+ : null;
5056
+ if (this._upgradeRequired || this._haltedMeshes.has(meshId)) {
5057
+ return unread;
5058
+ }
5059
+ const mesh = await this._openPooledMesh(meshId).catch(noop);
5060
+ if (!mesh || mesh.closing) {
5061
+ return unread;
5062
+ }
5063
+ const read = async () => {
5064
+ const ours = mesh.ourMemberId ?? (await mesh.refreshOurMember())?.memberId ?? null;
5065
+ return upgradedWorkspace(meshId, { records, view: mesh.view, ours });
5066
+ };
5067
+ return read().catch((err) => ifClosing(mesh, err, unread));
5068
+ }
5069
+ async _privateChatsCopiedInto(account, held, messages) {
5070
+ const prefix = publicRecordsOf(account.key);
5071
+ const workspaceId = migratedWorkspace(await this.local.listPrefix(prefix), prefix);
5072
+ if (!workspaceId || !held.has(workspaceId)) {
5073
+ return undefined;
5074
+ }
5075
+ return (await messages()) ? workspaceId : undefined;
5076
+ }
5077
+ async _privateChatsCopying(account, held, messages) {
5078
+ const own = this._ownWorkspaceId;
5079
+ if (own === null || !held.has(own) || this._pooled(own)?.minted === false) {
5080
+ return false;
5081
+ }
5082
+ const record = await this._publicRecord(account, b4a.from(own, 'hex'));
5083
+ return !record?.migrated && messages();
5084
+ }
5104
5085
  async archivedMeshContents(meshId) {
5105
5086
  if (!this.opened)
5106
5087
  await this.ready();
@@ -5174,6 +5155,7 @@ export class Core extends ReadyResource {
5174
5155
  storage: this.storage,
5175
5156
  deviceId: this._requireDeviceKeyPair().publicKey,
5176
5157
  logger: this._log,
5158
+ onerror: this._onerror,
5177
5159
  publicRecord
5178
5160
  }, fn);
5179
5161
  }
@@ -5243,6 +5225,8 @@ export class Core extends ReadyResource {
5243
5225
  }
5244
5226
  }
5245
5227
  await mesh.setName(trimmed);
5228
+ await mesh.refreshName();
5229
+ await this._keepName(mesh);
5246
5230
  }
5247
5231
  async createWorkspace({ name, icon } = {}) {
5248
5232
  if (!this.opened)
@@ -5319,13 +5303,13 @@ export class Core extends ReadyResource {
5319
5303
  }
5320
5304
  async _listProject(workspace, project, listing) {
5321
5305
  const batch = workspace.batch();
5322
- const listed = { id: project.key, ...listing, seedFingerprint: seedFingerprint(project.seed) };
5306
+ const { memberIds, ...fields } = listing;
5307
+ const listed = { id: project.key, ...fields, seedFingerprint: seedFingerprint(project.seed) };
5323
5308
  batch.dispatch('put-project', { project: listed });
5324
5309
  const ours = workspace.ourMemberId;
5325
- const sealing = ours && { projectId: project.key, seed: project.seed, sealedBy: ours };
5326
- const own = sealing ? await dueProjectKey(workspace.view, ours, sealing) : null;
5327
- if (own) {
5328
- batch.dispatch('put-project-key', own);
5310
+ if (ours) {
5311
+ const row = { ...listed, memberIds: memberIds ?? [ours] };
5312
+ await sealMembers(batch, { view: workspace.view, row, seed: project.seed, sealedBy: ours });
5329
5313
  }
5330
5314
  await batch.flush();
5331
5315
  }
@@ -5333,7 +5317,7 @@ export class Core extends ReadyResource {
5333
5317
  const named = mesh.name;
5334
5318
  const id = b4a.toHex(mesh.key);
5335
5319
  const kept = this._meshNames.get(id);
5336
- if (mesh.role !== 'project' || !named || (kept && kept.namedAt >= named.namedAt)) {
5320
+ if (!named || (kept && kept.namedAt >= named.namedAt)) {
5337
5321
  return;
5338
5322
  }
5339
5323
  const row = { meshKey: mesh.key, name: named.name, namedAt: named.namedAt };
@@ -5342,7 +5326,7 @@ export class Core extends ReadyResource {
5342
5326
  await this.local.keepMeshName(row);
5343
5327
  }
5344
5328
  catch (err) {
5345
- this._log?.warn(['p2p', 'mesh'], 'keeping the project name failed', err);
5329
+ this._log?.warn(['p2p', 'mesh'], 'keeping the mesh name failed', err);
5346
5330
  }
5347
5331
  }
5348
5332
  async _openNewProject(workspace, project) {
@@ -5473,6 +5457,10 @@ export class Core extends ReadyResource {
5473
5457
  await this._discardMesh(mesh);
5474
5458
  throw error;
5475
5459
  }
5460
+ if (workspace) {
5461
+ await mesh.refreshName();
5462
+ await this._keepName(mesh);
5463
+ }
5476
5464
  this._notifyStatus();
5477
5465
  return this._meshIdOf(mesh);
5478
5466
  }
@@ -5558,6 +5546,7 @@ export class Core extends ReadyResource {
5558
5546
  if (this._debugBeforeMeshOpen) {
5559
5547
  await this._debugBeforeMeshOpen(meshId);
5560
5548
  }
5549
+ this._refuseClosing();
5561
5550
  const started = Date.now();
5562
5551
  const key = this._meshKeyOf(meshId);
5563
5552
  const session = await this.local.readMeshSession(key);
@@ -5614,7 +5603,7 @@ export class Core extends ReadyResource {
5614
5603
  open: mesh !== null,
5615
5604
  account,
5616
5605
  label: joined?.label || undefined,
5617
- name: mesh?.name?.name || undefined,
5606
+ name: (mesh?.name ?? this._meshNames.get(meshId))?.name || undefined,
5618
5607
  members: mesh?.membership.members ?? 0,
5619
5608
  shared: joined?.shared ?? false,
5620
5609
  sharedAt: joined?.shared ? joined.sharedAt : undefined,
@@ -5790,9 +5779,10 @@ export class Core extends ReadyResource {
5790
5779
  if (!this.opened)
5791
5780
  await this.ready();
5792
5781
  const key = await this._storedAccountKey();
5782
+ const meshId = key ? b4a.toHex(key) : undefined;
5793
5783
  return {
5794
- meshId: key ? b4a.toHex(key) : undefined,
5795
- writable: this._accountWritable(),
5784
+ meshId,
5785
+ writable: meshId === this._accountId && this._accountWritable(),
5796
5786
  displayName: (await this.displayName()) ?? undefined
5797
5787
  };
5798
5788
  }
@@ -5909,16 +5899,9 @@ export class Core extends ReadyResource {
5909
5899
  const inviterName = memberId
5910
5900
  ? ((await mesh.view.get(MEMBERS, { id: memberId }))?.name ?? undefined)
5911
5901
  : undefined;
5912
- const snap = mesh.view.snapshot();
5913
- let blindPeerRows;
5914
- try {
5915
- blindPeerRows = await snap
5916
- .find(MESH_BLIND_PEERS, { limit: INVITE_BLIND_PEERS_LIMIT })
5917
- .toArray();
5918
- }
5919
- finally {
5920
- await snap.close();
5921
- }
5902
+ const blindPeerRows = await mesh.view
5903
+ .find(MESH_BLIND_PEERS, { limit: INVITE_BLIND_PEERS_LIMIT })
5904
+ .toArray();
5922
5905
  return {
5923
5906
  baseKey: mesh.key,
5924
5907
  seed: mesh.seed,
@@ -5934,7 +5917,8 @@ export class Core extends ReadyResource {
5934
5917
  deviceProof: delegation?.deviceProof,
5935
5918
  workspace,
5936
5919
  workspaceName: opts.memberInvite ? mesh.name?.name || undefined : undefined,
5937
- publicHeads: opts.memberInvite ? [] : await this._publicHeads()
5920
+ publicHeads: opts.memberInvite ? [] : await this._publicHeads(),
5921
+ inviterHeads: mesh.autobee.system.heads.map(({ key, length }) => ({ key, length }))
5938
5922
  };
5939
5923
  }
5940
5924
  async _publicHeads() {
@@ -6014,6 +5998,7 @@ export class Core extends ReadyResource {
6014
5998
  await Promise.all([...this._meshes.values()].map(({ mesh }) => swarm.leave(mesh.discoveryKey)));
6015
5999
  return;
6016
6000
  }
6001
+ swarm.on('connection', (conn) => conn.on('error', noop));
6017
6002
  for (const conn of swarm.connections) {
6018
6003
  conn.destroy?.();
6019
6004
  }
@@ -6252,6 +6237,11 @@ export class Core extends ReadyResource {
6252
6237
  return 'skipped';
6253
6238
  }
6254
6239
  const role = joined.workspace ? 'workspace' : undefined;
6240
+ this._log?.info(['p2p', 'mesh'], 'mesh role chosen', {
6241
+ mesh: b4a.toHex(meshKey).slice(0, 8),
6242
+ role: role ?? 'project',
6243
+ source: 'account row'
6244
+ });
6255
6245
  const session = this.local.createCandidateSession(held.seed, meshKey, { role });
6256
6246
  await this.local.recordMesh(meshKey, session, {
6257
6247
  deviceId: this._requireDeviceKeyPair().publicKey
@@ -6330,8 +6320,23 @@ export class Core extends ReadyResource {
6330
6320
  }
6331
6321
  });
6332
6322
  }
6323
+ await this._dropUnlistedFollows(workspace);
6333
6324
  void this._sealProjectKeys();
6334
6325
  }
6326
+ async _dropUnlistedFollows(workspace) {
6327
+ for (const row of await this.local.listMeshes()) {
6328
+ const meshId = b4a.toHex(row.key);
6329
+ if (row.role !== 'project' ||
6330
+ !row.awaitingAdmission ||
6331
+ !row.workspaceKey ||
6332
+ !b4a.equals(row.workspaceKey, workspace.key) ||
6333
+ this._following.has(meshId) ||
6334
+ (await readProject(workspace.view, row.key))) {
6335
+ continue;
6336
+ }
6337
+ await this._trackFollow(meshId, () => this._purgeProject(row.key));
6338
+ }
6339
+ }
6335
6340
  _mayFollowProjects(workspace) {
6336
6341
  const leaving = this._leaving !== null &&
6337
6342
  (this._leaving === this._accountId || this._leaving === this._meshIdOf(workspace));
@@ -6378,19 +6383,7 @@ export class Core extends ReadyResource {
6378
6383
  return;
6379
6384
  }
6380
6385
  const batch = workspace.batch();
6381
- for (const memberId of row.memberIds ?? []) {
6382
- const due = await dueProjectKey(workspace.view, memberId, {
6383
- projectId: row.id,
6384
- seed,
6385
- sealedBy
6386
- });
6387
- if (due) {
6388
- batch.dispatch('put-project-key', due);
6389
- }
6390
- }
6391
- for (const due of await dueProjectDeviceKeys(workspace.view, row, { seed, sealedBy })) {
6392
- batch.dispatch('put-project-device-key', due);
6393
- }
6386
+ await sealMembers(batch, { view: workspace.view, row, seed, sealedBy });
6394
6387
  await batch.flush();
6395
6388
  }
6396
6389
  async _sealProjectHosts(workspace, projectId, seed) {
@@ -6657,6 +6650,8 @@ export class Core extends ReadyResource {
6657
6650
  conn.on('error', noop);
6658
6651
  return;
6659
6652
  }
6653
+ conn.setTimeout(PEER_STREAM_TIMEOUT_MS);
6654
+ conn.setKeepAlive(PEER_KEEP_ALIVE_MS);
6660
6655
  const remoteId = b4a.toHex(conn.remotePublicKey).slice(0, 8);
6661
6656
  const start = Date.now();
6662
6657
  let bytes = 0;
@@ -6867,6 +6862,7 @@ export class Core extends ReadyResource {
6867
6862
  leaveWorkspace: (meshId) => this.leaveWorkspace(meshId),
6868
6863
  createProject: (workspaceId, opts) => this.createProject(workspaceId, opts),
6869
6864
  listArchived: () => this.listArchivedMeshes(),
6865
+ upgradeSummary: () => this.workspaceUpgradeSummary(),
6870
6866
  archivedContents: (meshId) => this.archivedMeshContents(meshId),
6871
6867
  migrateArchived: (meshId, workspaceId) => this.migrateArchivedMesh(meshId, workspaceId),
6872
6868
  recoverable: () => this.accountRecoverable(),
@@ -7146,15 +7142,17 @@ export class Core extends ReadyResource {
7146
7142
  }
7147
7143
  };
7148
7144
  }
7149
- registerEmbedder(embedder, extractors) {
7145
+ registerEmbedder(embedder, extractors, { knowledge = null } = {}) {
7150
7146
  this._embedder = embedder;
7151
7147
  this._ragExtractors = extractors ?? this._ragExtractors;
7148
+ this._knowledge = knowledge;
7152
7149
  const settled = this._reconcileIndexing();
7153
7150
  settled.catch((err) => this._log?.warn(['indexing'], 'reconcile failed', err));
7154
7151
  return () => {
7155
7152
  if (this._embedder !== embedder)
7156
7153
  return settled;
7157
7154
  this._embedder = null;
7155
+ this._knowledge = null;
7158
7156
  return this._reconcileIndexing();
7159
7157
  };
7160
7158
  }
@@ -7185,7 +7183,10 @@ export class Core extends ReadyResource {
7185
7183
  this._isForeign(entry.mesh)) {
7186
7184
  return;
7187
7185
  }
7188
- entry.vectorIndex ??= this._takeVectorIndex();
7186
+ if (this._knowledge && !(entry.vectorIndex instanceof PluginVectorIndex)) {
7187
+ this._releaseVectorIndex(entry);
7188
+ }
7189
+ entry.vectorIndex ??= this._takeVectorIndex(entry, this._embedder.model);
7189
7190
  entry.processingFiles = entry.mesh.files;
7190
7191
  entry.processingFiles.startProcessing({
7191
7192
  embedder: this._embedder,
@@ -7193,11 +7194,15 @@ export class Core extends ReadyResource {
7193
7194
  deviceId: this.devicePublicKey,
7194
7195
  device: this._indexingDevice,
7195
7196
  extractors: this._ragExtractors,
7196
- batches: this._embedBatches
7197
+ batches: this._embedBatches,
7198
+ textDir: this._textDir(entry.mesh.key)
7197
7199
  });
7198
7200
  this._indexedEmbedder = this._embedder;
7199
7201
  }
7200
- _takeVectorIndex() {
7202
+ _takeVectorIndex(entry, model) {
7203
+ if (this._knowledge) {
7204
+ return new PluginVectorIndex(this._knowledge, { mesh: b4a.toHex(entry.mesh.key), model });
7205
+ }
7201
7206
  const index = this._spareVectorIndex ?? new MemoryVectorIndex();
7202
7207
  this._spareVectorIndex = null;
7203
7208
  return index;
@@ -7465,17 +7470,18 @@ async function closeConfigReader(reader) {
7465
7470
  }
7466
7471
  async function inviteHead(mesh) {
7467
7472
  const auto = mesh.autobee;
7468
- const info = await auto.system.getInfo();
7469
- const head = info && info.view.length > 0 ? await auto.trusted.mostRecentTrusted(auto.view, null) : null;
7473
+ const head = mesh.view.db.head()?.length
7474
+ ? await auto.trusted.mostRecentTrusted(auto.view, null)
7475
+ : null;
7470
7476
  return head ? { key: head.key, length: head.length } : null;
7471
7477
  }
7472
7478
  async function recordedBlockIsLocal(core) {
7473
- await raceResource(core, core.ready(), 'config core');
7474
- const length = Number((await raceResource(core, core.getUserData('hyperconf'), 'config core')) || '0');
7479
+ await core.ready();
7480
+ const length = Number((await core.getUserData('hyperconf')) || '0');
7475
7481
  if (length <= 0) {
7476
7482
  return true;
7477
7483
  }
7478
- return raceResource(core, core.has(length - 1), 'config core');
7484
+ return core.has(length - 1);
7479
7485
  }
7480
7486
  function statusErrorMessage(context, err) {
7481
7487
  const detail = errorDetail(err);
@@ -7504,3 +7510,19 @@ function credentialKeysOwnedElsewhere(skills, skillName) {
7504
7510
  }
7505
7511
  return keys;
7506
7512
  }
7513
+ function viewUpdateOf(row) {
7514
+ if (!row) {
7515
+ return null;
7516
+ }
7517
+ const from = row.fromKey ? { key: row.fromKey, length: row.fromLength ?? 0 } : null;
7518
+ return { from, to: row.toLength ?? 0, incremental: !!row.incremental };
7519
+ }
7520
+ function meshUpdateRow(meshKey, update) {
7521
+ return (update && {
7522
+ meshKey,
7523
+ fromKey: update.from?.key,
7524
+ fromLength: update.from?.length,
7525
+ toLength: update.to,
7526
+ incremental: update.incremental
7527
+ });
7528
+ }