@qvac/core 1.0.0-rc.7 → 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 (237) hide show
  1. package/CHANGELOG.md +241 -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/device-routes.d.ts +6 -1
  8. package/dist-lib/lib/api/device-routes.js +20 -1
  9. package/dist-lib/lib/api/devices.js +6 -18
  10. package/dist-lib/lib/api/execution.d.ts +6 -2
  11. package/dist-lib/lib/api/execution.js +29 -3
  12. package/dist-lib/lib/api/host-run-log.d.ts +7 -1
  13. package/dist-lib/lib/api/host-run-log.js +3 -1
  14. package/dist-lib/lib/api/index.d.ts +3 -1
  15. package/dist-lib/lib/api/index.js +17 -6
  16. package/dist-lib/lib/api/members.js +13 -29
  17. package/dist-lib/lib/api/mesh.d.ts +2 -1
  18. package/dist-lib/lib/api/projects.d.ts +1 -1
  19. package/dist-lib/lib/api/projects.js +9 -5
  20. package/dist-lib/lib/api/rag-label.d.ts +4 -0
  21. package/dist-lib/lib/api/rag-label.js +21 -0
  22. package/dist-lib/lib/api/rag-lexical.d.ts +22 -0
  23. package/dist-lib/lib/api/rag-lexical.js +1058 -0
  24. package/dist-lib/lib/api/rag-manifest.d.ts +13 -0
  25. package/dist-lib/lib/api/rag-manifest.js +46 -0
  26. package/dist-lib/lib/api/rag-outline.d.ts +36 -0
  27. package/dist-lib/lib/api/rag-outline.js +426 -0
  28. package/dist-lib/lib/api/rag-plugin.d.ts +20 -0
  29. package/dist-lib/lib/api/rag-plugin.js +70 -0
  30. package/dist-lib/lib/api/rag-synthesis.d.ts +7 -0
  31. package/dist-lib/lib/api/rag-synthesis.js +102 -0
  32. package/dist-lib/lib/api/rag-targets.d.ts +23 -0
  33. package/dist-lib/lib/api/rag-targets.js +215 -0
  34. package/dist-lib/lib/api/rag-window.d.ts +5 -0
  35. package/dist-lib/lib/api/rag-window.js +46 -0
  36. package/dist-lib/lib/api/rag.d.ts +3 -2
  37. package/dist-lib/lib/api/rag.js +421 -184
  38. package/dist-lib/lib/api/served.js +8 -14
  39. package/dist-lib/lib/api/workspace.d.ts +8 -1
  40. package/dist-lib/lib/api/workspace.js +27 -13
  41. package/dist-lib/lib/assistant/harness.d.ts +9 -1
  42. package/dist-lib/lib/assistant/history.d.ts +11 -2
  43. package/dist-lib/lib/assistant/history.js +74 -4
  44. package/dist-lib/lib/assistant/media-round.d.ts +61 -0
  45. package/dist-lib/lib/assistant/media-round.js +111 -0
  46. package/dist-lib/lib/assistant/message.d.ts +7 -0
  47. package/dist-lib/lib/assistant/model-round.d.ts +19 -1
  48. package/dist-lib/lib/assistant/model-round.js +163 -28
  49. package/dist-lib/lib/assistant/pdf-pages.d.ts +16 -1
  50. package/dist-lib/lib/assistant/pdf-pages.js +67 -3
  51. package/dist-lib/lib/assistant/round-host.d.ts +22 -0
  52. package/dist-lib/lib/assistant/round-host.js +142 -8
  53. package/dist-lib/lib/assistant/upload.d.ts +12 -0
  54. package/dist-lib/lib/assistant/upload.js +36 -0
  55. package/dist-lib/lib/assistant-client.d.ts +1 -0
  56. package/dist-lib/lib/assistant-client.js +40 -19
  57. package/dist-lib/lib/assistant-tree.d.ts +2 -0
  58. package/dist-lib/lib/assistant-tree.js +48 -1
  59. package/dist-lib/lib/assistant.d.ts +18 -1
  60. package/dist-lib/lib/assistant.js +484 -95
  61. package/dist-lib/lib/client.d.ts +1 -0
  62. package/dist-lib/lib/client.js +4 -1
  63. package/dist-lib/lib/compaction.js +2 -2
  64. package/dist-lib/lib/core/agent.js +12 -45
  65. package/dist-lib/lib/core/approval-rules.js +1 -8
  66. package/dist-lib/lib/core/archived-mesh.d.ts +2 -1
  67. package/dist-lib/lib/core/archived-mesh.js +12 -8
  68. package/dist-lib/lib/core/chat-copy.d.ts +18 -3
  69. package/dist-lib/lib/core/chat-copy.js +74 -25
  70. package/dist-lib/lib/core/chat-fork.js +15 -6
  71. package/dist-lib/lib/core/chat-group.js +16 -24
  72. package/dist-lib/lib/core/chat-move.d.ts +9 -0
  73. package/dist-lib/lib/core/chat-move.js +94 -3
  74. package/dist-lib/lib/core/chat-order.js +12 -18
  75. package/dist-lib/lib/core/chat.js +38 -31
  76. package/dist-lib/lib/core/chunk.d.ts +2 -2
  77. package/dist-lib/lib/core/chunk.js +18 -25
  78. package/dist-lib/lib/core/collections.d.ts +2 -0
  79. package/dist-lib/lib/core/collections.js +2 -0
  80. package/dist-lib/lib/core/contract/activity.d.ts +4 -0
  81. package/dist-lib/lib/core/contract/activity.js +34 -4
  82. package/dist-lib/lib/core/contract/chat-group.d.ts +1 -0
  83. package/dist-lib/lib/core/contract/chat-group.js +21 -1
  84. package/dist-lib/lib/core/contract/chat.d.ts +9 -0
  85. package/dist-lib/lib/core/contract/chat.js +127 -5
  86. package/dist-lib/lib/core/contract/chunks.d.ts +1 -0
  87. package/dist-lib/lib/core/contract/chunks.js +37 -6
  88. package/dist-lib/lib/core/contract/features.d.ts +21 -0
  89. package/dist-lib/lib/core/contract/features.js +22 -0
  90. package/dist-lib/lib/core/contract/index.js +2 -0
  91. package/dist-lib/lib/core/contract/indexing.js +4 -2
  92. package/dist-lib/lib/core/contract/knowledge.js +5 -4
  93. package/dist-lib/lib/core/contract/members.js +16 -10
  94. package/dist-lib/lib/core/contract/mesh-kind.d.ts +1 -0
  95. package/dist-lib/lib/core/contract/mesh-kind.js +3 -1
  96. package/dist-lib/lib/core/contract/projects.d.ts +5 -0
  97. package/dist-lib/lib/core/contract/projects.js +13 -5
  98. package/dist-lib/lib/core/contract/roles.js +32 -4
  99. package/dist-lib/lib/core/contract/run-requests.d.ts +1 -0
  100. package/dist-lib/lib/core/contract/run-requests.js +35 -2
  101. package/dist-lib/lib/core/contract/seed-defaults.js +2 -2
  102. package/dist-lib/lib/core/contract/tool-configs.js +3 -2
  103. package/dist-lib/lib/core/contract/workspace.d.ts +2 -0
  104. package/dist-lib/lib/core/contract/workspace.js +40 -7
  105. package/dist-lib/lib/core/contract/writers.js +2 -2
  106. package/dist-lib/lib/core/departures.d.ts +3 -1
  107. package/dist-lib/lib/core/departures.js +3 -0
  108. package/dist-lib/lib/core/devices.js +1 -8
  109. package/dist-lib/lib/core/files/attachments.d.ts +2 -0
  110. package/dist-lib/lib/core/files/attachments.js +19 -1
  111. package/dist-lib/lib/core/files/index.d.ts +20 -4
  112. package/dist-lib/lib/core/files/index.js +132 -21
  113. package/dist-lib/lib/core/files/processing/document.d.ts +4 -0
  114. package/dist-lib/lib/core/files/processing/document.js +7 -0
  115. package/dist-lib/lib/core/files/processing/index.d.ts +13 -0
  116. package/dist-lib/lib/core/files/processing/index.js +144 -12
  117. package/dist-lib/lib/core/files/processing/protocol.js +13 -34
  118. package/dist-lib/lib/core/files/processing/state.d.ts +2 -0
  119. package/dist-lib/lib/core/files/rag/build.d.ts +19 -2
  120. package/dist-lib/lib/core/files/rag/build.js +123 -19
  121. package/dist-lib/lib/core/files/rag/failure.js +2 -1
  122. package/dist-lib/lib/core/files/rag/index.d.ts +11 -3
  123. package/dist-lib/lib/core/files/rag/index.js +13 -1
  124. package/dist-lib/lib/core/files/rag/load.d.ts +6 -3
  125. package/dist-lib/lib/core/files/rag/load.js +27 -6
  126. package/dist-lib/lib/core/files/rag/plugin-index.d.ts +32 -0
  127. package/dist-lib/lib/core/files/rag/plugin-index.js +127 -0
  128. package/dist-lib/lib/core/files/rag/produce.d.ts +5 -3
  129. package/dist-lib/lib/core/files/rag/produce.js +104 -16
  130. package/dist-lib/lib/core/files/rag/retrieval/full-text.d.ts +3 -1
  131. package/dist-lib/lib/core/files/rag/retrieval/full-text.js +62 -13
  132. package/dist-lib/lib/core/files/rag/text-cache.d.ts +28 -0
  133. package/dist-lib/lib/core/files/rag/text-cache.js +141 -0
  134. package/dist-lib/lib/core/files/records/knowledge.d.ts +1 -3
  135. package/dist-lib/lib/core/files/records/knowledge.js +7 -40
  136. package/dist-lib/lib/core/files/records/reclaim.js +5 -10
  137. package/dist-lib/lib/core/files/upload.js +68 -41
  138. package/dist-lib/lib/core/fs-drives.js +14 -26
  139. package/dist-lib/lib/core/invite.d.ts +1 -0
  140. package/dist-lib/lib/core/invite.js +4 -2
  141. package/dist-lib/lib/core/local/files.d.ts +1 -1
  142. package/dist-lib/lib/core/local/files.js +5 -5
  143. package/dist-lib/lib/core/local/index.d.ts +13 -6
  144. package/dist-lib/lib/core/local/index.js +103 -69
  145. package/dist-lib/lib/core/local/run-logs.d.ts +1 -1
  146. package/dist-lib/lib/core/local/run-logs.js +5 -5
  147. package/dist-lib/lib/core/mesh-processor.d.ts +19 -0
  148. package/dist-lib/lib/core/mesh-processor.js +87 -0
  149. package/dist-lib/lib/core/mesh.d.ts +48 -5
  150. package/dist-lib/lib/core/mesh.js +318 -129
  151. package/dist-lib/lib/core/migrations/agent-catalog.js +2 -8
  152. package/dist-lib/lib/core/migrations/agent-tools-scope.js +5 -11
  153. package/dist-lib/lib/core/migrations/workspace-from-0-21/index.d.ts +88 -0
  154. package/dist-lib/lib/core/migrations/workspace-from-0-21/index.js +731 -0
  155. package/dist-lib/lib/core/migrations/writer-links.d.ts +2 -0
  156. package/dist-lib/lib/core/migrations/writer-links.js +32 -0
  157. package/dist-lib/lib/core/project-keys.d.ts +6 -0
  158. package/dist-lib/lib/core/project-keys.js +11 -0
  159. package/dist-lib/lib/core/public-mesh.d.ts +3 -0
  160. package/dist-lib/lib/core/public-mesh.js +10 -8
  161. package/dist-lib/lib/core/run-requests.d.ts +3 -0
  162. package/dist-lib/lib/core/run-requests.js +21 -1
  163. package/dist-lib/lib/core/skill-availability.d.ts +33 -0
  164. package/dist-lib/lib/core/skill-availability.js +67 -0
  165. package/dist-lib/lib/core/sync/background.d.ts +0 -1
  166. package/dist-lib/lib/core/sync/background.js +2 -1
  167. package/dist-lib/lib/core/sync/changed.d.ts +4 -2
  168. package/dist-lib/lib/core/sync/changed.js +1 -1
  169. package/dist-lib/lib/core/sync/index.js +52 -10
  170. package/dist-lib/lib/core/sync/view-updates.d.ts +18 -12
  171. package/dist-lib/lib/core/sync/view-updates.js +82 -91
  172. package/dist-lib/lib/core/sync-status.js +97 -41
  173. package/dist-lib/lib/core/tool-activity.js +6 -12
  174. package/dist-lib/lib/core/tool-configs.js +17 -29
  175. package/dist-lib/lib/core/upgrade-summary.d.ts +19 -0
  176. package/dist-lib/lib/core/upgrade-summary.js +67 -0
  177. package/dist-lib/lib/core/view-read.d.ts +4 -0
  178. package/dist-lib/lib/core/view-read.js +23 -0
  179. package/dist-lib/lib/core/workspace-upgrade.d.ts +7 -0
  180. package/dist-lib/lib/core/workspace-upgrade.js +27 -16
  181. package/dist-lib/lib/core.d.ts +45 -42
  182. package/dist-lib/lib/core.js +675 -640
  183. package/dist-lib/lib/errors.d.ts +24 -0
  184. package/dist-lib/lib/errors.js +24 -0
  185. package/dist-lib/lib/llm-model-names.d.ts +1 -1
  186. package/dist-lib/lib/llm-model-names.js +1 -1
  187. package/dist-lib/lib/mesh-scoped-engine.js +4 -0
  188. package/dist-lib/lib/model-context.js +1 -2
  189. package/dist-lib/lib/model-download.d.ts +6 -1
  190. package/dist-lib/lib/model-download.js +16 -3
  191. package/dist-lib/lib/oauth/resolve.js +29 -8
  192. package/dist-lib/lib/oauth/token-exchange-core.js +11 -0
  193. package/dist-lib/lib/probe-rounds.d.ts +7 -2
  194. package/dist-lib/lib/probe-rounds.js +6 -1
  195. package/dist-lib/lib/run-transcript.d.ts +4 -1
  196. package/dist-lib/lib/run-transcript.js +15 -6
  197. package/dist-lib/schema/engine.js +83 -6
  198. package/dist-lib/schema/indexes.d.ts +3 -0
  199. package/dist-lib/schema/indexes.js +5 -0
  200. package/dist-lib/schema/local.js +11 -2
  201. package/dist-lib/schema/qvac.js +49 -5
  202. package/dist-lib/spec/bind.js +34 -0
  203. package/dist-lib/spec/calls.js +5 -0
  204. package/dist-lib/spec/capabilities.d.ts +17 -0
  205. package/dist-lib/spec/hrpc/hrpc.json +65 -0
  206. package/dist-lib/spec/hrpc/index.js +68 -5
  207. package/dist-lib/spec/hrpc/messages.js +260 -23
  208. package/dist-lib/spec/hyperschema/index.js +260 -23
  209. package/dist-lib/spec/hyperschema/schema.json +181 -0
  210. package/dist-lib/spec/hyperschema/types.d.ts +48 -0
  211. package/dist-lib/spec/local/hyperdb/messages.js +270 -224
  212. package/dist-lib/spec/local/hyperschema/index.js +73 -38
  213. package/dist-lib/spec/local/hyperschema/schema.json +31 -0
  214. package/dist-lib/spec/local/hyperschema/types.d.ts +8 -0
  215. package/dist-lib/spec/mesh/hyperdb/db.json +39 -2
  216. package/dist-lib/spec/mesh/hyperdb/index.js +120 -3
  217. package/dist-lib/spec/mesh/hyperdb/messages.js +1328 -838
  218. package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +30 -0
  219. package/dist-lib/spec/mesh/hyperdispatch/index.js +86 -1
  220. package/dist-lib/spec/mesh/hyperdispatch/messages.js +682 -287
  221. package/dist-lib/spec/mesh/hyperschema/index.js +682 -287
  222. package/dist-lib/spec/mesh/hyperschema/schema.json +201 -0
  223. package/dist-lib/spec/mesh/hyperschema/types.d.ts +52 -0
  224. package/package.json +82 -71
  225. package/tui/README.md +52 -0
  226. package/tui/app.mjs +69 -12
  227. package/tui/boot.mjs +27 -6
  228. package/tui/cli.mjs +93 -0
  229. package/tui/index.mjs +2 -87
  230. package/tui/lib/attachment-files.mjs +82 -0
  231. package/tui/lib/reopen-meshes.mjs +15 -0
  232. package/tui/lib/updater.mjs +86 -0
  233. package/tui/main.mjs +29 -57
  234. package/worker/assistant-sidecar.mjs +10 -3
  235. package/worker/stow-entry.mjs +3 -52
  236. package/dist-lib/lib/core/chat-project.d.ts +0 -2
  237. package/dist-lib/lib/core/chat-project.js +0 -15
@@ -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;
@@ -150,6 +190,7 @@ export class Core extends ReadyResource {
150
190
  _modelCatalogWatch;
151
191
  _modelCatalogWatchSource = new ReadyGuard();
152
192
  _retryModelDownload;
193
+ _siblingDownloads;
153
194
  _transcribe;
154
195
  _detectLanguage;
155
196
  _previewModelSource;
@@ -157,6 +198,7 @@ export class Core extends ReadyResource {
157
198
  _speakStream;
158
199
  _embedder;
159
200
  _ragExtractors;
201
+ _knowledge = null;
160
202
  _indexedEmbedder = null;
161
203
  _indexingLock = new ScopeLock();
162
204
  _embedBatches = new ScopeLock();
@@ -166,6 +208,7 @@ export class Core extends ReadyResource {
166
208
  };
167
209
  _meshTransitionLock = new ScopeLock();
168
210
  _opening = new Map();
211
+ _openingMeshSessions = new Set();
169
212
  _retiring = new Map();
170
213
  _tearingDown = new Map();
171
214
  _debugBeforeMeshOpen;
@@ -185,6 +228,7 @@ export class Core extends ReadyResource {
185
228
  _accountId;
186
229
  _offlineBoot = new ReadyGuard();
187
230
  onlineBoot = new ReadyGuard();
231
+ _upgradeBoot = null;
188
232
  _candidateMesh;
189
233
  _cancelCandidateJoin;
190
234
  _joinCancellation;
@@ -205,9 +249,7 @@ export class Core extends ReadyResource {
205
249
  _wakeup;
206
250
  _following = new Map();
207
251
  _kickedProjects = new Map();
208
- _upgrading = new Set();
209
- _groupPasses = new WeakSet();
210
- _groupPassesDue = new WeakSet();
252
+ _upgrade = new WorkspaceUpgrade(this._upgradeHost());
211
253
  _purging = new Map();
212
254
  _publicIds = new Map();
213
255
  _publicBoots = new Map();
@@ -220,12 +262,17 @@ export class Core extends ReadyResource {
220
262
  _publicFollow = new ScopeLock({ debounce: true });
221
263
  _publicMerges = new Map();
222
264
  _publicDrops = new Map();
223
- _publicMigration = null;
224
- _copyAgain = false;
225
- _accountCopied = null;
265
+ _publicSettles = new Map();
226
266
  _agentCopies = new ScopeLock({ debounce: true });
227
267
  _sharedAgents = new ScopeLock({ debounce: true });
228
- _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();
229
276
  _projectKeySeals = new ScopeLock({ debounce: true });
230
277
  _hosting = new ScopeLock({ debounce: true });
231
278
  _skillSecrets = new ScopeLock({ debounce: true });
@@ -262,7 +309,7 @@ export class Core extends ReadyResource {
262
309
  _systemInfo;
263
310
  _statusError;
264
311
  _upgradeRequired;
265
- 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 }) {
266
313
  super();
267
314
  this._transport = transport;
268
315
  this._apiLifecycle = apiLifecycle ?? {
@@ -270,6 +317,7 @@ export class Core extends ReadyResource {
270
317
  resume: () => this.resume()
271
318
  };
272
319
  this._log = logger;
320
+ this._onerror = onerror ?? logErrors(logger);
273
321
  if (swarm && (!store || !wakeup)) {
274
322
  throw new Error('swarm may only be passed alongside store and wakeup');
275
323
  }
@@ -284,7 +332,7 @@ export class Core extends ReadyResource {
284
332
  });
285
333
  this.rootCorestore = store ?? new Corestore(this.storage, { globalCache: new Rache() });
286
334
  this.store = this.rootCorestore.namespace(QVAC_CORE_NAMESPACE);
287
- this.local = openLocalStore(this.storage);
335
+ this.local = openLocalStore(this.storage, this._log);
288
336
  this.state = QvacState.RESUME;
289
337
  this._spec = spec;
290
338
  this._bootstrapNodes = bootstrapNodes;
@@ -315,6 +363,7 @@ export class Core extends ReadyResource {
315
363
  this._openModelCatalogWatchSource();
316
364
  void this._modelCatalogWatchSource.ready().catch(noop);
317
365
  this._retryModelDownload = retryModelDownload ?? null;
366
+ this._siblingDownloads = null;
318
367
  this._transcribe = transcribe ?? null;
319
368
  this._detectLanguage = detectLanguage ?? null;
320
369
  this._previewModelSource = previewModelSource ?? null;
@@ -441,11 +490,13 @@ export class Core extends ReadyResource {
441
490
  await writeAutoDownloadModels(this.local, this._autoDownloadModels);
442
491
  }
443
492
  const swarm = this._openSwarm(this._deviceKeyPair);
493
+ recoveryPeers.setRecoveryPeers([...this._resolveBlindPeerKeys()]);
444
494
  this._departures = new PendingDepartures(this.store, this.local, {
445
495
  swarm,
446
496
  isPooled: (key) => this._pooled(b4a.toHex(key)) !== null,
447
497
  deviceId: () => this._deviceKeyPair?.publicKey ?? null,
448
498
  logger: this._log,
499
+ onerror: this._onerror,
449
500
  blindPeerKeys: this._resolveBlindPeerKeys(),
450
501
  blindPeering: () => this._blindPeeringClient(),
451
502
  wakeup: this._wakeup,
@@ -519,11 +570,12 @@ export class Core extends ReadyResource {
519
570
  lifecycle: this._apiLifecycle,
520
571
  ragIndex: (meshId) => this._meshes.get(this._publicIdOf(meshId) ?? meshId)?.vectorIndex ?? null,
521
572
  ragEmbed: () => this._indexedEmbedder,
573
+ ragExtractors: () => this._ragExtractors,
522
574
  platform: () => this._localDevice?.platform ?? null,
523
575
  deletedProject: (projectId) => this._projectGone(projectId),
524
576
  projectOf: (workspaceKey, projectKey) => this._isProjectOf(workspaceKey, projectKey),
525
- nameOf: (projectKey) => {
526
- const id = b4a.toHex(projectKey);
577
+ nameOf: (meshKey) => {
578
+ const id = b4a.toHex(meshKey);
527
579
  return this._pooled(id)?.name ?? this._meshNames.get(id) ?? null;
528
580
  },
529
581
  pushSkillSecret: (request) => this.pushSkillSecret(request),
@@ -659,7 +711,10 @@ export class Core extends ReadyResource {
659
711
  : null
660
712
  });
661
713
  api.skillOauth = (request) => this._skillOauth(request);
662
- this._servedToSiblings = servedToSiblings(api);
714
+ this._servedToSiblings = servedToSiblings(api, {
715
+ asked: (name) => this._siblingDownloads?.asked(name),
716
+ resumed: (name) => this._siblingDownloads?.resumed(name)
717
+ });
663
718
  this._api = {
664
719
  ...api,
665
720
  ...forwardToDevices(api, {
@@ -731,7 +786,8 @@ export class Core extends ReadyResource {
731
786
  const mesh = await this._switchToMesh({
732
787
  seed: this._meshSeed,
733
788
  baseKey: this._meshKey,
734
- account: true
789
+ account: true,
790
+ boot: true
735
791
  });
736
792
  }
737
793
  catch (err) {
@@ -756,7 +812,11 @@ export class Core extends ReadyResource {
756
812
  if (mesh) {
757
813
  this._bringMeshOnline(mesh);
758
814
  }
759
- 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
+ });
760
820
  }
761
821
  catch (err) {
762
822
  if (this.closing) {
@@ -1126,6 +1186,8 @@ export class Core extends ReadyResource {
1126
1186
  this._offlineBoot.destroy(new CoreError('CORE_CLOSING'));
1127
1187
  this.onlineBoot.destroy(new CoreError('CORE_CLOSING'));
1128
1188
  this._signalClose();
1189
+ this._processor.close();
1190
+ await this._upgradeBoot;
1129
1191
  this._notifyStatus();
1130
1192
  await this._stopFileGc();
1131
1193
  this._stopTopicRetry();
@@ -1137,6 +1199,7 @@ export class Core extends ReadyResource {
1137
1199
  }
1138
1200
  this._clients.clear();
1139
1201
  await this._stopIndexing();
1202
+ await this._closeOpeningMeshes();
1140
1203
  await this._blobServer?.close();
1141
1204
  this._blobServer = null;
1142
1205
  await this._chatMoves?.close();
@@ -1145,6 +1208,7 @@ export class Core extends ReadyResource {
1145
1208
  this._unwireMesh(entry);
1146
1209
  await entry.mesh.close();
1147
1210
  }
1211
+ await this._processor.settled();
1148
1212
  await this._discardCandidateMesh();
1149
1213
  await this._departures?.close();
1150
1214
  this._departures = null;
@@ -1165,6 +1229,18 @@ export class Core extends ReadyResource {
1165
1229
  await this._closeStore();
1166
1230
  this._log?.info('lifecycle', 'core closed');
1167
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
+ }
1168
1244
  async _closeStore() {
1169
1245
  await this.store?.close();
1170
1246
  if (this._ownsStore) {
@@ -1232,17 +1308,25 @@ export class Core extends ReadyResource {
1232
1308
  throw halted;
1233
1309
  }
1234
1310
  await this._adoptAccount(mesh);
1311
+ const late = mesh.interrupted;
1312
+ if (late) {
1313
+ this._withdrawFromMesh(mesh, late);
1314
+ throw late;
1315
+ }
1235
1316
  return mesh;
1236
1317
  }
1237
1318
  async _openMesh(opts = {}) {
1238
1319
  const session = await this.local.resolveSession(this._requireDeviceKeyPair().publicKey, opts);
1239
1320
  return this._openMeshSession(session, opts);
1240
1321
  }
1241
- async _openMeshSession(session, { blindPeerKeys, account, initial, sharing, workspace, project } = {}) {
1322
+ async _openMeshSession(session, { blindPeerKeys, account, initial, sharing, workspace, project, boot } = {}) {
1323
+ this._refuseClosing();
1242
1324
  const deviceId = this._requireDeviceKeyPair().publicKey;
1243
1325
  const mesh = this._newMesh(session, session.role ?? (account ? 'account' : 'project'), blindPeerKeys);
1326
+ this._openingMeshSessions.add(mesh);
1244
1327
  try {
1245
1328
  await mesh.ready();
1329
+ this._refuseClosing();
1246
1330
  if (session.baseKey && mesh.localLength === 0) {
1247
1331
  this._requireLocalDevice();
1248
1332
  await this.local.recordMesh(mesh.key, session, { deviceId, account });
@@ -1260,16 +1344,21 @@ export class Core extends ReadyResource {
1260
1344
  initial,
1261
1345
  sharing,
1262
1346
  workspace,
1263
- project
1347
+ project,
1348
+ boot
1264
1349
  });
1265
1350
  }
1351
+ this._refuseClosing();
1266
1352
  }
1267
1353
  catch (err) {
1268
1354
  if (mesh.opened && mesh.interrupted) {
1269
1355
  this._markHalted(mesh);
1270
1356
  }
1271
1357
  await mesh.close().catch(noop);
1272
- throw err;
1358
+ throw this.closing ? new CoreError('CORE_CLOSING', ['Core']) : err;
1359
+ }
1360
+ finally {
1361
+ this._openingMeshSessions.delete(mesh);
1273
1362
  }
1274
1363
  this._poolEntry(mesh);
1275
1364
  return mesh;
@@ -1282,17 +1371,25 @@ export class Core extends ReadyResource {
1282
1371
  deviceId: this._requireDeviceKeyPair().publicKey,
1283
1372
  writerSeed: session.writerSeed,
1284
1373
  logger: this._log,
1374
+ onerror: this._onerror,
1285
1375
  blindPeerKeys: this._resolveBlindPeerKeys(blindPeerKeys),
1286
- trustedDevice: (deviceId) => this._isOurMemberDevice(deviceId),
1287
1376
  wakeup: this._wakeup,
1288
1377
  attachmentUrl: (attachment) => this._linkAttachment(attachment, mesh),
1289
1378
  fileIndex: this._fileIndexFor(() => mesh),
1379
+ history: this._upgrade.history(() => mesh),
1290
1380
  pendingUpdates: this._pendingUpdatesFor(() => mesh),
1291
- bootFrom: session.bootFrom
1381
+ sidebarChats: (limit) => this.local.listChatOrder(mesh.key, { limit }),
1382
+ bootFrom: session.bootFrom,
1383
+ joinHeads: session.joinHeads
1292
1384
  });
1293
1385
  return mesh;
1294
1386
  }
1295
- 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
+ }
1296
1393
  await raceResource(mesh.autobee, (async () => {
1297
1394
  const birth = sharing
1298
1395
  ? await this._accountBirthMember(mesh, session)
@@ -1329,6 +1426,17 @@ export class Core extends ReadyResource {
1329
1426
  this._markHalted(mesh);
1330
1427
  }
1331
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
+ });
1332
1440
  });
1333
1441
  }
1334
1442
  _joinProjectWhenListed(mesh, deviceId) {
@@ -1428,9 +1536,7 @@ export class Core extends ReadyResource {
1428
1536
  throw new CoreError('RECOVERY_PHRASE_NOT_OURS');
1429
1537
  }
1430
1538
  if (account) {
1431
- const member = await account.view
1432
- .get(MEMBERS, { id: identity.identityPublicKey })
1433
- .catch(noop);
1539
+ const member = await readView(account, (snap) => snap.get(MEMBERS, { id: identity.identityPublicKey }));
1434
1540
  if (!member) {
1435
1541
  throw new CoreError('RECOVERY_PHRASE_NOT_A_MEMBER');
1436
1542
  }
@@ -1825,14 +1931,7 @@ export class Core extends ReadyResource {
1825
1931
  if (!this._isPooled(mesh)) {
1826
1932
  return;
1827
1933
  }
1828
- const snap = mesh.view.snapshot();
1829
- let existing;
1830
- try {
1831
- existing = await snap.get(DEVICES, { id: deviceId });
1832
- }
1833
- finally {
1834
- await snap.close();
1835
- }
1934
+ const existing = await mesh.view.get(DEVICES, { id: deviceId });
1836
1935
  if (existing?.platform === local.platform &&
1837
1936
  existing.capabilityProfile === local.capabilityProfile &&
1838
1937
  existing.recommendedModelName === local.recommendedModelName) {
@@ -1859,14 +1958,7 @@ export class Core extends ReadyResource {
1859
1958
  if (!this._isPooled(mesh) || !this._isAccount(mesh)) {
1860
1959
  return;
1861
1960
  }
1862
- const snap = mesh.view.snapshot();
1863
- let existing;
1864
- try {
1865
- existing = await snap.get(DEVICES, { id: deviceId });
1866
- }
1867
- finally {
1868
- await snap.close();
1869
- }
1961
+ const existing = await mesh.view.get(DEVICES, { id: deviceId });
1870
1962
  if (existing?.appVersion === app.version &&
1871
1963
  (existing.appBuild || undefined) === app.build &&
1872
1964
  existing.appChannel === app.channel) {
@@ -1981,36 +2073,51 @@ export class Core extends ReadyResource {
1981
2073
  async _knownMember(exclude) {
1982
2074
  const account = this._accountMesh();
1983
2075
  if (account && account !== exclude && account.ourMemberId) {
1984
- const known = await this._memberIn(account).catch(noop);
2076
+ const known = await this._memberIn(account);
1985
2077
  if (known) {
1986
2078
  return known;
1987
2079
  }
1988
2080
  }
2081
+ let unread = null;
1989
2082
  for (const { mesh } of this._meshes.values()) {
1990
2083
  if (mesh === exclude ||
1991
2084
  !mesh.ourMemberId ||
1992
2085
  this._isHidden(this._meshIdOf(mesh), mesh.workspaceKey)) {
1993
2086
  continue;
1994
2087
  }
1995
- const known = await this._memberIn(mesh).catch(noop);
2088
+ const known = await this._memberIn(mesh).catch((err) => {
2089
+ unread = err;
2090
+ return null;
2091
+ });
1996
2092
  if (known) {
1997
2093
  return known;
1998
2094
  }
1999
2095
  }
2000
- return this._recordedMember();
2096
+ const recorded = await this._recordedMember();
2097
+ if (!recorded && unread) {
2098
+ throw unread;
2099
+ }
2100
+ return recorded;
2001
2101
  }
2002
- async _memberIn(mesh) {
2003
- const own = await mesh.view.get(DEVICES, {
2004
- 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
+ };
2005
2120
  });
2006
- if (!own?.memberId || !own.memberProof) {
2007
- return null;
2008
- }
2009
- const member = await mesh.view.get(MEMBERS, { id: own.memberId });
2010
- if (!member) {
2011
- return null;
2012
- }
2013
- return { id: member.id, name: member.name, updatedAt: member.updatedAt, proof: own.memberProof };
2014
2121
  }
2015
2122
  async _recordedMember() {
2016
2123
  const account = await this._storedAccountKey();
@@ -2040,12 +2147,22 @@ export class Core extends ReadyResource {
2040
2147
  }
2041
2148
  };
2042
2149
  }
2043
- async _recordMemberProof(mesh, membership) {
2150
+ async _recordMemberProof(mesh) {
2151
+ const membership = mesh.ourMembership;
2152
+ if (!membership || !this._isPooled(mesh)) {
2153
+ return;
2154
+ }
2044
2155
  try {
2156
+ const kept = await this.local.readMemberProof(mesh.key);
2157
+ if (kept && kept.attestedAt >= membership.attestedAt) {
2158
+ return;
2159
+ }
2045
2160
  await this.local.recordMemberProof({ meshKey: mesh.key, ...membership });
2046
2161
  }
2047
2162
  catch (err) {
2048
- 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
+ }
2049
2166
  }
2050
2167
  }
2051
2168
  async _ensureNotRevoked(mesh) {
@@ -2134,10 +2251,14 @@ export class Core extends ReadyResource {
2134
2251
  this._haltedMeshes.delete(b4a.toHex(key));
2135
2252
  this._notifyStatus();
2136
2253
  }
2254
+ await fs.promises.rm(this._textDir(key), { recursive: true, force: true });
2137
2255
  if (cores) {
2138
2256
  await this._finishPurge(key);
2139
2257
  }
2140
2258
  }
2259
+ _textDir(key) {
2260
+ return path.join(path.dirname(this._storagePath), 'knowledge-text', b4a.toHex(key));
2261
+ }
2141
2262
  async _stopMeshServices(entry) {
2142
2263
  if (!entry)
2143
2264
  return;
@@ -2150,7 +2271,7 @@ export class Core extends ReadyResource {
2150
2271
  return;
2151
2272
  }
2152
2273
  entry.processingFiles = null;
2153
- if (!(await files.stopProcessing())) {
2274
+ if (!(await files.stopProcessing()) || entry.vectorIndex instanceof PluginVectorIndex) {
2154
2275
  entry.vectorIndex = null;
2155
2276
  }
2156
2277
  }
@@ -2352,31 +2473,35 @@ export class Core extends ReadyResource {
2352
2473
  this._retiredAccount !== this._meshIdOf(account)) {
2353
2474
  return;
2354
2475
  }
2476
+ const minted = this._ownWorkspaceId;
2355
2477
  this._retiredAccount = null;
2356
2478
  this._retiredMeshes.clear();
2357
2479
  await this.local.restoreMesh(account.key);
2480
+ if (fresh && fresh !== account) {
2481
+ await this.local.archiveMesh(fresh.key, { homeTo: account.key });
2482
+ }
2358
2483
  await this._restorePublicWriters(account);
2359
2484
  await this._adoptAccount(account);
2360
2485
  if (fresh && fresh !== account) {
2361
- await this._dropOwnMint(fresh);
2362
- await this._teardownMesh(fresh, { purge: true });
2363
- }
2364
- }
2365
- async _dropOwnMint(fresh) {
2366
- const minted = new Set((await this.local.listMeshes()).filter((row) => row.initial).map((row) => b4a.toHex(row.key)));
2367
- for (const joined of await fresh.view.find(JOINED_MESHES).toArray()) {
2368
- const id = b4a.toHex(joined.meshKey);
2369
- if (!minted.has(id)) {
2370
- continue;
2371
- }
2372
- const own = this._publicMesh(id);
2373
- if (own) {
2374
- 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);
2375
2501
  }
2376
- await this._teardownMesh(this._pooled(id) ?? (await this._openPooledMesh(id, 'leave')), {
2377
- purge: true
2378
- });
2502
+ this._retiredMeshes.add(b4a.toHex(key));
2379
2503
  }
2504
+ this._notifyStatus();
2380
2505
  }
2381
2506
  async _replaceRevokedAccount(mesh) {
2382
2507
  this._minting = true;
@@ -2440,9 +2565,7 @@ export class Core extends ReadyResource {
2440
2565
  }
2441
2566
  this._kickedProjects.set(b4a.toHex(row.key), null);
2442
2567
  const project = await this._heldMesh(row.key, 'leave');
2443
- await (project
2444
- ? this._departRevokedMesh(project, { purge: true })
2445
- : this._teardownMesh(row.key));
2568
+ await (project ? this._departRevokedMesh(project) : this._teardownMesh(row.key));
2446
2569
  }
2447
2570
  }
2448
2571
  async _departRevokedMesh(mesh, opts = {}) {
@@ -2540,7 +2663,7 @@ export class Core extends ReadyResource {
2540
2663
  let pooled = this._pooled(meshId);
2541
2664
  if (this._isHidden(meshId, pooled?.workspaceKey)) {
2542
2665
  if (pooled) {
2543
- await this._teardownMesh(pooled, { depart: true, purge: true });
2666
+ await this._teardownMesh(pooled, { depart: true });
2544
2667
  }
2545
2668
  else {
2546
2669
  await this._dropKickedCopy(meshId);
@@ -2612,7 +2735,7 @@ export class Core extends ReadyResource {
2612
2735
  return false;
2613
2736
  }
2614
2737
  const account = await this._openPooledMesh(b4a.toHex(invite.baseKey));
2615
- if (!(await this._keepsWriter(account))) {
2738
+ if (!(await this._keepsJoin(account, invite))) {
2616
2739
  return false;
2617
2740
  }
2618
2741
  if (!this._isAccount(account)) {
@@ -2664,18 +2787,25 @@ export class Core extends ReadyResource {
2664
2787
  async _reopenRecordedMesh(invite) {
2665
2788
  if (!(await this.local.readMeshSession(invite.baseKey)))
2666
2789
  return false;
2667
- return this._keepsWriter(await this._openPooledMesh(b4a.toHex(invite.baseKey)));
2790
+ return this._keepsJoin(await this._openPooledMesh(b4a.toHex(invite.baseKey)), invite);
2668
2791
  }
2669
- async _keepsWriter(mesh) {
2670
- if (mesh.writable)
2671
- return true;
2672
- if (await mesh.ownWriterRefused()) {
2673
- 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
+ }
2674
2803
  }
2675
- const own = await mesh.view.get(DEVICES, {
2676
- id: this._requireDeviceKeyPair().publicKey
2677
- });
2678
- 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;
2679
2809
  }
2680
2810
  async _runJoinMesh(invite, claim) {
2681
2811
  const deviceId = this._requireDeviceKeyPair().publicKey;
@@ -2703,9 +2833,18 @@ export class Core extends ReadyResource {
2703
2833
  claim.reject(new CoreError('MESH_BELOW_FLOOR'));
2704
2834
  }
2705
2835
  }
2706
- _candidateSession({ seed, baseKey, workspace, fastForward }) {
2836
+ _candidateSession({ seed, baseKey, workspace, memberInvite, fastForward, inviterHeads }) {
2707
2837
  const role = workspace ? 'workspace' : undefined;
2708
- 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
+ };
2709
2848
  }
2710
2849
  async _joinCandidate(invite, source, deviceId) {
2711
2850
  const session = this._candidateSession(invite);
@@ -2721,14 +2860,15 @@ export class Core extends ReadyResource {
2721
2860
  if (this._joinCancellation) {
2722
2861
  this._joinCancellation.accepting = false;
2723
2862
  }
2863
+ let member;
2724
2864
  try {
2865
+ member = await this._candidateBirthMember(candidate, invite);
2725
2866
  await this.local.recordMesh(candidate.key, session, { deviceId });
2726
2867
  }
2727
2868
  catch (error) {
2728
2869
  await this._discardCandidateMesh();
2729
2870
  throw error;
2730
2871
  }
2731
- const member = await this._candidateBirthMember(candidate, invite);
2732
2872
  const appended = this._appendJoinLater(candidate, async () => member);
2733
2873
  return this._landJoinedMesh(candidate, {
2734
2874
  source,
@@ -2753,13 +2893,20 @@ export class Core extends ReadyResource {
2753
2893
  }
2754
2894
  if (memberInvite) {
2755
2895
  this._candidateMesh = null;
2756
- if (!candidate.writable) {
2757
- 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
+ }
2758
2906
  }
2759
- this._wireMesh(this._poolEntry(candidate));
2760
- this._bringMeshOnline(candidate);
2761
- if (candidate.writable) {
2762
- await this.local.markAwaitingAdmission(candidate.key, { awaitingAdmission: false });
2907
+ catch (err) {
2908
+ this._recordingJoins.delete(candidate);
2909
+ throw err;
2763
2910
  }
2764
2911
  this._notifyStatus();
2765
2912
  }
@@ -2815,333 +2962,31 @@ export class Core extends ReadyResource {
2815
2962
  }
2816
2963
  async _recordJoinedMesh(mesh, appended) {
2817
2964
  try {
2965
+ await this._registerMesh(mesh.key, { shared: false });
2818
2966
  await appended;
2819
2967
  const kind = await meshKind(mesh.view);
2820
2968
  const workspace = kind === 'workspace';
2821
2969
  if (kind && workspace !== (mesh.role === 'workspace')) {
2822
- 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));
2823
2974
  }
2824
- await this._registerMesh(mesh.key, { shared: false, workspace });
2825
2975
  }
2826
2976
  catch (error) {
2827
2977
  this._log?.warn(['p2p', 'mesh'], 'recording the joined mesh failed', error);
2828
2978
  }
2829
- }
2830
- async _followUpgrade(mesh) {
2831
- const meshId = this._meshIdOf(mesh);
2832
- if (this.closing || this._upgrading.has(meshId)) {
2833
- return;
2834
- }
2835
- this._upgrading.add(meshId);
2836
- try {
2837
- let kind = await meshKind(mesh.view);
2838
- if (kind === 'shared' && (await this._upgradable(mesh))) {
2839
- await mesh.dispatch('upgrade-to-workspace', { upgradedAt: Date.now() });
2840
- kind = await meshKind(mesh.view);
2841
- }
2842
- if (kind === 'workspace' && mesh.writable && this._isPooled(mesh)) {
2843
- await this.local.set(upgradeCheckedKey(mesh.key), GROUPS_PENDING);
2844
- await this._markFoldDue(mesh.key);
2845
- await this._reopenInRole(mesh, 'workspace');
2846
- await this._markJoinedWorkspace(mesh.key);
2847
- }
2848
- else if (kind !== 'project' && (kind !== 'shared' || (await bornUnderRules(mesh.view)))) {
2849
- await this._markUpgradeChecked(mesh.key);
2850
- }
2851
- }
2852
- catch (err) {
2853
- if (!this.closing) {
2854
- this._log?.warn(['p2p', 'mesh'], 'upgrading a shared mesh to a workspace failed', err);
2855
- }
2856
- }
2857
2979
  finally {
2858
- this._upgrading.delete(meshId);
2980
+ this._recordingJoins.delete(mesh);
2859
2981
  }
2860
2982
  }
2861
- _followGroups(workspace) {
2862
- if (this.closing || !workspace.writable) {
2863
- return;
2864
- }
2865
- if (this._groupPasses.has(workspace)) {
2866
- this._groupPassesDue.add(workspace);
2867
- return;
2868
- }
2869
- this._groupPasses.add(workspace);
2870
- void this._passGroups(workspace)
2871
- .catch((err) => {
2872
- if (!this.closing && this._isPooled(workspace)) {
2873
- this._log?.warn(['p2p', 'mesh'], 'moving chat-groups into projects failed', err);
2874
- }
2875
- })
2876
- .finally(() => {
2877
- this._groupPasses.delete(workspace);
2878
- if (this._groupPassesDue.delete(workspace)) {
2879
- this._followGroups(workspace);
2880
- }
2881
- });
2882
- }
2883
- async _passGroups(workspace) {
2884
- const checked = await this.local.get(upgradeCheckedKey(workspace.key));
2885
- if (checked === true) {
2886
- return this._passAgents(workspace);
2887
- }
2888
- if (checked !== GROUPS_PENDING) {
2889
- return;
2890
- }
2891
- const ours = workspace.ourMemberId ?? (await workspace.refreshOurMember())?.memberId;
2892
- if (!ours || !(await isWorkspace(workspace.view))) {
2893
- return;
2894
- }
2895
- const buckets = await this._groupBuckets(workspace);
2896
- const rows = await projectsByGroup(workspace.view);
2897
- const passes = await Promise.allSettled(buckets.map((bucket) => this._passGroup(workspace, ours, { bucket, row: rows.get(bucket.groupId) ?? null })));
2898
- const settled = [];
2899
- for (const pass of passes) {
2900
- if (pass.status === 'rejected') {
2901
- throw pass.reason;
2902
- }
2903
- settled.push(pass.value);
2904
- }
2905
- if (settled.every((state) => state === 'done') && this._isPooled(workspace)) {
2906
- await this.local.set(upgradeCheckedKey(workspace.key), true);
2907
- }
2908
- if (!settled.includes('pending')) {
2909
- await this._passAgents(workspace);
2910
- }
2911
- }
2912
- async _groupBuckets(workspace) {
2913
- const named = workspace.name?.name?.trim() || 'Workspace';
2914
- const groups = await liveGroups(workspace.view);
2915
- const ids = [...groups.map((group) => group.id), UNGROUPED];
2916
- const records = await Promise.all(ids.map((id) => this._groupRecord(workspace, id)));
2917
- if (!records.every((record) => record.copied || record.done)) {
2918
- return this._scanBuckets(workspace, named);
2919
- }
2920
- return ids.map((groupId, idx) => ({
2921
- groupId,
2922
- name: groups[idx]?.name ?? named,
2923
- chats: new Set()
2924
- }));
2925
- }
2926
- async _markFoldDue(workspaceKey) {
2927
- const key = agentsFoldKey(workspaceKey);
2928
- if ((await this.local.get(key)) !== true) {
2929
- await this.local.set(key, FOLD_PENDING);
2930
- }
2931
- }
2932
- async _passAgents(workspace) {
2933
- if ((await this.local.get(agentsFoldKey(workspace.key))) !== FOLD_PENDING) {
2934
- return;
2935
- }
2936
- const copies = await ownCopies(workspace, this._requireDeviceKeyPair().publicKey);
2937
- if (copies.length > 0) {
2938
- const account = await this._writableAccount();
2939
- const projects = await this._heldProjects(workspace);
2940
- if (!account || !projects) {
2941
- return;
2942
- }
2943
- await foldAgents({ account, workspace, projects, picks: this._agentPicks() }, copies);
2944
- }
2945
- if (this._isPooled(workspace)) {
2946
- await this.local.set(agentsFoldKey(workspace.key), true);
2947
- }
2948
- }
2949
- async _heldProjects(workspace) {
2950
- const ours = workspace.ourMemberId;
2951
- const held = [];
2952
- for (const row of await workspace.view.find(PROJECTS).toArray()) {
2953
- if (row.deletedAt || !ours || !listedProject(row, ours))
2954
- continue;
2955
- const project = await this._heldMesh(row.id);
2956
- if (!project?.writable) {
2957
- return null;
2958
- }
2959
- held.push(project);
2960
- }
2961
- return held;
2962
- }
2963
- async _groupRecord(workspace, groupId) {
2964
- const stored = await this.local.get(groupRecordKey(workspace.key, groupId));
2965
- return isGroupRecord(stored) ? stored : {};
2966
- }
2967
- async _scanBuckets(workspace, named) {
2968
- const buckets = await groupBuckets(workspace.view, named);
2969
- const ungrouped = buckets.some((bucket) => bucket.groupId === UNGROUPED);
2970
- if (!ungrouped && !(await this._groupRecord(workspace, UNGROUPED)).key) {
2971
- await this.local.set(groupRecordKey(workspace.key, UNGROUPED), { done: true });
2972
- }
2973
- return buckets;
2974
- }
2975
- async _passGroup(workspace, ours, { bucket, row: listed }) {
2976
- const recordKey = groupRecordKey(workspace.key, bucket.groupId);
2977
- let record = await this._groupRecord(workspace, bucket.groupId);
2978
- if (record.done) {
2979
- return 'done';
2980
- }
2981
- let row = listed;
2982
- if (!row) {
2983
- record = await this._foundGroupProject(workspace, bucket, { recordKey, record });
2984
- row = await projectOfGroup(workspace.view, bucket.groupId);
2985
- }
2986
- const live = !!row && !row.deletedAt;
2987
- if (row && live && record.key === b4a.toHex(row.id)) {
2988
- await this._listNewMembers(workspace, { row, recordKey, record });
2989
- return 'won';
2990
- }
2991
- if (row && live && !(await this._mergeGroupInto(workspace, ours, { row, bucket, record }))) {
2992
- return 'pending';
2993
- }
2994
- if (record.key) {
2995
- await this._purgeProject(b4a.from(record.key, 'hex'));
2996
- }
2997
- await this.local.set(recordKey, { ...record, done: true });
2998
- return 'done';
2999
- }
3000
- async _foundGroupProject(workspace, bucket, { recordKey, record }) {
3001
- const held = record.key ? await this._heldMesh(b4a.from(record.key, 'hex')) : null;
3002
- const name = projectName(bucket.name.trim() || 'Project');
3003
- const project = held ?? (await this._openNewProject(workspace, { name, upgraded: true }));
3004
- let next = { ...record, key: this._meshIdOf(project) };
3005
- await this.local.set(recordKey, next);
3006
- await this._addWorkspaceMembers(workspace, project);
3007
- if (!next.copied) {
3008
- await this._copyGroup(workspace, project, bucket);
3009
- next = { ...next, copied: true };
3010
- await this.local.set(recordKey, next);
3011
- }
3012
- const now = Date.now();
3013
- await this._listProject(workspace, project, {
3014
- createdAt: now,
3015
- updatedAt: now,
3016
- groupId: bucket.groupId
3017
- });
3018
- this._notifyStatus();
3019
- return next;
3020
- }
3021
- async _mergeGroupInto(workspace, ours, { row, bucket, record }) {
3022
- const winner = listsMember(row, ours) ? await this._winnerOf(workspace, row) : null;
3023
- if (!winner) {
3024
- return false;
3025
- }
3026
- await this._addWorkspaceMembers(workspace, winner);
3027
- await this._listWorkspaceMembers(workspace, row);
3028
- const own = record.key ? await this._heldMesh(b4a.from(record.key, 'hex')) : null;
3029
- if (own) {
3030
- await copyContent({
3031
- from: own,
3032
- into: winner,
3033
- deviceId: this._requireDeviceKeyPair().publicKey
3034
- });
3035
- }
3036
- await this._copyGroup(workspace, winner, bucket);
3037
- this._log?.info(['p2p', 'mesh'], 'merged a chat-group into the project that won it', {
3038
- project: b4a.toHex(row.id).slice(0, 8)
3039
- });
3040
- return true;
3041
- }
3042
- async _winnerOf(workspace, row) {
3043
- const projectId = b4a.toHex(row.id);
3044
- if (!this._pooled(projectId) && !(await this.local.readMeshSession(row.id))) {
3045
- await this._followProjects(workspace);
3046
- await this._following.get(projectId);
3047
- }
3048
- const winner = this._pooled(projectId) ?? (await this._heldMesh(row.id));
3049
- await winner?.waitForWritable(WINNER_ADMISSION_WAIT_MS);
3050
- return winner?.writable ? winner : null;
3051
- }
3052
- async _copyGroup(workspace, project, { chats }) {
3053
- const deviceId = this._requireDeviceKeyPair().publicKey;
3054
- await copyContent({ from: workspace, into: project, deviceId, chats, endsOpenRuns: true });
3055
- }
3056
- async _addWorkspaceMembers(workspace, project) {
3057
- for (const memberId of await this._workspaceMembers(workspace)) {
3058
- if (!(await inProject(project.view, memberId))) {
3059
- await project.dispatch('put-project-member', { memberId });
3060
- }
3061
- }
3062
- }
3063
- async _listNewMembers(workspace, { row, recordKey, record }) {
3064
- const listed = new Set(record.listedMembers ?? []);
3065
- const members = await this._workspaceMembers(workspace);
3066
- const added = members.filter((id) => !listed.has(b4a.toHex(id)));
3067
- const project = added.length > 0 ? await this._heldMesh(row.id) : null;
3068
- for (const memberId of added) {
3069
- if (project && !(await inProject(project.view, memberId))) {
3070
- await project.dispatch('put-project-member', { memberId });
3071
- }
3072
- if (!listsMember(row, memberId)) {
3073
- await workspace.dispatch('set-project-listing', { projectId: row.id, memberId });
3074
- }
3075
- }
3076
- if (added.length > 0 && this._isPooled(workspace)) {
3077
- const listedMembers = [...listed, ...added.map((id) => b4a.toHex(id))];
3078
- await this.local.set(recordKey, { ...record, listedMembers });
3079
- }
3080
- }
3081
- async _listWorkspaceMembers(workspace, row) {
3082
- for (const memberId of await this._workspaceMembers(workspace)) {
3083
- if (!listsMember(row, memberId)) {
3084
- await workspace.dispatch('set-project-listing', { projectId: row.id, memberId });
3085
- }
3086
- }
3087
- }
3088
- async _workspaceMembers(workspace) {
3089
- const live = await liveMemberIds(workspace.view);
3090
- return (await upgradeMembers(workspace.view)).filter((id) => live.has(b4a.toHex(id)));
3091
- }
3092
- async _openUpgradable() {
3093
- const account = await this._storedAccountKey();
3094
- for (const row of await this.local.listMeshes()) {
3095
- const skip = row.workspaceKey || (account && b4a.equals(row.key, account));
3096
- const role = recordedRole(row);
3097
- if (this.closing || skip || (role !== 'project' && role !== 'workspace')) {
3098
- continue;
3099
- }
3100
- const checked = await this.local.get(upgradeCheckedKey(row.key));
3101
- const due = role === 'workspace'
3102
- ? checked === GROUPS_PENDING ||
3103
- (await this.local.get(agentsFoldKey(row.key))) === FOLD_PENDING
3104
- : checked !== true;
3105
- if (!due) {
3106
- continue;
3107
- }
3108
- await this._openPooledMesh(b4a.toHex(row.key)).catch((err) => {
3109
- this._log?.warn(['p2p', 'mesh'], 'opening a shared mesh to upgrade it failed', err);
3110
- });
3111
- }
3112
- }
3113
- async _markUpgradeChecked(meshKey) {
3114
- const key = upgradeCheckedKey(meshKey);
3115
- if ((await this.local.get(key)) !== true) {
3116
- await this.local.set(key, true);
3117
- }
3118
- }
3119
- async _upgradable(mesh) {
3120
- if (!mesh.writable || (await rulesVersion(mesh.view)) < WORKSPACES_RULES) {
3121
- return false;
3122
- }
3123
- return !(await bornUnderRules(mesh.view));
3124
- }
3125
- async _markJoinedWorkspace(meshKey) {
3126
- const registry = await this._writableAccount();
3127
- const joined = await registry?.view.get(JOINED_MESHES, { meshKey });
3128
- if (!registry || !joined || joined.workspace || joined.leftAt) {
3129
- return;
3130
- }
3131
- await registry.dispatch('put-joined-mesh', {
3132
- room: {
3133
- meshKey,
3134
- memberId: joined.memberId,
3135
- joinedAt: joined.joinedAt,
3136
- label: joined.label,
3137
- labeledAt: joined.labeledAt,
3138
- shared: joined.shared,
3139
- sharedAt: joined.sharedAt,
3140
- workspace: true
3141
- }
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
3142
2989
  });
3143
- }
3144
- async _reopenInRole(mesh, role) {
3145
2990
  await this.local.setMeshRole(mesh.key, role);
3146
2991
  await this._retirePooledMesh(mesh);
3147
2992
  const reopened = await this._openPooledMesh(this._meshIdOf(mesh));
@@ -3207,6 +3052,7 @@ export class Core extends ReadyResource {
3207
3052
  continue;
3208
3053
  }
3209
3054
  const pooled = this._pooled(b4a.toHex(row.key));
3055
+ const kind = pooled ? await meshKind(pooled.view) : null;
3210
3056
  batch.dispatch('put-joined-mesh', {
3211
3057
  room: {
3212
3058
  meshKey: row.key,
@@ -3214,7 +3060,7 @@ export class Core extends ReadyResource {
3214
3060
  joinedAt: now,
3215
3061
  shared: false,
3216
3062
  sharedAt: 0,
3217
- workspace: row.role === 'workspace' || (!!pooled && (await isWorkspace(pooled.view)))
3063
+ workspace: kind === null ? row.role === 'workspace' : kind === 'workspace'
3218
3064
  }
3219
3065
  });
3220
3066
  await this.local.markJoinedUnder(row.key, now);
@@ -3292,13 +3138,15 @@ export class Core extends ReadyResource {
3292
3138
  deviceId: this._requireDeviceKeyPair().publicKey,
3293
3139
  writerSeed: session.writerSeed,
3294
3140
  logger: this._log,
3141
+ onerror: this._onerror,
3295
3142
  blindPeerKeys: this._resolveBlindPeerKeys(invite.blindPeers),
3296
3143
  bootFrom: session.bootFrom,
3297
- trustedDevice: (deviceId) => this._isOurMemberDevice(deviceId),
3144
+ joinHeads: session.joinHeads,
3298
3145
  wakeup: this._wakeup,
3299
3146
  attachmentUrl: (attachment) => this._linkAttachment(attachment, candidate),
3300
3147
  fileIndex: this._fileIndexFor(() => candidate),
3301
- pendingUpdates: this._pendingUpdatesFor(() => candidate)
3148
+ pendingUpdates: this._pendingUpdatesFor(() => candidate),
3149
+ sidebarChats: (limit) => this.local.listChatOrder(candidate.key, { limit })
3302
3150
  });
3303
3151
  this._candidateMesh = candidate;
3304
3152
  if (this._swarm) {
@@ -3382,17 +3230,18 @@ export class Core extends ReadyResource {
3382
3230
  }
3383
3231
  _onSharedWritable(mesh) {
3384
3232
  if (mesh.role === 'project') {
3385
- void this._followUpgrade(mesh);
3386
- void this._followSharedAgents();
3233
+ this._processor.enqueue(mesh, [MESH_WRITER.upgrade, MESH_WRITER.sharedAgents]);
3387
3234
  void this._workspaceOf(mesh)
3388
- .then((workspace) => workspace && this._followGroups(workspace))
3235
+ .then((workspace) => workspace && this._upgrade.follow(workspace))
3389
3236
  .catch(noop);
3390
3237
  }
3391
3238
  else if (mesh.role === 'workspace') {
3392
- this._followGroups(mesh);
3393
- void this._followSharedAgents();
3394
- this._shareMemberKey(mesh);
3395
- 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
+ ]);
3396
3245
  }
3397
3246
  }
3398
3247
  _onMeshWritable(mesh) {
@@ -3406,15 +3255,17 @@ export class Core extends ReadyResource {
3406
3255
  void this._followPublicMesh(mesh);
3407
3256
  void this._followSkillSecrets();
3408
3257
  }
3409
- if (this._isOwnPublic(mesh)) {
3258
+ const account = this._accountMesh();
3259
+ if (this._isOwnPublic(mesh) && account) {
3410
3260
  void this._followAgentCopies();
3261
+ this._upgrade.copyAccount(account);
3411
3262
  }
3412
3263
  this._notifyStatus();
3413
3264
  }
3414
3265
  _onMeshDead(mesh, err) {
3415
3266
  if (this.closing || !this._isPooled(mesh))
3416
3267
  return;
3417
- this._log?.error(['p2p', 'mesh'], 'mesh autobee died - retiring it', err);
3268
+ this._log?.info(['p2p', 'mesh'], 'mesh autobee died - retiring it');
3418
3269
  if (this._isAccount(mesh)) {
3419
3270
  this._statusError = statusErrorMessage(MESH_DIED, err);
3420
3271
  }
@@ -3448,6 +3299,9 @@ export class Core extends ReadyResource {
3448
3299
  entry.handlers = handlers;
3449
3300
  mesh.on('kick-dirty', () => this._reviewKick(mesh).catch((err) => this._log?.warn(['p2p', 'mesh'], 'reviewing a kick failed', err)));
3450
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 }));
3451
3305
  if (mesh.writable) {
3452
3306
  this._onMeshSeedDue(mesh);
3453
3307
  this._onProjectWritable(mesh);
@@ -3469,20 +3323,21 @@ export class Core extends ReadyResource {
3469
3323
  this._dropUnpairedSiblings(mesh).catch(warn('unpairing a kicked device'));
3470
3324
  });
3471
3325
  mesh.on('agents-dirty', () => {
3326
+ const due = [];
3472
3327
  if (mesh.role === 'workspace' || mesh.role === 'project' || this._isOwnPublic(mesh)) {
3473
- void this._followSharedAgents();
3328
+ due.push(MESH_WRITER.sharedAgents);
3474
3329
  }
3475
3330
  if (mesh.role === 'workspace') {
3476
- void this._sealProjectKeys();
3477
- void this._followHosting();
3331
+ due.push(MESH_WRITER.projectKeys, MESH_WRITER.hosting);
3478
3332
  }
3479
3333
  if (this._isAccount(mesh)) {
3480
3334
  void this._followAgentCopies();
3481
- void this._followSharedAgents();
3335
+ due.push(MESH_WRITER.sharedAgents);
3482
3336
  for (const listener of [...this._agentListeners]) {
3483
3337
  listener();
3484
3338
  }
3485
3339
  }
3340
+ this._processor.enqueue(mesh, due);
3486
3341
  });
3487
3342
  mesh.on('members-dirty', () => {
3488
3343
  if (!this._isAccount(mesh))
@@ -3490,6 +3345,8 @@ export class Core extends ReadyResource {
3490
3345
  void this._followAccountName(mesh);
3491
3346
  });
3492
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));
3493
3350
  mesh.on('joined', () => {
3494
3351
  if (entry.meshesPending) {
3495
3352
  void this._followJoinedMeshes(mesh);
@@ -3503,16 +3360,14 @@ export class Core extends ReadyResource {
3503
3360
  void this._keepName(mesh);
3504
3361
  this._notifyStatus();
3505
3362
  });
3506
- mesh.on('membership', (membership) => {
3363
+ mesh.on('membership', () => {
3507
3364
  if (this._isHidden(this._meshIdOf(mesh), mesh.workspaceKey))
3508
3365
  return;
3509
- void this._recordMemberProof(mesh, membership);
3510
- void this._followJoinedMeshes(mesh);
3366
+ const due = [MESH_WRITER.memberProof, MESH_WRITER.joinedMeshes];
3511
3367
  if (mesh.role === 'workspace') {
3512
- void this._followProjects(mesh);
3513
- this._shareMemberKey(mesh);
3514
- void this._followHosting();
3368
+ due.push(MESH_WRITER.memberKey, MESH_WRITER.hosting, MESH_WRITER.projects);
3515
3369
  }
3370
+ this._processor.enqueue(mesh, due);
3516
3371
  void this._followPublicMesh(mesh);
3517
3372
  });
3518
3373
  this._listenMesh(mesh, handlers);
@@ -3526,26 +3381,91 @@ export class Core extends ReadyResource {
3526
3381
  if (this._isHidden(this._meshIdOf(mesh), mesh.workspaceKey)) {
3527
3382
  return;
3528
3383
  }
3384
+ const due = [];
3529
3385
  if (changed || entry.meshesPending) {
3530
- void this._followJoinedMeshes(mesh);
3386
+ due.push(MESH_WRITER.joinedMeshes);
3531
3387
  }
3532
3388
  if (changed && mesh.role === 'workspace') {
3533
- void this._followProjects(mesh);
3534
- void this._followHosting();
3389
+ due.push(MESH_WRITER.hosting, MESH_WRITER.projects);
3535
3390
  }
3536
3391
  if (mesh.role === 'project') {
3537
- void this._followUpgrade(mesh);
3392
+ due.push(MESH_WRITER.upgrade);
3538
3393
  }
3539
- if ((changed || groupsChanged) && mesh.role === 'workspace') {
3540
- 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);
3541
3400
  }
3542
3401
  const opening = this._ownWorkspaceId !== null && !this._publicMesh();
3543
3402
  if (this._isAccount(mesh) && (changed || entry.publicPending || opening)) {
3544
3403
  void this._followPublicMesh(mesh);
3545
3404
  }
3546
- const copying = chatsChanged || this._accountCopied === null;
3547
- if (copying && this._isAccount(mesh) && this._ownWorkspaceId && this._publicMesh()) {
3548
- 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);
3549
3469
  }
3550
3470
  }
3551
3471
  _listenMesh(mesh, handlers) {
@@ -3653,31 +3573,24 @@ export class Core extends ReadyResource {
3653
3573
  batch.dispatch('put-project-host-key', due);
3654
3574
  }
3655
3575
  }
3656
- _shareMemberKey(workspace) {
3657
- const write = (this._memberKeyWrites.get(workspace) ?? Promise.resolve())
3658
- .then(() => this._writeMemberKey(workspace))
3659
- .catch((err) => {
3660
- if (!this.closing) {
3661
- this._log?.warn(['p2p', 'mesh'], 'sharing our member key failed', err);
3662
- }
3663
- });
3664
- this._memberKeyWrites.set(workspace, write);
3665
- }
3666
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) {
3667
3583
  const account = this._accountMesh();
3668
3584
  const memberId = workspace.ourMemberId;
3669
3585
  if (workspace.role !== 'workspace' || workspace.closing || !workspace.writable) {
3670
- return;
3586
+ return null;
3671
3587
  }
3672
- if (!memberId || !account) {
3673
- return;
3588
+ if (!memberId || !account || !(await isWorkspace(workspace.view))) {
3589
+ return null;
3674
3590
  }
3675
3591
  const boxKey = memberBoxKeyPair(account.seed).publicKey;
3676
3592
  const held = await readMemberKey(workspace.view, memberId);
3677
- if (held && b4a.equals(held.boxKey, boxKey)) {
3678
- return;
3679
- }
3680
- await workspace.dispatch('put-member-key', { memberId, boxKey });
3593
+ return held && b4a.equals(held.boxKey, boxKey) ? null : { memberId, boxKey };
3681
3594
  }
3682
3595
  _devicesOf(mesh) {
3683
3596
  return this._isOwnPublic(mesh) ? (this._accountMesh() ?? mesh) : mesh;
@@ -3685,14 +3598,33 @@ export class Core extends ReadyResource {
3685
3598
  _hasPublic(mesh) {
3686
3599
  return mesh.role === 'workspace' && this._isPooled(mesh);
3687
3600
  }
3688
- async _publicOf(scope, { follow = false } = {}) {
3601
+ async _publicOf(scope, { follow = false, route } = {}) {
3689
3602
  const workspaceId = this._meshIdOf(scope);
3690
3603
  const held = this._publicMesh(workspaceId);
3691
3604
  if (held) {
3692
3605
  return held;
3693
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);
3694
3619
  const account = this._accountMesh();
3695
- 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) {
3696
3628
  await this._followPublicMesh(account);
3697
3629
  }
3698
3630
  const opened = this._publicMesh(workspaceId);
@@ -3717,6 +3649,8 @@ export class Core extends ReadyResource {
3717
3649
  if (!(await this._publicFollow.lock())) {
3718
3650
  return;
3719
3651
  }
3652
+ const started = Date.now();
3653
+ const held = this._publicIds.size;
3720
3654
  try {
3721
3655
  const current = this._accountMesh();
3722
3656
  for (let attempt = 1; !this.closing && current; attempt++) {
@@ -3737,6 +3671,13 @@ export class Core extends ReadyResource {
3737
3671
  }
3738
3672
  finally {
3739
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
+ }
3740
3681
  }
3741
3682
  }
3742
3683
  async _publicWorkspaces(account) {
@@ -3763,7 +3704,7 @@ export class Core extends ReadyResource {
3763
3704
  async _settlePublicMeshes(account) {
3764
3705
  let settled = true;
3765
3706
  try {
3766
- await this._settleOwnWorkspace(account);
3707
+ settled = await this._settleOwnWorkspace(account);
3767
3708
  }
3768
3709
  catch (err) {
3769
3710
  settled = false;
@@ -3790,7 +3731,7 @@ export class Core extends ReadyResource {
3790
3731
  }
3791
3732
  async _settleOwnWorkspace(account) {
3792
3733
  if (this._localDevice && !account.ourMemberId) {
3793
- return;
3734
+ return true;
3794
3735
  }
3795
3736
  const local = (await this.local.listMeshes()).filter((row) => !b4a.equals(row.key, account.key) &&
3796
3737
  !row.workspaceKey &&
@@ -3800,11 +3741,14 @@ export class Core extends ReadyResource {
3800
3741
  const own = ours
3801
3742
  .filter((row) => row.initial && !row.kicked)
3802
3743
  .sort((a, b) => (b.joinedAt || Infinity) - (a.joinedAt || Infinity))[0];
3744
+ if (!own && !account.joined) {
3745
+ return false;
3746
+ }
3803
3747
  const mints = !own &&
3804
3748
  !(await this._markDerivedInitial(account, rows)) &&
3805
3749
  (await this._mintsOwnWorkspace(account, { rows, local: ours }));
3806
3750
  if (!this._isAccount(account)) {
3807
- return;
3751
+ return true;
3808
3752
  }
3809
3753
  this._setOwnWorkspace(own ? b4a.toHex(own.key) : null);
3810
3754
  if (mints) {
@@ -3814,6 +3758,7 @@ export class Core extends ReadyResource {
3814
3758
  this._ownDecided = account;
3815
3759
  this._notifyStatus();
3816
3760
  }
3761
+ return true;
3817
3762
  }
3818
3763
  async _mintsOwnWorkspace(account, { rows, local }) {
3819
3764
  if (!rows.length && !local.length) {
@@ -3919,7 +3864,19 @@ export class Core extends ReadyResource {
3919
3864
  });
3920
3865
  this._publicDrops.set(workspaceId, drop);
3921
3866
  }
3922
- 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) {
3923
3880
  const workspaceId = b4a.toHex(workspaceKey);
3924
3881
  const key = await publicMeshKey(account, workspaceKey);
3925
3882
  const recorded = await this._publicRecord(account, workspaceKey);
@@ -3958,9 +3915,7 @@ export class Core extends ReadyResource {
3958
3915
  else if (key && !b4a.equals(key, held.key)) {
3959
3916
  this._mergeLostMint(account, held, key);
3960
3917
  }
3961
- if (workspaceId === this._ownWorkspaceId) {
3962
- this._migrateIntoPublic(account, workspaceKey);
3963
- }
3918
+ this._upgrade.copyAccount(account);
3964
3919
  }
3965
3920
  async _publicRecord(account, workspaceKey) {
3966
3921
  const value = await this.local.get(publicRecordKey(account.key, workspaceKey));
@@ -3981,6 +3936,7 @@ export class Core extends ReadyResource {
3981
3936
  writerSeed: record.writerSeed,
3982
3937
  bootFrom: record.key ? this._takePublicBoot(record.key) : null
3983
3938
  };
3939
+ const started = Date.now();
3984
3940
  const mesh = this._newMesh(session, 'public');
3985
3941
  mesh.workspaceKey = workspaceKey;
3986
3942
  const deviceId = this._requireDeviceKeyPair().publicKey;
@@ -4005,7 +3961,8 @@ export class Core extends ReadyResource {
4005
3961
  mesh: this._meshIdOf(mesh).slice(0, 8),
4006
3962
  workspace: b4a.toHex(workspaceKey).slice(0, 8),
4007
3963
  minted: !session.baseKey,
4008
- writable: mesh.writable
3964
+ writable: mesh.writable,
3965
+ ms: Date.now() - started
4009
3966
  });
4010
3967
  return mesh;
4011
3968
  }
@@ -4040,7 +3997,7 @@ export class Core extends ReadyResource {
4040
3997
  }
4041
3998
  this._publicIds.set(workspaceId, this._meshIdOf(winner));
4042
3999
  this._notifyStatus();
4043
- await this._publicMigration;
4000
+ await this._upgrade.accountCopy;
4044
4001
  await copyContent({
4045
4002
  from: lost,
4046
4003
  into: winner,
@@ -4065,77 +4022,6 @@ export class Core extends ReadyResource {
4065
4022
  });
4066
4023
  this._publicMerges.set(workspaceId, merge);
4067
4024
  }
4068
- _migrateIntoPublic(account, workspaceKey) {
4069
- if (this._publicMigration) {
4070
- this._copyAgain = true;
4071
- return;
4072
- }
4073
- this._publicMigration = (async () => {
4074
- const into = this._publicMesh(b4a.toHex(workspaceKey));
4075
- const workspace = into && (await this._heldMesh(workspaceKey));
4076
- if (!into || !workspace?.minted) {
4077
- return;
4078
- }
4079
- const chats = await oldChats(account);
4080
- const copied = `${this._meshIdOf(account)}:${chats?.mark}`;
4081
- if (this._accountCopied === copied) {
4082
- return;
4083
- }
4084
- if (!chats) {
4085
- await this._recordCopied(account, workspaceKey);
4086
- this._accountCopied = copied;
4087
- return;
4088
- }
4089
- await into.waitForWritable();
4090
- if (!this._isAccount(account) || !this._isPooled(into)) {
4091
- return;
4092
- }
4093
- await copyContent({
4094
- from: account,
4095
- into,
4096
- deviceId: this._requireDeviceKeyPair().publicKey,
4097
- endsOpenRuns: true
4098
- });
4099
- await this._recordCopied(account, workspaceKey);
4100
- if (await this._markCopied(account, workspaceKey)) {
4101
- this._accountCopied = copied;
4102
- }
4103
- this._log?.info(['p2p', 'mesh'], 'copied the account into its public mesh', {
4104
- mesh: this._meshIdOf(into).slice(0, 8)
4105
- });
4106
- })()
4107
- .catch((err) => {
4108
- if (!this.closing) {
4109
- this._log?.warn(['p2p', 'mesh'], 'copying the account into its public mesh failed', err);
4110
- }
4111
- })
4112
- .finally(() => {
4113
- this._publicMigration = null;
4114
- if (this._copyAgain && !this.closing) {
4115
- this._copyAgain = false;
4116
- this._migrateIntoPublic(account, workspaceKey);
4117
- }
4118
- });
4119
- }
4120
- async _recordCopied(account, workspaceKey) {
4121
- const record = await this._publicRecord(account, workspaceKey);
4122
- if (record && !record.migrated) {
4123
- await this._savePublicRecord(account, workspaceKey, { ...record, migrated: true });
4124
- }
4125
- }
4126
- async _markCopied(account, workspaceKey) {
4127
- const row = await account.view.get(JOINED_MESHES, { meshKey: workspaceKey });
4128
- if (!row || !account.writable) {
4129
- return false;
4130
- }
4131
- if (!row.initial) {
4132
- const { meshKey, memberId, joinedAt } = row;
4133
- await account.dispatch('put-joined-mesh', {
4134
- room: { meshKey, memberId, joinedAt, initial: true }
4135
- });
4136
- }
4137
- return true;
4138
- }
4139
4025
  async _followAgentCopies(agentId) {
4140
4026
  if (!agentId && !(await this._agentCopies.lock()))
4141
4027
  return;
@@ -4397,7 +4283,7 @@ export class Core extends ReadyResource {
4397
4283
  await this._rotatePublicWriters(account);
4398
4284
  }
4399
4285
  async _dropKickedCopy(meshId) {
4400
- await this._teardownMesh(await this._openPooledMesh(meshId, 'leave'), { purge: true });
4286
+ await this._teardownMesh(this._pooled(meshId) ?? b4a.from(meshId, 'hex'));
4401
4287
  }
4402
4288
  async _rotatePublicWriters(account) {
4403
4289
  for (const workspaceKey of await this._publicWorkspaces(account)) {
@@ -4424,14 +4310,62 @@ export class Core extends ReadyResource {
4424
4310
  return null;
4425
4311
  return this._meshes.get(meshKey)?.mesh ?? null;
4426
4312
  }
4427
- async _isOurMemberDevice(deviceId) {
4428
- const account = this._accountMesh();
4429
- const ours = account?.ourMemberId;
4430
- if (!account || !ours || account.closing) {
4431
- 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
+ }
4432
4324
  }
4433
- const row = await account.view.get(DEVICES, { id: deviceId });
4434
- return !!row && !row.revoked && !!row.memberId && b4a.equals(row.memberId, ours);
4325
+ catch (err) {
4326
+ if (!mesh.closing) {
4327
+ mesh.reportError(err);
4328
+ }
4329
+ }
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
+ };
4435
4369
  }
4436
4370
  _accountMesh() {
4437
4371
  return this._pooled(this._accountId);
@@ -4475,25 +4409,9 @@ export class Core extends ReadyResource {
4475
4409
  return indexed ? this._pooled(b4a.toHex(indexed)) : null;
4476
4410
  }
4477
4411
  _pendingUpdatesFor(mesh) {
4478
- const logged = (what) => (err) => this._log?.warn(['p2p', 'mesh'], `${what} the pending view update failed`, err);
4479
4412
  return {
4480
- read: async () => {
4481
- const row = await this.local.readMeshUpdate(mesh().key);
4482
- if (!row) {
4483
- return null;
4484
- }
4485
- const from = row.fromKey ? { key: row.fromKey, length: row.fromLength ?? 0 } : null;
4486
- return { from, to: 0, incremental: !!row.incremental };
4487
- },
4488
- write: ({ from, incremental }) => this.local
4489
- .writeMeshUpdate({
4490
- meshKey: mesh().key,
4491
- fromKey: from?.key,
4492
- fromLength: from?.length,
4493
- incremental
4494
- })
4495
- .catch(logged('writing')),
4496
- 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))))
4497
4415
  };
4498
4416
  }
4499
4417
  _fileIndexFor(mesh) {
@@ -4699,13 +4617,18 @@ export class Core extends ReadyResource {
4699
4617
  void mesh.refreshMembership().catch((error) => {
4700
4618
  this._log?.warn(['p2p', 'mesh'], 'tallying membership failed', error);
4701
4619
  });
4702
- void mesh
4703
- .refreshName()
4704
- .then(() => {
4705
- void this._keepName(mesh);
4706
- this._notifyStatus();
4707
- })
4708
- .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();
4709
4632
  this._syncMeshInBackground(mesh);
4710
4633
  if (inRole(mesh.role, '@qvac/put-chat') && !this._isForeign(mesh)) {
4711
4634
  this._startContentServices(entry, deviceId);
@@ -4716,9 +4639,16 @@ export class Core extends ReadyResource {
4716
4639
  mesh.on('notification', (event) => this.emit('notification', event));
4717
4640
  const build = () => rebuildChatOrder(mesh, this.local)
4718
4641
  .then((gone) => {
4642
+ mesh.chatOrderStale = false;
4719
4643
  void retireGoneChats(mesh, this.local, gone).catch((error) => this._log?.warn(['p2p', 'mesh'], 'retiring gone chats failed', error));
4720
4644
  })
4721
- .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
+ });
4722
4652
  let built = build();
4723
4653
  mesh.chatOrderBuilt = built;
4724
4654
  mesh.syncChatOrder = async (chatIds) => {
@@ -4729,6 +4659,12 @@ export class Core extends ReadyResource {
4729
4659
  built = built.then(build);
4730
4660
  mesh.chatOrderBuilt = built;
4731
4661
  });
4662
+ mesh.on('background-synced', () => {
4663
+ if (!mesh.chatOrderStale)
4664
+ return;
4665
+ built = built.then(build);
4666
+ mesh.chatOrderBuilt = built;
4667
+ });
4732
4668
  mesh.on('chat-tombstone', (chatId, deletedAt) => {
4733
4669
  void this.local
4734
4670
  .clearChatNotify(chatId)
@@ -4794,6 +4730,7 @@ export class Core extends ReadyResource {
4794
4730
  };
4795
4731
  void this._indexMeshFiles(mesh).then(() => Promise.all([reclaim(), sweep()]));
4796
4732
  void this._backfillChatChunks(mesh);
4733
+ void this._backfillWriterLinks(mesh);
4797
4734
  mesh.autobee.on('move-to', () => void sweep());
4798
4735
  }
4799
4736
  async _discardCandidateMesh() {
@@ -4818,17 +4755,11 @@ export class Core extends ReadyResource {
4818
4755
  if (!mesh.writable)
4819
4756
  return false;
4820
4757
  const deviceId = this._requireDeviceKeyPair().publicKey;
4821
- const snap = mesh.view.snapshot();
4822
- try {
4823
- for await (const device of snap.find(DEVICES)) {
4824
- if (!device.revoked && !b4a.equals(device.id, deviceId))
4825
- return true;
4826
- }
4827
- return false;
4828
- }
4829
- finally {
4830
- 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;
4831
4761
  }
4762
+ return false;
4832
4763
  }
4833
4764
  async _announceDeparture(mesh = this._accountMesh(), strict = false) {
4834
4765
  if (!mesh?.writable) {
@@ -5096,6 +5027,61 @@ export class Core extends ReadyResource {
5096
5027
  }))
5097
5028
  };
5098
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
+ }
5099
5085
  async archivedMeshContents(meshId) {
5100
5086
  if (!this.opened)
5101
5087
  await this.ready();
@@ -5169,6 +5155,7 @@ export class Core extends ReadyResource {
5169
5155
  storage: this.storage,
5170
5156
  deviceId: this._requireDeviceKeyPair().publicKey,
5171
5157
  logger: this._log,
5158
+ onerror: this._onerror,
5172
5159
  publicRecord
5173
5160
  }, fn);
5174
5161
  }
@@ -5238,6 +5225,8 @@ export class Core extends ReadyResource {
5238
5225
  }
5239
5226
  }
5240
5227
  await mesh.setName(trimmed);
5228
+ await mesh.refreshName();
5229
+ await this._keepName(mesh);
5241
5230
  }
5242
5231
  async createWorkspace({ name, icon } = {}) {
5243
5232
  if (!this.opened)
@@ -5314,13 +5303,13 @@ export class Core extends ReadyResource {
5314
5303
  }
5315
5304
  async _listProject(workspace, project, listing) {
5316
5305
  const batch = workspace.batch();
5317
- 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) };
5318
5308
  batch.dispatch('put-project', { project: listed });
5319
5309
  const ours = workspace.ourMemberId;
5320
- const sealing = ours && { projectId: project.key, seed: project.seed, sealedBy: ours };
5321
- const own = sealing ? await dueProjectKey(workspace.view, ours, sealing) : null;
5322
- if (own) {
5323
- 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 });
5324
5313
  }
5325
5314
  await batch.flush();
5326
5315
  }
@@ -5328,7 +5317,7 @@ export class Core extends ReadyResource {
5328
5317
  const named = mesh.name;
5329
5318
  const id = b4a.toHex(mesh.key);
5330
5319
  const kept = this._meshNames.get(id);
5331
- if (mesh.role !== 'project' || !named || (kept && kept.namedAt >= named.namedAt)) {
5320
+ if (!named || (kept && kept.namedAt >= named.namedAt)) {
5332
5321
  return;
5333
5322
  }
5334
5323
  const row = { meshKey: mesh.key, name: named.name, namedAt: named.namedAt };
@@ -5337,7 +5326,7 @@ export class Core extends ReadyResource {
5337
5326
  await this.local.keepMeshName(row);
5338
5327
  }
5339
5328
  catch (err) {
5340
- this._log?.warn(['p2p', 'mesh'], 'keeping the project name failed', err);
5329
+ this._log?.warn(['p2p', 'mesh'], 'keeping the mesh name failed', err);
5341
5330
  }
5342
5331
  }
5343
5332
  async _openNewProject(workspace, project) {
@@ -5468,6 +5457,10 @@ export class Core extends ReadyResource {
5468
5457
  await this._discardMesh(mesh);
5469
5458
  throw error;
5470
5459
  }
5460
+ if (workspace) {
5461
+ await mesh.refreshName();
5462
+ await this._keepName(mesh);
5463
+ }
5471
5464
  this._notifyStatus();
5472
5465
  return this._meshIdOf(mesh);
5473
5466
  }
@@ -5553,6 +5546,7 @@ export class Core extends ReadyResource {
5553
5546
  if (this._debugBeforeMeshOpen) {
5554
5547
  await this._debugBeforeMeshOpen(meshId);
5555
5548
  }
5549
+ this._refuseClosing();
5556
5550
  const started = Date.now();
5557
5551
  const key = this._meshKeyOf(meshId);
5558
5552
  const session = await this.local.readMeshSession(key);
@@ -5609,7 +5603,7 @@ export class Core extends ReadyResource {
5609
5603
  open: mesh !== null,
5610
5604
  account,
5611
5605
  label: joined?.label || undefined,
5612
- name: mesh?.name?.name || undefined,
5606
+ name: (mesh?.name ?? this._meshNames.get(meshId))?.name || undefined,
5613
5607
  members: mesh?.membership.members ?? 0,
5614
5608
  shared: joined?.shared ?? false,
5615
5609
  sharedAt: joined?.shared ? joined.sharedAt : undefined,
@@ -5785,9 +5779,10 @@ export class Core extends ReadyResource {
5785
5779
  if (!this.opened)
5786
5780
  await this.ready();
5787
5781
  const key = await this._storedAccountKey();
5782
+ const meshId = key ? b4a.toHex(key) : undefined;
5788
5783
  return {
5789
- meshId: key ? b4a.toHex(key) : undefined,
5790
- writable: this._accountWritable(),
5784
+ meshId,
5785
+ writable: meshId === this._accountId && this._accountWritable(),
5791
5786
  displayName: (await this.displayName()) ?? undefined
5792
5787
  };
5793
5788
  }
@@ -5904,16 +5899,9 @@ export class Core extends ReadyResource {
5904
5899
  const inviterName = memberId
5905
5900
  ? ((await mesh.view.get(MEMBERS, { id: memberId }))?.name ?? undefined)
5906
5901
  : undefined;
5907
- const snap = mesh.view.snapshot();
5908
- let blindPeerRows;
5909
- try {
5910
- blindPeerRows = await snap
5911
- .find(MESH_BLIND_PEERS, { limit: INVITE_BLIND_PEERS_LIMIT })
5912
- .toArray();
5913
- }
5914
- finally {
5915
- await snap.close();
5916
- }
5902
+ const blindPeerRows = await mesh.view
5903
+ .find(MESH_BLIND_PEERS, { limit: INVITE_BLIND_PEERS_LIMIT })
5904
+ .toArray();
5917
5905
  return {
5918
5906
  baseKey: mesh.key,
5919
5907
  seed: mesh.seed,
@@ -5929,7 +5917,8 @@ export class Core extends ReadyResource {
5929
5917
  deviceProof: delegation?.deviceProof,
5930
5918
  workspace,
5931
5919
  workspaceName: opts.memberInvite ? mesh.name?.name || undefined : undefined,
5932
- publicHeads: opts.memberInvite ? [] : await this._publicHeads()
5920
+ publicHeads: opts.memberInvite ? [] : await this._publicHeads(),
5921
+ inviterHeads: mesh.autobee.system.heads.map(({ key, length }) => ({ key, length }))
5933
5922
  };
5934
5923
  }
5935
5924
  async _publicHeads() {
@@ -6009,6 +5998,7 @@ export class Core extends ReadyResource {
6009
5998
  await Promise.all([...this._meshes.values()].map(({ mesh }) => swarm.leave(mesh.discoveryKey)));
6010
5999
  return;
6011
6000
  }
6001
+ swarm.on('connection', (conn) => conn.on('error', noop));
6012
6002
  for (const conn of swarm.connections) {
6013
6003
  conn.destroy?.();
6014
6004
  }
@@ -6247,6 +6237,11 @@ export class Core extends ReadyResource {
6247
6237
  return 'skipped';
6248
6238
  }
6249
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
+ });
6250
6245
  const session = this.local.createCandidateSession(held.seed, meshKey, { role });
6251
6246
  await this.local.recordMesh(meshKey, session, {
6252
6247
  deviceId: this._requireDeviceKeyPair().publicKey
@@ -6325,8 +6320,23 @@ export class Core extends ReadyResource {
6325
6320
  }
6326
6321
  });
6327
6322
  }
6323
+ await this._dropUnlistedFollows(workspace);
6328
6324
  void this._sealProjectKeys();
6329
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
+ }
6330
6340
  _mayFollowProjects(workspace) {
6331
6341
  const leaving = this._leaving !== null &&
6332
6342
  (this._leaving === this._accountId || this._leaving === this._meshIdOf(workspace));
@@ -6373,19 +6383,7 @@ export class Core extends ReadyResource {
6373
6383
  return;
6374
6384
  }
6375
6385
  const batch = workspace.batch();
6376
- for (const memberId of row.memberIds ?? []) {
6377
- const due = await dueProjectKey(workspace.view, memberId, {
6378
- projectId: row.id,
6379
- seed,
6380
- sealedBy
6381
- });
6382
- if (due) {
6383
- batch.dispatch('put-project-key', due);
6384
- }
6385
- }
6386
- for (const due of await dueProjectDeviceKeys(workspace.view, row, { seed, sealedBy })) {
6387
- batch.dispatch('put-project-device-key', due);
6388
- }
6386
+ await sealMembers(batch, { view: workspace.view, row, seed, sealedBy });
6389
6387
  await batch.flush();
6390
6388
  }
6391
6389
  async _sealProjectHosts(workspace, projectId, seed) {
@@ -6652,6 +6650,8 @@ export class Core extends ReadyResource {
6652
6650
  conn.on('error', noop);
6653
6651
  return;
6654
6652
  }
6653
+ conn.setTimeout(PEER_STREAM_TIMEOUT_MS);
6654
+ conn.setKeepAlive(PEER_KEEP_ALIVE_MS);
6655
6655
  const remoteId = b4a.toHex(conn.remotePublicKey).slice(0, 8);
6656
6656
  const start = Date.now();
6657
6657
  let bytes = 0;
@@ -6862,6 +6862,7 @@ export class Core extends ReadyResource {
6862
6862
  leaveWorkspace: (meshId) => this.leaveWorkspace(meshId),
6863
6863
  createProject: (workspaceId, opts) => this.createProject(workspaceId, opts),
6864
6864
  listArchived: () => this.listArchivedMeshes(),
6865
+ upgradeSummary: () => this.workspaceUpgradeSummary(),
6865
6866
  archivedContents: (meshId) => this.archivedMeshContents(meshId),
6866
6867
  migrateArchived: (meshId, workspaceId) => this.migrateArchivedMesh(meshId, workspaceId),
6867
6868
  recoverable: () => this.accountRecoverable(),
@@ -7076,6 +7077,14 @@ export class Core extends ReadyResource {
7076
7077
  this._modelCatalogWatchSource = new ReadyGuard();
7077
7078
  void this._modelCatalogWatchSource.ready().catch(noop);
7078
7079
  }
7080
+ registerSiblingDownloads(hooks) {
7081
+ this._siblingDownloads = hooks;
7082
+ return () => {
7083
+ if (this._siblingDownloads === hooks) {
7084
+ this._siblingDownloads = null;
7085
+ }
7086
+ };
7087
+ }
7079
7088
  registerRetryModelDownload(hook) {
7080
7089
  this._retryModelDownload = hook;
7081
7090
  return () => {
@@ -7133,15 +7142,17 @@ export class Core extends ReadyResource {
7133
7142
  }
7134
7143
  };
7135
7144
  }
7136
- registerEmbedder(embedder, extractors) {
7145
+ registerEmbedder(embedder, extractors, { knowledge = null } = {}) {
7137
7146
  this._embedder = embedder;
7138
7147
  this._ragExtractors = extractors ?? this._ragExtractors;
7148
+ this._knowledge = knowledge;
7139
7149
  const settled = this._reconcileIndexing();
7140
7150
  settled.catch((err) => this._log?.warn(['indexing'], 'reconcile failed', err));
7141
7151
  return () => {
7142
7152
  if (this._embedder !== embedder)
7143
7153
  return settled;
7144
7154
  this._embedder = null;
7155
+ this._knowledge = null;
7145
7156
  return this._reconcileIndexing();
7146
7157
  };
7147
7158
  }
@@ -7172,7 +7183,10 @@ export class Core extends ReadyResource {
7172
7183
  this._isForeign(entry.mesh)) {
7173
7184
  return;
7174
7185
  }
7175
- 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);
7176
7190
  entry.processingFiles = entry.mesh.files;
7177
7191
  entry.processingFiles.startProcessing({
7178
7192
  embedder: this._embedder,
@@ -7180,11 +7194,15 @@ export class Core extends ReadyResource {
7180
7194
  deviceId: this.devicePublicKey,
7181
7195
  device: this._indexingDevice,
7182
7196
  extractors: this._ragExtractors,
7183
- batches: this._embedBatches
7197
+ batches: this._embedBatches,
7198
+ textDir: this._textDir(entry.mesh.key)
7184
7199
  });
7185
7200
  this._indexedEmbedder = this._embedder;
7186
7201
  }
7187
- _takeVectorIndex() {
7202
+ _takeVectorIndex(entry, model) {
7203
+ if (this._knowledge) {
7204
+ return new PluginVectorIndex(this._knowledge, { mesh: b4a.toHex(entry.mesh.key), model });
7205
+ }
7188
7206
  const index = this._spareVectorIndex ?? new MemoryVectorIndex();
7189
7207
  this._spareVectorIndex = null;
7190
7208
  return index;
@@ -7452,17 +7470,18 @@ async function closeConfigReader(reader) {
7452
7470
  }
7453
7471
  async function inviteHead(mesh) {
7454
7472
  const auto = mesh.autobee;
7455
- const info = await auto.system.getInfo();
7456
- 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;
7457
7476
  return head ? { key: head.key, length: head.length } : null;
7458
7477
  }
7459
7478
  async function recordedBlockIsLocal(core) {
7460
- await raceResource(core, core.ready(), 'config core');
7461
- 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');
7462
7481
  if (length <= 0) {
7463
7482
  return true;
7464
7483
  }
7465
- return raceResource(core, core.has(length - 1), 'config core');
7484
+ return core.has(length - 1);
7466
7485
  }
7467
7486
  function statusErrorMessage(context, err) {
7468
7487
  const detail = errorDetail(err);
@@ -7491,3 +7510,19 @@ function credentialKeysOwnedElsewhere(skills, skillName) {
7491
7510
  }
7492
7511
  return keys;
7493
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
+ }