@qvac/core 1.0.0-rc.1 → 1.0.0-rc.3

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 (548) hide show
  1. package/CHANGELOG.md +1008 -1
  2. package/README.md +40 -81
  3. package/dist-lib/index.d.ts +1 -1
  4. package/dist-lib/index.js +1 -1
  5. package/dist-lib/lib/api/agent-folders.d.ts +22 -0
  6. package/dist-lib/lib/api/agent-folders.js +115 -0
  7. package/dist-lib/lib/api/agents.d.ts +19 -2
  8. package/dist-lib/lib/api/agents.js +121 -24
  9. package/dist-lib/lib/api/app-config.d.ts +4 -1
  10. package/dist-lib/lib/api/app-config.js +17 -6
  11. package/dist-lib/lib/api/attachments.js +3 -2
  12. package/dist-lib/lib/api/chat-move.d.ts +4 -0
  13. package/dist-lib/lib/api/chat-move.js +7 -0
  14. package/dist-lib/lib/api/chat.d.ts +13 -2
  15. package/dist-lib/lib/api/chat.js +114 -9
  16. package/dist-lib/lib/api/chunk-delta.d.ts +12 -0
  17. package/dist-lib/lib/api/chunk-delta.js +115 -0
  18. package/dist-lib/lib/api/chunks.d.ts +17 -2
  19. package/dist-lib/lib/api/chunks.js +70 -13
  20. package/dist-lib/lib/api/config.d.ts +6 -2
  21. package/dist-lib/lib/api/config.js +47 -5
  22. package/dist-lib/lib/api/current-mesh.d.ts +13 -5
  23. package/dist-lib/lib/api/current-mesh.js +15 -11
  24. package/dist-lib/lib/api/custom-models.d.ts +7 -1
  25. package/dist-lib/lib/api/custom-models.js +12 -12
  26. package/dist-lib/lib/api/device-routes.d.ts +9 -0
  27. package/dist-lib/lib/api/device-routes.js +123 -0
  28. package/dist-lib/lib/api/devices.d.ts +3 -2
  29. package/dist-lib/lib/api/devices.js +23 -11
  30. package/dist-lib/lib/api/diagnostics.d.ts +2 -1
  31. package/dist-lib/lib/api/diagnostics.js +2 -2
  32. package/dist-lib/lib/api/execution.d.ts +12 -1
  33. package/dist-lib/lib/api/execution.js +44 -14
  34. package/dist-lib/lib/api/files.d.ts +1 -1
  35. package/dist-lib/lib/api/files.js +65 -8
  36. package/dist-lib/lib/api/host-run-log.d.ts +2 -41
  37. package/dist-lib/lib/api/host-run-log.js +35 -83
  38. package/dist-lib/lib/api/index.d.ts +28 -6
  39. package/dist-lib/lib/api/index.js +101 -33
  40. package/dist-lib/lib/api/instrument.d.ts +2 -2
  41. package/dist-lib/lib/api/instrument.js +24 -48
  42. package/dist-lib/lib/api/knowledge.d.ts +4 -1
  43. package/dist-lib/lib/api/knowledge.js +25 -7
  44. package/dist-lib/lib/api/local-files.d.ts +5 -1
  45. package/dist-lib/lib/api/local-files.js +9 -5
  46. package/dist-lib/lib/api/logs.js +3 -3
  47. package/dist-lib/lib/api/members.d.ts +10 -1
  48. package/dist-lib/lib/api/members.js +67 -26
  49. package/dist-lib/lib/api/mesh.d.ts +12 -4
  50. package/dist-lib/lib/api/mesh.js +12 -8
  51. package/dist-lib/lib/api/model-download.d.ts +2 -1
  52. package/dist-lib/lib/api/model-download.js +2 -2
  53. package/dist-lib/lib/api/network.d.ts +10 -2
  54. package/dist-lib/lib/api/network.js +8 -2
  55. package/dist-lib/lib/api/notifications.d.ts +3 -1
  56. package/dist-lib/lib/api/notifications.js +14 -9
  57. package/dist-lib/lib/api/projects.d.ts +20 -0
  58. package/dist-lib/lib/api/projects.js +169 -0
  59. package/dist-lib/lib/api/rag.d.ts +0 -1
  60. package/dist-lib/lib/api/rag.js +71 -34
  61. package/dist-lib/lib/api/served.d.ts +17 -0
  62. package/dist-lib/lib/api/served.js +48 -0
  63. package/dist-lib/lib/api/sync.d.ts +2 -1
  64. package/dist-lib/lib/api/sync.js +2 -2
  65. package/dist-lib/lib/api/tool-activity.d.ts +2 -1
  66. package/dist-lib/lib/api/tool-activity.js +4 -3
  67. package/dist-lib/lib/api/tool-configs.d.ts +2 -1
  68. package/dist-lib/lib/api/tool-configs.js +6 -9
  69. package/dist-lib/lib/api/upload-frames.d.ts +0 -1
  70. package/dist-lib/lib/api/upload-frames.js +0 -1
  71. package/dist-lib/lib/api/watchable.d.ts +3 -2
  72. package/dist-lib/lib/api/watchable.js +2 -3
  73. package/dist-lib/lib/api/workspace.d.ts +5 -0
  74. package/dist-lib/lib/api/workspace.js +47 -0
  75. package/dist-lib/lib/assistant/fake-models.d.ts +20 -0
  76. package/dist-lib/lib/assistant/fake-models.js +112 -0
  77. package/dist-lib/lib/assistant/harness.d.ts +37 -31
  78. package/dist-lib/lib/assistant/harness.js +51 -10
  79. package/dist-lib/lib/assistant/history.d.ts +30 -17
  80. package/dist-lib/lib/assistant/history.js +179 -87
  81. package/dist-lib/lib/assistant/host-run-log.d.ts +6 -1
  82. package/dist-lib/lib/assistant/host-run-log.js +24 -36
  83. package/dist-lib/lib/assistant/message.d.ts +27 -1
  84. package/dist-lib/lib/assistant/message.js +65 -2
  85. package/dist-lib/lib/assistant/model-round.d.ts +100 -0
  86. package/dist-lib/lib/assistant/model-round.js +619 -0
  87. package/dist-lib/lib/assistant/pdf-budget.d.ts +11 -0
  88. package/dist-lib/lib/assistant/pdf-budget.js +12 -0
  89. package/dist-lib/lib/assistant/pdf-pages.d.ts +25 -0
  90. package/dist-lib/lib/assistant/pdf-pages.js +75 -0
  91. package/dist-lib/lib/assistant/round-host.d.ts +42 -0
  92. package/dist-lib/lib/assistant/round-host.js +241 -0
  93. package/dist-lib/lib/assistant/skill-encode.d.ts +2 -1
  94. package/dist-lib/lib/assistant/skill-encode.js +2 -1
  95. package/dist-lib/lib/assistant/video-budget.d.ts +13 -0
  96. package/dist-lib/lib/assistant/video-budget.js +29 -0
  97. package/dist-lib/lib/assistant-client.d.ts +4 -1
  98. package/dist-lib/lib/assistant-client.js +27 -8
  99. package/dist-lib/lib/assistant-tree.d.ts +10 -3
  100. package/dist-lib/lib/assistant-tree.js +63 -20
  101. package/dist-lib/lib/assistant.d.ts +93 -24
  102. package/dist-lib/lib/assistant.js +2048 -1231
  103. package/dist-lib/lib/audio-format.js +1 -1
  104. package/dist-lib/lib/chat-suggestion.d.ts +3 -0
  105. package/dist-lib/lib/chat-suggestion.js +47 -0
  106. package/dist-lib/lib/client.d.ts +2 -2
  107. package/dist-lib/lib/client.js +10 -1
  108. package/dist-lib/lib/compaction.d.ts +3 -1
  109. package/dist-lib/lib/compaction.js +18 -4
  110. package/dist-lib/lib/constants.d.ts +10 -0
  111. package/dist-lib/lib/constants.js +10 -0
  112. package/dist-lib/lib/core/agent-copies.d.ts +9 -0
  113. package/dist-lib/lib/core/agent-copies.js +40 -0
  114. package/dist-lib/lib/core/agent-hosting.d.ts +25 -0
  115. package/dist-lib/lib/core/agent-hosting.js +71 -0
  116. package/dist-lib/lib/core/agent.d.ts +9 -1
  117. package/dist-lib/lib/core/agent.js +45 -19
  118. package/dist-lib/lib/core/archived-mesh.d.ts +11 -6
  119. package/dist-lib/lib/core/archived-mesh.js +77 -58
  120. package/dist-lib/lib/core/async-iterable.js +1 -1
  121. package/dist-lib/lib/core/blind-peers.d.ts +1 -0
  122. package/dist-lib/lib/core/blind-peers.js +1 -0
  123. package/dist-lib/lib/core/chat-activity.d.ts +1 -1
  124. package/dist-lib/lib/core/chat-activity.js +45 -34
  125. package/dist-lib/lib/core/chat-copy.d.ts +42 -0
  126. package/dist-lib/lib/core/chat-copy.js +395 -0
  127. package/dist-lib/lib/core/chat-fork.d.ts +16 -0
  128. package/dist-lib/lib/core/chat-fork.js +246 -0
  129. package/dist-lib/lib/core/chat-group.js +8 -7
  130. package/dist-lib/lib/core/chat-move.d.ts +75 -0
  131. package/dist-lib/lib/core/chat-move.js +598 -0
  132. package/dist-lib/lib/core/chat-order.d.ts +2 -1
  133. package/dist-lib/lib/core/chat-order.js +19 -4
  134. package/dist-lib/lib/core/chat-project.d.ts +2 -0
  135. package/dist-lib/lib/core/chat-project.js +15 -0
  136. package/dist-lib/lib/core/chat-read.d.ts +4 -0
  137. package/dist-lib/lib/core/chat-read.js +49 -0
  138. package/dist-lib/lib/core/chat-search.js +14 -12
  139. package/dist-lib/lib/core/chat.d.ts +33 -4
  140. package/dist-lib/lib/core/chat.js +122 -39
  141. package/dist-lib/lib/core/chunk.d.ts +35 -7
  142. package/dist-lib/lib/core/chunk.js +250 -45
  143. package/dist-lib/lib/core/collections.d.ts +40 -1
  144. package/dist-lib/lib/core/collections.js +40 -1
  145. package/dist-lib/lib/core/config-value.d.ts +6 -0
  146. package/dist-lib/lib/core/config-value.js +14 -0
  147. package/dist-lib/lib/core/contract/activity.d.ts +3 -2
  148. package/dist-lib/lib/core/contract/activity.js +39 -21
  149. package/dist-lib/lib/core/contract/agent-join.d.ts +10 -0
  150. package/dist-lib/lib/core/contract/agent-join.js +104 -0
  151. package/dist-lib/lib/core/contract/agents.d.ts +5 -0
  152. package/dist-lib/lib/core/contract/agents.js +142 -3
  153. package/dist-lib/lib/core/contract/chat.d.ts +4 -0
  154. package/dist-lib/lib/core/contract/chat.js +53 -3
  155. package/dist-lib/lib/core/contract/chunks.d.ts +7 -0
  156. package/dist-lib/lib/core/contract/chunks.js +125 -38
  157. package/dist-lib/lib/core/contract/devices.js +6 -4
  158. package/dist-lib/lib/core/contract/index.d.ts +2 -3
  159. package/dist-lib/lib/core/contract/index.js +36 -11
  160. package/dist-lib/lib/core/contract/indexing.d.ts +1 -0
  161. package/dist-lib/lib/core/contract/indexing.js +51 -7
  162. package/dist-lib/lib/core/contract/invites.d.ts +3 -14
  163. package/dist-lib/lib/core/contract/invites.js +26 -36
  164. package/dist-lib/lib/core/contract/joined-meshes.js +3 -1
  165. package/dist-lib/lib/core/contract/knowledge.d.ts +1 -0
  166. package/dist-lib/lib/core/contract/knowledge.js +57 -6
  167. package/dist-lib/lib/core/contract/members.d.ts +3 -1
  168. package/dist-lib/lib/core/contract/members.js +34 -4
  169. package/dist-lib/lib/core/contract/mesh-kind.d.ts +3 -3
  170. package/dist-lib/lib/core/contract/mesh-kind.js +20 -16
  171. package/dist-lib/lib/core/contract/mesh-name.d.ts +3 -0
  172. package/dist-lib/lib/core/contract/mesh-name.js +31 -0
  173. package/dist-lib/lib/core/contract/mesh-rules.d.ts +8 -0
  174. package/dist-lib/lib/core/contract/mesh-rules.js +35 -0
  175. package/dist-lib/lib/core/contract/mesh-seeds.d.ts +2 -0
  176. package/dist-lib/lib/core/contract/mesh-seeds.js +26 -0
  177. package/dist-lib/lib/core/contract/model-requests.d.ts +26 -0
  178. package/dist-lib/lib/core/contract/model-requests.js +280 -0
  179. package/dist-lib/lib/core/contract/oldest-version.d.ts +9 -0
  180. package/dist-lib/lib/core/contract/oldest-version.js +20 -0
  181. package/dist-lib/lib/core/contract/open-join.d.ts +2 -0
  182. package/dist-lib/lib/core/contract/open-join.js +77 -0
  183. package/dist-lib/lib/core/contract/project-device-keys.d.ts +7 -0
  184. package/dist-lib/lib/core/contract/project-device-keys.js +38 -0
  185. package/dist-lib/lib/core/contract/project-host-keys.d.ts +7 -0
  186. package/dist-lib/lib/core/contract/project-host-keys.js +41 -0
  187. package/dist-lib/lib/core/contract/project-order.d.ts +2 -0
  188. package/dist-lib/lib/core/contract/project-order.js +25 -0
  189. package/dist-lib/lib/core/contract/projects.d.ts +21 -0
  190. package/dist-lib/lib/core/contract/projects.js +327 -0
  191. package/dist-lib/lib/core/contract/public-mesh.d.ts +3 -0
  192. package/dist-lib/lib/core/contract/public-mesh.js +31 -0
  193. package/dist-lib/lib/core/contract/public-seeds.d.ts +9 -0
  194. package/dist-lib/lib/core/contract/public-seeds.js +43 -0
  195. package/dist-lib/lib/core/contract/roles.d.ts +10 -0
  196. package/dist-lib/lib/core/contract/roles.js +121 -0
  197. package/dist-lib/lib/core/contract/run-requests.d.ts +10 -0
  198. package/dist-lib/lib/core/contract/run-requests.js +81 -0
  199. package/dist-lib/lib/core/contract/seed-defaults.d.ts +3 -1
  200. package/dist-lib/lib/core/contract/seed-defaults.js +17 -4
  201. package/dist-lib/lib/core/contract/shipped-rows.d.ts +2 -0
  202. package/dist-lib/lib/core/contract/shipped-rows.js +22 -0
  203. package/dist-lib/lib/core/contract/skill-secrets.d.ts +3 -0
  204. package/dist-lib/lib/core/contract/skill-secrets.js +32 -0
  205. package/dist-lib/lib/core/contract/tool-activity.js +12 -1
  206. package/dist-lib/lib/core/contract/tool-configs.js +2 -1
  207. package/dist-lib/lib/core/contract/types.d.ts +2 -2
  208. package/dist-lib/lib/core/contract/workspace.d.ts +31 -0
  209. package/dist-lib/lib/core/contract/workspace.js +311 -0
  210. package/dist-lib/lib/core/contract/writers.js +100 -16
  211. package/dist-lib/lib/core/departures.d.ts +16 -5
  212. package/dist-lib/lib/core/departures.js +79 -6
  213. package/dist-lib/lib/core/device-control.d.ts +21 -0
  214. package/dist-lib/lib/core/device-control.js +91 -0
  215. package/dist-lib/lib/core/device-seal.d.ts +4 -0
  216. package/dist-lib/lib/core/device-seal.js +22 -0
  217. package/dist-lib/lib/core/devices.d.ts +2 -1
  218. package/dist-lib/lib/core/devices.js +5 -3
  219. package/dist-lib/lib/core/document-attachment.d.ts +10 -0
  220. package/dist-lib/lib/core/document-attachment.js +14 -0
  221. package/dist-lib/lib/core/files/attachments.js +8 -7
  222. package/dist-lib/lib/core/files/index.d.ts +27 -14
  223. package/dist-lib/lib/core/files/index.js +220 -43
  224. package/dist-lib/lib/core/files/knowledge.d.ts +10 -1
  225. package/dist-lib/lib/core/files/knowledge.js +77 -5
  226. package/dist-lib/lib/core/files/preview/index.js +6 -2
  227. package/dist-lib/lib/core/files/processing/claims.d.ts +7 -0
  228. package/dist-lib/lib/core/files/processing/claims.js +33 -7
  229. package/dist-lib/lib/core/files/processing/document.d.ts +8 -0
  230. package/dist-lib/lib/core/files/processing/document.js +26 -2
  231. package/dist-lib/lib/core/files/processing/index.d.ts +8 -3
  232. package/dist-lib/lib/core/files/processing/index.js +99 -73
  233. package/dist-lib/lib/core/files/processing/protocol.d.ts +7 -1
  234. package/dist-lib/lib/core/files/processing/protocol.js +14 -3
  235. package/dist-lib/lib/core/files/processing/source.d.ts +4 -0
  236. package/dist-lib/lib/core/files/processing/source.js +12 -4
  237. package/dist-lib/lib/core/files/processing/state.d.ts +5 -1
  238. package/dist-lib/lib/core/files/processing/state.js +9 -1
  239. package/dist-lib/lib/core/files/rag/build.d.ts +5 -1
  240. package/dist-lib/lib/core/files/rag/build.js +32 -8
  241. package/dist-lib/lib/core/files/rag/failure.d.ts +20 -0
  242. package/dist-lib/lib/core/files/rag/failure.js +57 -0
  243. package/dist-lib/lib/core/files/rag/index.d.ts +13 -2
  244. package/dist-lib/lib/core/files/rag/index.js +30 -8
  245. package/dist-lib/lib/core/files/rag/joined-index.d.ts +6 -0
  246. package/dist-lib/lib/core/files/rag/joined-index.js +35 -0
  247. package/dist-lib/lib/core/files/rag/limits.d.ts +1 -0
  248. package/dist-lib/lib/core/files/rag/limits.js +1 -0
  249. package/dist-lib/lib/core/files/rag/load.js +6 -3
  250. package/dist-lib/lib/core/files/rag/policy.d.ts +10 -1
  251. package/dist-lib/lib/core/files/rag/policy.js +23 -2
  252. package/dist-lib/lib/core/files/rag/produce.d.ts +1 -1
  253. package/dist-lib/lib/core/files/rag/produce.js +31 -18
  254. package/dist-lib/lib/core/files/rag/retrieval/full-text.d.ts +2 -0
  255. package/dist-lib/lib/core/files/rag/retrieval/full-text.js +8 -2
  256. package/dist-lib/lib/core/files/rag/similarity.d.ts +2 -2
  257. package/dist-lib/lib/core/files/rag/similarity.js +2 -3
  258. package/dist-lib/lib/core/files/records/chunks.js +22 -1
  259. package/dist-lib/lib/core/files/records/index.d.ts +1 -1
  260. package/dist-lib/lib/core/files/records/index.js +1 -1
  261. package/dist-lib/lib/core/files/records/knowledge.d.ts +11 -1
  262. package/dist-lib/lib/core/files/records/knowledge.js +40 -3
  263. package/dist-lib/lib/core/files/records/purge.js +13 -8
  264. package/dist-lib/lib/core/files/records/reclaim.js +20 -48
  265. package/dist-lib/lib/core/files/records/routing.d.ts +1 -1
  266. package/dist-lib/lib/core/files/records/routing.js +6 -2
  267. package/dist-lib/lib/core/files/records/source.js +3 -5
  268. package/dist-lib/lib/core/files/records/sweep.d.ts +3 -0
  269. package/dist-lib/lib/core/files/records/sweep.js +9 -3
  270. package/dist-lib/lib/core/files/store/blob.d.ts +2 -0
  271. package/dist-lib/lib/core/files/store/blob.js +2 -2
  272. package/dist-lib/lib/core/files/store/index.d.ts +3 -1
  273. package/dist-lib/lib/core/files/store/index.js +4 -1
  274. package/dist-lib/lib/core/files/store/reference.d.ts +3 -0
  275. package/dist-lib/lib/core/files/store/reference.js +12 -2
  276. package/dist-lib/lib/core/files/store/sweep.js +3 -1
  277. package/dist-lib/lib/core/files/upload.d.ts +5 -1
  278. package/dist-lib/lib/core/files/upload.js +53 -14
  279. package/dist-lib/lib/core/first-event.d.ts +6 -0
  280. package/dist-lib/lib/core/first-event.js +13 -0
  281. package/dist-lib/lib/core/fs-drives.d.ts +6 -3
  282. package/dist-lib/lib/core/fs-drives.js +30 -18
  283. package/dist-lib/lib/core/id.d.ts +1 -0
  284. package/dist-lib/lib/core/id.js +3 -0
  285. package/dist-lib/lib/core/identity.d.ts +3 -1
  286. package/dist-lib/lib/core/identity.js +25 -4
  287. package/dist-lib/lib/core/invite.d.ts +6 -2
  288. package/dist-lib/lib/core/invite.js +22 -8
  289. package/dist-lib/lib/core/local/files.d.ts +2 -1
  290. package/dist-lib/lib/core/local/files.js +5 -7
  291. package/dist-lib/lib/core/local/index.d.ts +86 -7
  292. package/dist-lib/lib/core/local/index.js +312 -27
  293. package/dist-lib/lib/core/local/run-logs.d.ts +38 -0
  294. package/dist-lib/lib/core/local/run-logs.js +132 -0
  295. package/dist-lib/lib/core/member-key.d.ts +7 -0
  296. package/dist-lib/lib/core/member-key.js +24 -0
  297. package/dist-lib/lib/core/membership.d.ts +3 -14
  298. package/dist-lib/lib/core/membership.js +3 -96
  299. package/dist-lib/lib/core/mesh.d.ts +107 -30
  300. package/dist-lib/lib/core/mesh.js +625 -171
  301. package/dist-lib/lib/core/migrations/agent-tools-scope.d.ts +2 -0
  302. package/dist-lib/lib/core/migrations/agent-tools-scope.js +24 -0
  303. package/dist-lib/lib/core/migrations/chat-chunks.d.ts +4 -0
  304. package/dist-lib/lib/core/migrations/chat-chunks.js +47 -0
  305. package/dist-lib/lib/core/migrations/file-index.d.ts +1 -1
  306. package/dist-lib/lib/core/migrations/file-index.js +8 -6
  307. package/dist-lib/lib/core/migrations/index.d.ts +2 -1
  308. package/dist-lib/lib/core/migrations/index.js +69 -8
  309. package/dist-lib/lib/core/model-requests.d.ts +13 -0
  310. package/dist-lib/lib/core/model-requests.js +65 -0
  311. package/dist-lib/lib/core/next-frame.js +2 -1
  312. package/dist-lib/lib/core/notification.js +100 -25
  313. package/dist-lib/lib/core/pdf/figure.d.ts +22 -0
  314. package/dist-lib/lib/core/pdf/figure.js +64 -0
  315. package/dist-lib/lib/core/pdf/rasterize.d.ts +12 -3
  316. package/dist-lib/lib/core/pdf/rasterize.js +77 -28
  317. package/dist-lib/lib/core/peer-duplex.d.ts +34 -0
  318. package/dist-lib/lib/core/{harness-loan.js → peer-duplex.js} +104 -45
  319. package/dist-lib/lib/core/project-keys.d.ts +13 -0
  320. package/dist-lib/lib/core/project-keys.js +56 -0
  321. package/dist-lib/lib/core/prompt-history.js +4 -4
  322. package/dist-lib/lib/core/protocol-pairing.d.ts +11 -0
  323. package/dist-lib/lib/core/protocol-pairing.js +22 -0
  324. package/dist-lib/lib/core/public-mesh.d.ts +26 -0
  325. package/dist-lib/lib/core/public-mesh.js +94 -0
  326. package/dist-lib/lib/core/purge.d.ts +3 -0
  327. package/dist-lib/lib/core/purge.js +7 -0
  328. package/dist-lib/lib/core/run-driver.d.ts +17 -0
  329. package/dist-lib/lib/core/run-driver.js +20 -0
  330. package/dist-lib/lib/core/run-requests.d.ts +11 -0
  331. package/dist-lib/lib/core/run-requests.js +58 -0
  332. package/dist-lib/lib/core/send.d.ts +2 -0
  333. package/dist-lib/lib/core/send.js +17 -4
  334. package/dist-lib/lib/core/senders.d.ts +19 -0
  335. package/dist-lib/lib/core/senders.js +45 -0
  336. package/dist-lib/lib/core/shared-agents.d.ts +40 -0
  337. package/dist-lib/lib/core/shared-agents.js +283 -0
  338. package/dist-lib/lib/core/skill-secrets.d.ts +10 -16
  339. package/dist-lib/lib/core/skill-secrets.js +90 -72
  340. package/dist-lib/lib/core/sync/background.d.ts +35 -0
  341. package/dist-lib/lib/core/sync/background.js +85 -0
  342. package/dist-lib/lib/core/sync/changed.d.ts +20 -0
  343. package/dist-lib/lib/core/sync/changed.js +7 -0
  344. package/dist-lib/lib/core/sync/devices.js +6 -3
  345. package/dist-lib/lib/core/sync/files.d.ts +1 -0
  346. package/dist-lib/lib/core/sync/files.js +50 -72
  347. package/dist-lib/lib/core/sync/index.d.ts +1 -1
  348. package/dist-lib/lib/core/sync/index.js +164 -81
  349. package/dist-lib/lib/core/sync/view-updates.d.ts +33 -0
  350. package/dist-lib/lib/core/sync/view-updates.js +133 -0
  351. package/dist-lib/lib/core/sync-status.js +3 -3
  352. package/dist-lib/lib/core/system-info.d.ts +2 -2
  353. package/dist-lib/lib/core/system-info.js +10 -11
  354. package/dist-lib/lib/core/tool-configs.d.ts +5 -3
  355. package/dist-lib/lib/core/tool-configs.js +11 -11
  356. package/dist-lib/lib/core/transcript-visibility.d.ts +5 -5
  357. package/dist-lib/lib/core/transcript-visibility.js +27 -31
  358. package/dist-lib/lib/core/upgrade-agents.d.ts +11 -0
  359. package/dist-lib/lib/core/upgrade-agents.js +78 -0
  360. package/dist-lib/lib/core/video/attachment.d.ts +1 -0
  361. package/dist-lib/lib/core/video/attachment.js +7 -0
  362. package/dist-lib/lib/core/video/digest.d.ts +16 -0
  363. package/dist-lib/lib/core/video/digest.js +57 -0
  364. package/dist-lib/lib/core/video/frames.d.ts +32 -0
  365. package/dist-lib/lib/core/video/frames.js +224 -0
  366. package/dist-lib/lib/core/workspace-upgrade.d.ts +22 -0
  367. package/dist-lib/lib/core/workspace-upgrade.js +50 -0
  368. package/dist-lib/lib/core.d.ts +303 -84
  369. package/dist-lib/lib/core.js +3952 -1511
  370. package/dist-lib/lib/credential-pusher.d.ts +17 -7
  371. package/dist-lib/lib/credential-pusher.js +111 -62
  372. package/dist-lib/lib/custom-model-pusher.d.ts +2 -4
  373. package/dist-lib/lib/custom-model-task.d.ts +2 -1
  374. package/dist-lib/lib/custom-model-task.js +2 -2
  375. package/dist-lib/lib/device-tree.d.ts +18 -0
  376. package/dist-lib/lib/device-tree.js +32 -0
  377. package/dist-lib/lib/diagnostics.d.ts +7 -0
  378. package/dist-lib/lib/diagnostics.js +22 -0
  379. package/dist-lib/lib/doctor/checks.d.ts +15 -0
  380. package/dist-lib/lib/doctor/checks.js +272 -0
  381. package/dist-lib/lib/doctor/redact.d.ts +4 -0
  382. package/dist-lib/lib/doctor/redact.js +31 -0
  383. package/dist-lib/lib/doctor/render.d.ts +8 -0
  384. package/dist-lib/lib/doctor/render.js +83 -0
  385. package/dist-lib/lib/doctor/tree.d.ts +5 -0
  386. package/dist-lib/lib/doctor/tree.js +167 -0
  387. package/dist-lib/lib/downloads-allowed-pusher.d.ts +8 -0
  388. package/dist-lib/lib/downloads-allowed-pusher.js +37 -0
  389. package/dist-lib/lib/errors.d.ts +794 -0
  390. package/dist-lib/lib/errors.js +728 -0
  391. package/dist-lib/lib/fs-drive-bridge.d.ts +8 -1
  392. package/dist-lib/lib/fs-drive-bridge.js +312 -31
  393. package/dist-lib/lib/guards.d.ts +1 -0
  394. package/dist-lib/lib/guards.js +4 -0
  395. package/dist-lib/lib/inference-calls.d.ts +1 -2
  396. package/dist-lib/lib/inference-calls.js +2 -10
  397. package/dist-lib/lib/integrations/setup-routes.js +7 -5
  398. package/dist-lib/lib/knowledge-search.d.ts +1 -1
  399. package/dist-lib/lib/knowledge-search.js +5 -51
  400. package/dist-lib/lib/kv-cache-reclaim.d.ts +2 -1
  401. package/dist-lib/lib/kv-cache-reclaim.js +30 -0
  402. package/dist-lib/lib/kv-cache-retention.d.ts +35 -0
  403. package/dist-lib/lib/kv-cache-retention.js +98 -0
  404. package/dist-lib/lib/latest-frame.d.ts +10 -0
  405. package/dist-lib/lib/latest-frame.js +48 -0
  406. package/dist-lib/lib/log.d.ts +7 -6
  407. package/dist-lib/lib/log.js +14 -7
  408. package/dist-lib/lib/logger-type.d.ts +2 -5
  409. package/dist-lib/lib/mesh-scoped-engine.js +42 -2
  410. package/dist-lib/lib/model-download-status.d.ts +3 -3
  411. package/dist-lib/lib/model-download.d.ts +16 -12
  412. package/dist-lib/lib/model-download.js +90 -34
  413. package/dist-lib/lib/model-idle-pusher.d.ts +2 -4
  414. package/dist-lib/lib/model-memory-cap.d.ts +27 -0
  415. package/dist-lib/lib/model-memory-cap.js +69 -0
  416. package/dist-lib/lib/models-watch.d.ts +1 -1
  417. package/dist-lib/lib/models-watch.js +4 -4
  418. package/dist-lib/lib/oauth/browser-connect.js +3 -2
  419. package/dist-lib/lib/oauth/flow-core.js +5 -3
  420. package/dist-lib/lib/oauth/flow.js +1 -1
  421. package/dist-lib/lib/oauth/loopback.d.ts +1 -0
  422. package/dist-lib/lib/oauth/loopback.js +57 -12
  423. package/dist-lib/lib/oauth/providers.js +4 -2
  424. package/dist-lib/lib/oauth/redirect-types.d.ts +1 -0
  425. package/dist-lib/lib/oauth/register.js +35 -19
  426. package/dist-lib/lib/oauth/request.d.ts +10 -0
  427. package/dist-lib/lib/oauth/request.js +22 -0
  428. package/dist-lib/lib/oauth/resolve.js +13 -5
  429. package/dist-lib/lib/oauth/token-exchange-core.js +31 -33
  430. package/dist-lib/lib/oauth/types.d.ts +3 -0
  431. package/dist-lib/lib/oauth.d.ts +2 -1
  432. package/dist-lib/lib/oauth.js +1 -0
  433. package/dist-lib/lib/persistent-dir.d.ts +1 -0
  434. package/dist-lib/lib/persistent-dir.js +9 -0
  435. package/dist-lib/lib/probe-rounds.d.ts +3 -0
  436. package/dist-lib/lib/probe-rounds.js +5 -0
  437. package/dist-lib/lib/reconnecting-watch.js +19 -11
  438. package/dist-lib/lib/run-input.d.ts +14 -0
  439. package/dist-lib/lib/run-input.js +33 -0
  440. package/dist-lib/lib/run-notices.d.ts +5 -0
  441. package/dist-lib/lib/run-notices.js +13 -2
  442. package/dist-lib/lib/run-replay.d.ts +44 -0
  443. package/dist-lib/lib/run-replay.js +158 -0
  444. package/dist-lib/lib/run-transcript.d.ts +11 -12
  445. package/dist-lib/lib/run-transcript.js +16 -18
  446. package/dist-lib/lib/suspend-lifecycle.js +15 -5
  447. package/dist-lib/lib/task.js +6 -4
  448. package/dist-lib/lib/test-env.d.ts +1 -0
  449. package/dist-lib/lib/test-env.js +4 -0
  450. package/dist-lib/schema/build.js +66 -55
  451. package/dist-lib/schema/config.d.ts +1 -1
  452. package/dist-lib/schema/config.js +1 -1
  453. package/dist-lib/schema/content.d.ts +5 -1
  454. package/dist-lib/schema/content.js +60 -12
  455. package/dist-lib/schema/engine.d.ts +1 -0
  456. package/dist-lib/schema/engine.js +780 -155
  457. package/dist-lib/schema/field-order.js +8 -1
  458. package/dist-lib/schema/indexes.d.ts +7 -0
  459. package/dist-lib/schema/indexes.js +48 -3
  460. package/dist-lib/schema/local.js +80 -5
  461. package/dist-lib/schema/qvac.d.ts +4 -1
  462. package/dist-lib/schema/qvac.js +474 -14
  463. package/dist-lib/spec/bind.js +212 -48
  464. package/dist-lib/spec/calls.js +46 -5
  465. package/dist-lib/spec/capabilities.d.ts +100 -18
  466. package/dist-lib/spec/config/hyperconf/index.js +1 -1
  467. package/dist-lib/spec/hrpc/hrpc.json +739 -206
  468. package/dist-lib/spec/hrpc/index.js +844 -345
  469. package/dist-lib/spec/hrpc/messages.js +3906 -1749
  470. package/dist-lib/spec/hyperschema/index.js +3906 -1749
  471. package/dist-lib/spec/hyperschema/schema.json +4809 -3119
  472. package/dist-lib/spec/hyperschema/types.d.ts +482 -62
  473. package/dist-lib/spec/local/hyperdb/db.json +95 -1
  474. package/dist-lib/spec/local/hyperdb/index.js +459 -2
  475. package/dist-lib/spec/local/hyperdb/messages.js +1565 -487
  476. package/dist-lib/spec/local/hyperschema/index.js +573 -16
  477. package/dist-lib/spec/local/hyperschema/schema.json +354 -1
  478. package/dist-lib/spec/local/hyperschema/types.d.ts +85 -2
  479. package/dist-lib/spec/mesh/hyperdb/db.json +577 -7
  480. package/dist-lib/spec/mesh/hyperdb/index.js +2224 -6
  481. package/dist-lib/spec/mesh/hyperdb/messages.js +6476 -1266
  482. package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +193 -1
  483. package/dist-lib/spec/mesh/hyperdispatch/index.js +545 -1
  484. package/dist-lib/spec/mesh/hyperdispatch/messages.js +3655 -265
  485. package/dist-lib/spec/mesh/hyperschema/index.js +3655 -265
  486. package/dist-lib/spec/mesh/hyperschema/schema.json +1974 -19
  487. package/dist-lib/spec/mesh/hyperschema/types.d.ts +498 -6
  488. package/package.json +64 -43
  489. package/worker/assistant-sidecar.mjs +18 -2
  490. package/worker/stow-entry.mjs +2 -0
  491. package/dist-lib/lib/core/contract/compute.d.ts +0 -11
  492. package/dist-lib/lib/core/contract/compute.js +0 -48
  493. package/dist-lib/lib/core/device-invites.d.ts +0 -11
  494. package/dist-lib/lib/core/device-invites.js +0 -63
  495. package/dist-lib/lib/core/files/processing/settle.d.ts +0 -13
  496. package/dist-lib/lib/core/files/processing/settle.js +0 -86
  497. package/dist-lib/lib/core/grant-expiry.d.ts +0 -11
  498. package/dist-lib/lib/core/grant-expiry.js +0 -66
  499. package/dist-lib/lib/core/handoff-channel.d.ts +0 -47
  500. package/dist-lib/lib/core/handoff-channel.js +0 -123
  501. package/dist-lib/lib/core/harness-loan.d.ts +0 -29
  502. package/dist-lib/lib/core/host-run-log-store.d.ts +0 -114
  503. package/dist-lib/lib/core/host-run-log-store.js +0 -976
  504. package/dist-lib/lib/core/hosted-agents.d.ts +0 -38
  505. package/dist-lib/lib/core/hosted-agents.js +0 -140
  506. package/dist-lib/lib/core/inference-calls-handoff.d.ts +0 -20
  507. package/dist-lib/lib/core/inference-calls-handoff.js +0 -37
  508. package/dist-lib/lib/core/inference-calls-receiver.d.ts +0 -14
  509. package/dist-lib/lib/core/inference-calls-receiver.js +0 -42
  510. package/dist-lib/lib/core/mesh-seed-handoff.d.ts +0 -25
  511. package/dist-lib/lib/core/mesh-seed-handoff.js +0 -52
  512. package/dist-lib/lib/core/migrations/device-indexes.d.ts +0 -2
  513. package/dist-lib/lib/core/migrations/device-indexes.js +0 -7
  514. package/dist-lib/lib/core/secret-handoff.d.ts +0 -17
  515. package/dist-lib/lib/core/secret-handoff.js +0 -29
  516. package/dist-lib/lib/core/skill-overlay-handoff.d.ts +0 -25
  517. package/dist-lib/lib/core/skill-overlay-handoff.js +0 -50
  518. package/dist-lib/lib/core/skill-overlay-receiver.d.ts +0 -8
  519. package/dist-lib/lib/core/skill-overlay-receiver.js +0 -23
  520. package/dist-lib/lib/image-cache-dir.d.ts +0 -1
  521. package/dist-lib/lib/image-cache-dir.js +0 -5
  522. package/dist-lib/lib/log-levels.d.ts +0 -7
  523. package/dist-lib/lib/log-levels.js +0 -1
  524. package/dist-lib/lib/logger.d.ts +0 -5
  525. package/dist-lib/lib/logger.js +0 -18
  526. package/dist-lib/lib/oauth/loopback.stub.d.ts +0 -3
  527. package/dist-lib/lib/oauth/loopback.stub.js +0 -6
  528. package/tui/README.md +0 -142
  529. package/tui/app.mjs +0 -6939
  530. package/tui/boot.mjs +0 -111
  531. package/tui/index.mjs +0 -53
  532. package/tui/lib/boot-timing.mjs +0 -41
  533. package/tui/lib/busy-words.mjs +0 -116
  534. package/tui/lib/export.mjs +0 -194
  535. package/tui/lib/knowledge-command.mjs +0 -190
  536. package/tui/lib/knowledge-import.mjs +0 -171
  537. package/tui/lib/render.mjs +0 -196
  538. package/tui/lib/save-file.mjs +0 -88
  539. package/tui/lib/select.mjs +0 -81
  540. package/tui/lib/selection.mjs +0 -18
  541. package/tui/lib/setup.mjs +0 -101
  542. package/tui/lib/sidebar.mjs +0 -63
  543. package/tui/lib/splash-art.mjs +0 -107
  544. package/tui/lib/splash.mjs +0 -18
  545. package/tui/lib/upload-frames.mjs +0 -9
  546. package/tui/lib/usage.mjs +0 -59
  547. package/tui/lib/wrap.mjs +0 -143
  548. package/tui/main.mjs +0 -264
@@ -5,37 +5,59 @@ import HyperDB from 'hyperdb';
5
5
  import { ERRORS } from 'hyperdispatch/runtime';
6
6
  import Hyperblobs from 'hyperblobs';
7
7
  import crypto from 'hypercore-crypto';
8
+ import HypercoreId from 'hypercore-id-encoding';
8
9
  import ReadyResource from 'ready-resource';
9
10
  import { Files } from './files/index.js';
10
11
  import MeshDefinition from '../../spec/mesh/hyperdb/index.js';
11
- import { Router, encode } from '../../spec/mesh/hyperdispatch/index.js';
12
+ import { Router, encode, version } from '../../spec/mesh/hyperdispatch/index.js';
12
13
  import { syncChanges } from './sync/index.js';
14
+ import { ViewUpdates } from './sync/view-updates.js';
15
+ import { BackgroundSync } from './sync/background.js';
13
16
  import { buildDefaultSeed, isOwnKickedRow, isOwnRevokedRow } from './helpers/index.js';
14
17
  import { raceResource } from '../race-resource.js';
15
- import { signDeviceAttestation, signWriterClaim } from './device-attestation.js';
16
- import { attestMemberClaim, claimAttestation, deviceInviteToken } from './identity.js';
17
- import { GrantExpiry } from './grant-expiry.js';
18
- import { grantKeyPair, signInviteClaim } from './invite-grants.js';
19
- import { liveDeviceForWriter } from './contract/activity.js';
20
- import { assertComputeRoute } from './contract/compute.js';
18
+ import { signDeviceAttestation, signWriterClaim, verifyWriterClaim } from './device-attestation.js';
19
+ import { devicesByWriterKey, devicesOfMember, liveDeviceForWriter } from './contract/activity.js';
20
+ import { MESH_NAME_ROW } from './contract/mesh-name.js';
21
+ import { versionedOp } from './contract/oldest-version.js';
22
+ import { KNOWN_RULES, rulesVersion } from './contract/mesh-rules.js';
23
+ import { WORKSPACE_ROW } from './contract/workspace.js';
21
24
  import { registerRoutes } from './contract/index.js';
25
+ import { assertRoleRoute, inRole } from './contract/roles.js';
22
26
  import { createSyncActivity } from './sync-status.js';
23
27
  import { buildHealthAssistant } from './health-assistant.js';
24
28
  import b4a from 'b4a';
25
29
  import { errorMessage, isRecord } from '../guards.js';
26
- import { ACTIVE_AGENTS, ACTIVE_CHATS, DEVICES, LIVE_WRITERS_BY_MOST_RECENT, MEMBERS, MESH_KINDS } from './collections.js';
30
+ import { CoreError, isCoreError } from '../errors.js';
31
+ import { ACTIVE_AGENTS, ACTIVE_CHATS, DEVICES, LIVE_WRITERS_BY_MOST_RECENT, MEMBERS, MESH_NAMES } from './collections.js';
27
32
  const [MESH_KEYPAIR, MESH_ENCRYPTION, MESH_DEVICE] = crypto.namespace('qvac-assistant/mesh', 3);
33
+ const DEFAULT_NAMESPACE = b4a.alloc(32);
34
+ export const SCHEMA_VERSION = version;
35
+ const SLOW_WRITE_MS = 5000;
36
+ const SLOW_DRAIN_MS = 15_000;
37
+ const BLOB_CORE = 'qvac-blobs';
28
38
  export function meshPublicKeyFromSeed(seed) {
29
39
  return crypto.keyPair(crypto.hash([MESH_KEYPAIR, seed])).publicKey;
30
40
  }
31
- const GENESIS_POLL_MS = 250;
41
+ async function ownCores(storage, namespace, writerKey) {
42
+ const keys = [crypto.discoveryKey(writerKey)];
43
+ for await (const discoveryKey of storage.createDiscoveryKeyStream(namespace)) {
44
+ keys.push(discoveryKey);
45
+ }
46
+ const views = `autobee/${HypercoreId.encode(writerKey)}/`;
47
+ for await (const { alias, discoveryKey } of storage.createAliasStream(DEFAULT_NAMESPACE)) {
48
+ if (alias.name.startsWith(views)) {
49
+ keys.push(discoveryKey);
50
+ }
51
+ }
52
+ return keys;
53
+ }
32
54
  const WARMUP_COLLECTIONS = [
33
55
  '@qvac/devices',
34
56
  '@qvac/active-agents',
35
57
  '@qvac/members',
36
58
  '@qvac/active-chats'
37
59
  ];
38
- export async function isOurTrustedWriter(snap, writerKey, ourMemberId) {
60
+ export async function isTrustedWriter(snap, writerKey, { ourMemberId }) {
39
61
  const device = await liveDeviceForWriter(snap, writerKey);
40
62
  if (!device) {
41
63
  return false;
@@ -43,7 +65,7 @@ export async function isOurTrustedWriter(snap, writerKey, ourMemberId) {
43
65
  if (!ourMemberId) {
44
66
  return true;
45
67
  }
46
- return !!device.memberId && b4a.equals(device.memberId, ourMemberId);
68
+ return !!device.memberId;
47
69
  }
48
70
  export function claimableDevice(row) {
49
71
  return {
@@ -61,13 +83,13 @@ export class Mesh extends ReadyResource {
61
83
  return this._files;
62
84
  }
63
85
  _store;
86
+ _namespaceId = null;
64
87
  _seed;
65
- _compute;
88
+ _role;
66
89
  _key;
67
90
  _writerSeed;
68
91
  _log;
69
92
  _blindPeerKeys;
70
- _blindPeeringOpts;
71
93
  _bootFrom;
72
94
  _wakeup;
73
95
  attachmentUrl;
@@ -78,10 +100,18 @@ export class Mesh extends ReadyResource {
78
100
  _deviceKeyPair;
79
101
  _encryptionKey;
80
102
  _blindPeering = null;
103
+ openedEmpty = false;
104
+ foreign = false;
105
+ workspaceKey = null;
106
+ _updates;
107
+ _openedAt = 0;
108
+ _trustedDevice;
109
+ _joined = false;
81
110
  _orphanedBlobSweepStatus = { state: 'pending' };
82
111
  _bootInterrupted = false;
83
112
  _localWriterUploadedLength = 0;
84
113
  _ourMemberId = null;
114
+ _name = null;
85
115
  _membership = {
86
116
  members: 0,
87
117
  attested: 0,
@@ -89,11 +119,18 @@ export class Mesh extends ReadyResource {
89
119
  revoked: 0
90
120
  };
91
121
  _deviceId;
92
- _grantExpiry;
122
+ _slowWriteMs;
123
+ _slowDrainMs;
124
+ _stallReported = false;
93
125
  _admitted = false;
126
+ _refused = false;
127
+ _kickSeen = null;
128
+ _birth = null;
129
+ _rulesRaised = null;
94
130
  _autobee = null;
95
131
  _blobs = null;
96
132
  _syncActivity = null;
133
+ _background;
97
134
  discoveryKey;
98
135
  get autobee() {
99
136
  return openedPart(this._autobee, 'autobee');
@@ -107,14 +144,17 @@ export class Mesh extends ReadyResource {
107
144
  get publicKey() {
108
145
  return this._keyPair.publicKey;
109
146
  }
147
+ get log() {
148
+ return this._log;
149
+ }
110
150
  get devicePublicKey() {
111
151
  return this._deviceKeyPair.publicKey;
112
152
  }
113
153
  get key() {
114
154
  return this.autobee.key;
115
155
  }
116
- get compute() {
117
- return this._compute;
156
+ get role() {
157
+ return this._role;
118
158
  }
119
159
  get encryptionKey() {
120
160
  return this._encryptionKey;
@@ -133,7 +173,10 @@ export class Mesh extends ReadyResource {
133
173
  if (!peering?.blindPeers) {
134
174
  return [];
135
175
  }
136
- return [...peering.blindPeers.values()].map((bp) => ({
176
+ const own = new Set(this._blindPeerKeys.map((key) => b4a.toHex(key)));
177
+ return [...peering.blindPeers.values()]
178
+ .filter((bp) => own.has(b4a.toHex(bp.remotePublicKey)))
179
+ .map((bp) => ({
137
180
  key: b4a.toHex(bp.remotePublicKey).slice(0, 16),
138
181
  connected: bp.connected,
139
182
  connects: bp.connects,
@@ -149,6 +192,11 @@ export class Mesh extends ReadyResource {
149
192
  get membership() {
150
193
  return this._membership;
151
194
  }
195
+ tallyMembership() {
196
+ this.refreshMembership()
197
+ .then(() => this.emit('membership-tallied'))
198
+ .catch((err) => this._log?.warn(this._scope, 'tallying membership failed', err));
199
+ }
152
200
  async refreshMembership() {
153
201
  const snap = this.view.snapshot();
154
202
  try {
@@ -183,7 +231,7 @@ export class Mesh extends ReadyResource {
183
231
  }
184
232
  async refreshOurMember() {
185
233
  if (!this._deviceId)
186
- return;
234
+ return null;
187
235
  const own = await this.view.get(DEVICES, { id: this._deviceId });
188
236
  this._ourMemberId = own?.memberId ?? null;
189
237
  if (own?.memberId && own.memberProof) {
@@ -194,28 +242,20 @@ export class Mesh extends ReadyResource {
194
242
  };
195
243
  this.emit('membership', membership);
196
244
  }
245
+ return own;
197
246
  }
198
- refreshGrantExpiry() {
199
- return this._grantExpiry.refresh();
247
+ get name() {
248
+ return this._name;
200
249
  }
201
- async bornKind() {
202
- for await (const _device of this.view.find(DEVICES)) {
203
- return (await this.view.get(MESH_KINDS, { id: 'mesh' }))?.kind ?? null;
204
- }
205
- return undefined;
250
+ async refreshName() {
251
+ this._name = await this.view.get(MESH_NAMES, { id: MESH_NAME_ROW });
206
252
  }
207
- async waitForGenesis(timeout) {
208
- const deadline = Date.now() + timeout;
209
- let kind = await this.bornKind();
210
- while (kind === undefined && Date.now() < deadline) {
211
- await new Promise((resolve) => setTimeout(resolve, GENESIS_POLL_MS));
212
- if (this.closing) {
213
- return undefined;
214
- }
215
- await this.autobee.update();
216
- kind = await this.bornKind();
217
- }
218
- return kind;
253
+ async setName(name) {
254
+ const current = await this.view.get(MESH_NAMES, { id: MESH_NAME_ROW });
255
+ await this.dispatch('name-mesh', {
256
+ name,
257
+ namedAt: Math.max(Date.now(), (current?.namedAt ?? 0) + 1)
258
+ });
219
259
  }
220
260
  sign(message) {
221
261
  return crypto.sign(message, this._keyPair.secretKey);
@@ -227,35 +267,55 @@ export class Mesh extends ReadyResource {
227
267
  super();
228
268
  this._store = store;
229
269
  this._seed = opts.seed ?? crypto.randomBytes(32);
230
- this._compute = opts.compute ?? false;
270
+ this._role = opts.role ?? 'account';
231
271
  this._key = opts.key ?? null;
232
272
  this._writerSeed = opts.writerSeed;
233
273
  this._log = opts.logger ?? null;
274
+ this._slowWriteMs = opts.slowWriteMs ?? SLOW_WRITE_MS;
275
+ this._slowDrainMs = opts.slowDrainMs ?? SLOW_DRAIN_MS;
234
276
  this._blindPeerKeys = opts.blindPeerKeys ?? [];
235
- this._blindPeeringOpts = opts.blindPeeringOpts;
236
277
  this._bootFrom = opts.bootFrom;
278
+ this._trustedDevice = opts.trustedDevice ?? null;
237
279
  this._deviceId = opts.deviceId ?? null;
238
280
  this._wakeup = opts.wakeup;
239
281
  this.attachmentUrl = opts.attachmentUrl ?? null;
240
282
  this.fileIndex = opts.fileIndex ?? null;
283
+ this._updates = new ViewUpdates((update) => this._syncUpdate(update), opts.pendingUpdates);
241
284
  this.router = new Router();
242
285
  this._keyPair = crypto.keyPair(crypto.hash([MESH_KEYPAIR, this._seed]));
243
286
  this._deviceKeyPair = crypto.keyPair(crypto.hash([MESH_DEVICE, this._seed]));
244
287
  this._encryptionKey = crypto.hash([MESH_ENCRYPTION, this._seed]);
245
288
  this.discoveryKey = crypto.discoveryKey(this._keyPair.publicKey);
246
289
  this._scope = ['p2p', 'mesh', b4a.toHex(this._keyPair.publicKey).slice(0, 8)];
247
- registerRoutes(this.router, { compute: this._compute });
248
- this._grantExpiry = new GrantExpiry(this, (err) => this._log?.warn(this._scope, 'revoking expired invite grants failed', err));
290
+ registerRoutes(this.router, { role: this._role });
291
+ this._background = new BackgroundSync(this, {
292
+ logger: this._log,
293
+ scope: this._scope
294
+ });
249
295
  }
250
296
  async _open() {
251
- this._log?.info(this._scope, 'opening');
297
+ this._openedAt = Date.now();
298
+ this._log?.info(this._scope, 'opening', {
299
+ schema: SCHEMA_VERSION,
300
+ creator: this._key === null,
301
+ role: this._role,
302
+ bootFrom: this._bootFrom?.length ?? null,
303
+ blindPeers: this._blindPeerKeys.length
304
+ });
252
305
  const namespace = this._store.namespace(this.discoveryKey);
253
- const blobCore = namespace.get({ name: 'qvac-blobs', encryptionKey: this._encryptionKey });
306
+ this._namespaceId = namespace.ns;
307
+ const blobCore = namespace.get({
308
+ name: await this._blobCoreName(namespace),
309
+ encryptionKey: this._encryptionKey
310
+ });
254
311
  const blobs = new Hyperblobs(blobCore);
255
312
  this._blobs = blobs;
256
313
  try {
257
314
  await raceResource(blobs.core, blobs.ready(), 'blobs');
258
- this._log?.info(this._scope, 'blob store opened');
315
+ this._log?.info(this._scope, 'blob store opened', {
316
+ blobs: blobs.core.length,
317
+ sinceOpenMs: this._sinceOpen()
318
+ });
259
319
  const writerKeyPair = await namespace.createKeyPair(this._writerSeed ?? 'local');
260
320
  const autobee = new Autobee(namespace, this._key, {
261
321
  open: this._autobeeOpen.bind(this),
@@ -274,14 +334,17 @@ export class Mesh extends ReadyResource {
274
334
  });
275
335
  this._autobee = autobee;
276
336
  this.autobee.on('error', (err) => this._log?.warn(this._scope, 'autobee error', err));
277
- this.autobee.on('writable', () => void this.checkOwnAdmission().catch(noop));
337
+ this.autobee.on('writable', () => {
338
+ void this.checkOwnAdmission().catch(noop);
339
+ this._raiseRulesLater();
340
+ });
278
341
  this.autobee.on('move-to', (to, from) => this._log?.info(this._scope, 'fast-forwarded onto a head', {
279
342
  from: from?.length ?? 0,
280
343
  to: to?.length ?? 0,
281
- fastForwards: this.autobee.stats.fastForwards
344
+ fastForwards: this.autobee.stats.fastForwards,
345
+ sinceOpenMs: this._sinceOpen()
282
346
  }));
283
347
  this.autobee.on('interrupt', (err) => this._log?.warn(this._scope, 'autobee interrupt', err));
284
- this.autobee.on('writable', () => void this._grantExpiry.refresh());
285
348
  if (this._bootInterrupted) {
286
349
  void this.autobee.close().catch(noop);
287
350
  }
@@ -292,37 +355,103 @@ export class Mesh extends ReadyResource {
292
355
  await blobs.close().catch(noop);
293
356
  throw err;
294
357
  }
358
+ this.autobee.bootstrap.on('peer-add', () => this.emit('peer-add'));
359
+ this.openedEmpty = !this.view.db.head()?.length;
295
360
  this.autobee.local.on('upload', (index) => {
296
361
  this._localWriterUploadedLength = Math.max(this._localWriterUploadedLength, index + 1);
297
362
  });
298
363
  this._syncActivity = createSyncActivity(this);
299
364
  void this.refreshOurMember().catch((err) => this._log?.warn(this._scope, 'reading our member failed', err));
300
- void this._grantExpiry.refresh();
301
365
  this._log?.info(this._scope, 'autobee opened', {
302
366
  writable: this.autobee.writable,
303
- creator: this._key === null
367
+ creator: this._key === null,
368
+ ...this._lengths(),
369
+ fastForwards: this.autobee.stats.fastForwards,
370
+ writers: this.writerCount,
371
+ sinceOpenMs: this._sinceOpen()
304
372
  });
305
- void this._sweepOrphanedBlobs(this.blobs.core.length);
373
+ void this._updates
374
+ .start()
375
+ .catch((err) => this._log?.warn(this._scope, 'resuming the view sync failed', err));
376
+ if (!this.foreign) {
377
+ void this._sweepOrphanedBlobs(this.blobs.core.length);
378
+ }
379
+ this._raiseRulesLater();
306
380
  this.files.start({ deviceId: this._deviceId });
307
381
  }
308
- startBlindPeering(dht) {
382
+ startBlindPeering(peering) {
309
383
  if (this.closing || this._blindPeering || !this._autobee || !this._blobs) {
310
384
  return;
311
385
  }
312
- this._blindPeering = new BlindPeering(dht, this._store, {
313
- pick: 2,
314
- wakeup: this._wakeup,
315
- ...this._blindPeeringOpts
316
- });
317
- this._blindPeering.addAutobaseBackground(this._autobee, {
386
+ this._blindPeering = peering;
387
+ peering.addAutobaseBackground(this._autobee, {
318
388
  target: this._keyPair.publicKey,
319
- keys: this._blindPeerKeys
389
+ keys: this._blindPeerKeys,
390
+ pick: 2
320
391
  });
321
- this._blindPeering.addCoreBackground(this._blobs.core, {
392
+ peering.addCoreBackground(this._blobs.core, {
322
393
  target: this._keyPair.publicKey,
323
- keys: this._blindPeerKeys
394
+ keys: this._blindPeerKeys,
395
+ pick: 2
324
396
  });
325
- this._log?.info(this._scope, 'blind peering started');
397
+ this._log?.info(this._scope, 'blind peering started', { peers: this._blindPeerKeys.length });
398
+ }
399
+ stopBlindPeering() {
400
+ if (!this._blindPeering) {
401
+ return;
402
+ }
403
+ this._blindPeering = null;
404
+ this._log?.info(this._scope, 'blind peering stopped', { peers: this._blindPeerKeys.length });
405
+ }
406
+ get bootFrom() {
407
+ return this._bootFrom?.length ?? null;
408
+ }
409
+ async wakeHeldWriters() {
410
+ for await (const writer of this.autobee.system.list()) {
411
+ if (!writer.isRemoved) {
412
+ this.autobee.writers.wakeup(writer.key, 0);
413
+ }
414
+ }
415
+ }
416
+ get writerCount() {
417
+ let count = 0;
418
+ for (const _ of this.autobee.activeWriters) {
419
+ count++;
420
+ }
421
+ return count;
422
+ }
423
+ get uptimeMs() {
424
+ return this._openedAt ? this._sinceOpen() : 0;
425
+ }
426
+ blindPeersConnected() {
427
+ return this.blindPeerStats().filter((peer) => peer.connected).length;
428
+ }
429
+ _sinceOpen() {
430
+ return Date.now() - this._openedAt;
431
+ }
432
+ _lengths() {
433
+ return {
434
+ localLength: this.autobee.local.length,
435
+ systemLength: this.autobee.system.bee.head()?.length ?? 0,
436
+ viewLength: this.view.db.head()?.length ?? 0,
437
+ viewCore: viewCoreOf(this.view.db.head()),
438
+ flushes: this.autobee.system.flushes
439
+ };
440
+ }
441
+ async _blobCoreName(namespace) {
442
+ if (this._role !== 'project' || !this._writerSeed) {
443
+ return BLOB_CORE;
444
+ }
445
+ const storage = this._store.storage;
446
+ const shared = await storage.getAlias({ name: BLOB_CORE, namespace: namespace.ns });
447
+ return shared ? BLOB_CORE : `${BLOB_CORE}/${this._writerSeed}`;
448
+ }
449
+ ownCoreDiscoveryKeys(storage) {
450
+ const namespace = this._namespaceId;
451
+ if (!namespace) {
452
+ return Promise.resolve([]);
453
+ }
454
+ return ownCores(storage, namespace, this.autobee.local.key);
326
455
  }
327
456
  async openBlobs(coreKey) {
328
457
  const core = this._store.get({ key: coreKey, encryptionKey: this._encryptionKey });
@@ -337,16 +466,21 @@ export class Mesh extends ReadyResource {
337
466
  }
338
467
  }
339
468
  async _close() {
340
- this._log?.info(this._scope, 'closing');
341
- this._grantExpiry.close();
469
+ this._updates.close();
470
+ this._log?.info(this._scope, 'closing', {
471
+ sinceOpenMs: this._sinceOpen(),
472
+ ...this._lengths(),
473
+ ...this.autobee.stats
474
+ });
475
+ this._background.stop();
342
476
  await this._files?.close();
343
- await Promise.all([this.autobee.close(), this.blobs.close(), this._blindPeering?.close()]);
477
+ await Promise.all([this.autobee.close(), this.blobs.close()]);
344
478
  this._blindPeering = null;
345
479
  }
346
480
  async _sweepOrphanedBlobs(blobLength) {
347
481
  this._orphanedBlobSweepStatus = { state: 'running' };
348
482
  try {
349
- await this.autobee.update();
483
+ await this._watchStall(this.autobee.update(), { what: 'mesh drain', ms: this._slowDrainMs });
350
484
  await this.files.sweepOrphans(blobLength);
351
485
  this._orphanedBlobSweepStatus = { state: 'succeeded' };
352
486
  this._log?.info(this._scope, 'orphaned blob sweep finished');
@@ -383,14 +517,176 @@ export class Mesh extends ReadyResource {
383
517
  get localLength() {
384
518
  return this.autobee.local.length;
385
519
  }
520
+ get joining() {
521
+ return this._birth !== null;
522
+ }
386
523
  async dispatch(name, payload, opts) {
387
- assertComputeRoute(this._compute, name);
388
- await this.autobee.append(encode(`@qvac/${name}`, payload), opts);
524
+ assertRoleRoute(this._role, name);
525
+ assertJoined(this.autobee, opts);
526
+ const write = this.autobee.append(encode(`@qvac/${name}`, versionedOp(name, payload)), opts);
527
+ await this._watchStall(write, { what: 'mesh write', op: name, ms: this._slowWriteMs });
528
+ }
529
+ async _watchStall(pending, watch) {
530
+ const started = Date.now();
531
+ const clock = { reports: 0, timer: null };
532
+ const report = (wait) => {
533
+ clock.timer = setTimeout(() => {
534
+ if (!this.closing) {
535
+ report(wait * 2);
536
+ }
537
+ if (clock.reports === 0 && this._stallReported) {
538
+ return;
539
+ }
540
+ this._stallReported = true;
541
+ clock.reports++;
542
+ this._reportStall(watch, { started, reports: clock.reports }).catch(noop);
543
+ }, wait);
544
+ };
545
+ report(watch.ms);
546
+ try {
547
+ await pending;
548
+ this._endStall(watch, { reported: clock.reports > 0, started, failed: false });
549
+ }
550
+ catch (err) {
551
+ this._endStall(watch, { reported: clock.reports > 0, started, failed: true, err });
552
+ throw err;
553
+ }
554
+ finally {
555
+ if (clock.timer) {
556
+ clearTimeout(clock.timer);
557
+ }
558
+ }
559
+ }
560
+ _endStall({ what, op }, { reported, started, failed, err }) {
561
+ if (!reported) {
562
+ return;
563
+ }
564
+ this._stallReported = false;
565
+ const ms = Date.now() - started;
566
+ if (failed) {
567
+ this._log?.warn(this._scope, `${what} failed`, { op, ms, err });
568
+ }
569
+ else {
570
+ this._log?.info(this._scope, `${what} settled`, { op, ms });
571
+ }
572
+ }
573
+ async _reportStall({ what, op }, { started, reports }) {
574
+ const { stats, system, writable } = this.autobee;
575
+ const missing = await this._coresNotHeld([system.bee, this.autobee.bee]).catch(noop);
576
+ const fastForwarding = await this._fastForwardHeld().catch(noop);
577
+ const writers = writable ? undefined : await this._writersHeld().catch(noop);
578
+ this._log?.warn(this._scope, `${what} still pending`, {
579
+ op,
580
+ reports,
581
+ afterMs: Date.now() - started,
582
+ writable,
583
+ interrupted: !!this.autobee.interrupted,
584
+ drains: stats.drains,
585
+ applies: stats.applies,
586
+ appends: stats.appends,
587
+ fastForwards: stats.fastForwards,
588
+ fastForwarding,
589
+ localLength: this.autobee.local.length,
590
+ missing,
591
+ writers,
592
+ blindPeers: this.blindPeerStats()
593
+ });
594
+ }
595
+ async _fastForwardHeld() {
596
+ const ff = this.autobee.fastForwarding;
597
+ if (!ff) {
598
+ return null;
599
+ }
600
+ return { to: ff.head.length, ...(await this._coreHeld(ff.head.key)) };
601
+ }
602
+ async _writersHeld() {
603
+ const writers = [...this.autobee.activeWriters];
604
+ return Promise.all(writers.map((writer) => this._coreHeld(writer.core.key)));
605
+ }
606
+ async _coresNotHeld(views) {
607
+ const keys = new Map();
608
+ for (const { context } of views) {
609
+ for (const { key } of [context.core, ...context.cores]) {
610
+ keys.set(b4a.toHex(key), key);
611
+ }
612
+ }
613
+ const held = await Promise.all([...keys.values()].map((key) => this._coreHeld(key)));
614
+ return held.filter((core) => core.held < core.length);
615
+ }
616
+ async _coreHeld(key) {
617
+ const core = this._store.get({ key, active: false });
618
+ try {
619
+ await core.ready();
620
+ const reach = core.peers.map(remoteReach);
621
+ return {
622
+ key: b4a.toHex(key).slice(0, 8),
623
+ length: core.length,
624
+ held: core.contiguousLength,
625
+ peers: core.peers.length,
626
+ peerLength: Math.max(0, ...reach.map((peer) => peer.length)),
627
+ peerHeld: Math.max(0, ...reach.map((peer) => peer.held))
628
+ };
629
+ }
630
+ finally {
631
+ await core.close();
632
+ }
389
633
  }
390
634
  batch() {
391
- return new MeshBatch(this.autobee, { compute: this._compute });
635
+ return new MeshBatch(this.autobee, {
636
+ role: this._role,
637
+ joined: true,
638
+ watch: (op, write) => this._watchStall(write, { what: 'mesh write', op, ms: this._slowWriteMs })
639
+ });
640
+ }
641
+ _raiseRulesLater() {
642
+ if (this._rulesRaised || !this.autobee.writable)
643
+ return;
644
+ if (this.minted && this.localLength === 0) {
645
+ this._rulesRaised = Promise.resolve();
646
+ return;
647
+ }
648
+ this._rulesRaised = this._raiseRules().catch((err) => {
649
+ if (!this.closing) {
650
+ this._log?.warn(this._scope, 'raising the mesh rules failed', err);
651
+ }
652
+ });
653
+ }
654
+ async _raiseRules() {
655
+ await this.autobee.update();
656
+ if ((await rulesVersion(this.view)) >= KNOWN_RULES) {
657
+ return;
658
+ }
659
+ await this.dispatch('raise-mesh-rules', { rules: KNOWN_RULES });
392
660
  }
393
- async appendBirthOp(identity, device, { member, claim, sharing = false, grantSecret, recovery, vouched = false } = {}) {
661
+ appendBirthOp(identity, device, opts = {}) {
662
+ this._birth ??= (async () => {
663
+ await this._appendBirthOp(identity, device, typeof opts === 'function' ? await opts() : opts);
664
+ })().finally(() => {
665
+ this._birth = null;
666
+ });
667
+ return this._birth.then(() => this._settleBirth());
668
+ }
669
+ async _settleBirth() {
670
+ const started = Date.now();
671
+ await this.autobee.update();
672
+ this.throwIfInterrupted();
673
+ const { applies, undos, drains, fastForwards } = this.autobee.stats;
674
+ this._log?.info(this._scope, 'joined', {
675
+ ms: Date.now() - started,
676
+ applies,
677
+ undos,
678
+ drains,
679
+ fastForwards,
680
+ ...this._lengths(),
681
+ sinceOpenMs: this._sinceOpen()
682
+ });
683
+ this._joined = true;
684
+ this.emit('joined');
685
+ }
686
+ get joined() {
687
+ return this._joined;
688
+ }
689
+ async _appendBirthOp(identity, device, { member, sharing = false, defaults = true, workspace, project } = {}) {
394
690
  if (this.autobee.local.length > 0)
395
691
  return;
396
692
  const { publicKey: deviceId, secretKey } = 'secretKey' in identity ? identity : { publicKey: identity, secretKey: null };
@@ -400,80 +696,72 @@ export class Mesh extends ReadyResource {
400
696
  : undefined;
401
697
  const deviceSignature = secretKey && row ? this.signDeviceClaim(secretKey, deviceId, row.joinedAt) : undefined;
402
698
  if (this._key === null) {
403
- const birth = this.batch();
699
+ const birth = new MeshBatch(this.autobee, { role: this._role });
700
+ birth.dispatch('raise-mesh-rules', { rules: KNOWN_RULES });
404
701
  birth.dispatch('add-writer', { writerKey: localKey, device: row, deviceSignature });
405
- if (sharing) {
406
- birth.dispatch('mint-mesh-kind', { id: 'mesh', kind: 'shared' });
702
+ if (sharing || this._role === 'public') {
703
+ birth.dispatch('mint-mesh-kind', { id: 'mesh', kind: this._bornKind(project) });
407
704
  }
408
705
  if (row) {
409
706
  birth.dispatch('attest-device', this.attestDevice(deviceId));
410
707
  if (member) {
411
708
  birth.dispatch('mint-member', member);
412
709
  }
413
- if (!this._compute && !sharing) {
414
- birth.dispatch('seed-defaults', buildDefaultSeed(row, this._blindPeerKeys));
415
- birth.dispatch('put-agent', { agent: buildHealthAssistant(row) });
710
+ if (workspace) {
711
+ const { name, icon } = workspace;
712
+ birth.dispatch('put-workspace', {
713
+ workspace: { id: WORKSPACE_ROW, icon, updatedAt: row.joinedAt }
714
+ });
715
+ birth.dispatch('name-mesh', { name, namedAt: row.joinedAt });
716
+ }
717
+ if (project && member) {
718
+ birth.dispatch('put-project-member', {
719
+ memberId: member.member.id,
720
+ addedAt: project.upgraded ? undefined : row.joinedAt
721
+ });
722
+ birth.dispatch('name-mesh', { name: project.name, namedAt: row.joinedAt });
723
+ }
724
+ if (!sharing) {
725
+ this._seedDefaults(birth, row, defaults);
416
726
  }
417
727
  }
418
728
  await birth.flush();
419
729
  }
420
- else if (!this.autobee.writable && !vouched && (grantSecret || !sharing)) {
421
- const signature = this.sign(localKey);
422
- const device = row ? claimableDevice(row) : undefined;
423
- const join = this.batch();
424
- if (grantSecret) {
425
- const pair = grantKeyPair(grantSecret);
426
- join.dispatch('claim-invite', {
427
- signature,
428
- deviceId,
429
- device,
430
- deviceSignature,
431
- grantKey: pair.publicKey,
432
- inviteSignature: signInviteClaim(pair.secretKey, {
433
- meshPublicKey: this.publicKey,
434
- grantKey: pair.publicKey,
435
- deviceId,
436
- writerKey: localKey
437
- }),
438
- member: member?.member,
439
- memberClaim: member?.proof
440
- });
441
- }
442
- else if (recovery) {
443
- if (!device || !secretKey) {
444
- throw new Error('A recovery needs this device row and its key');
445
- }
446
- join.dispatch('recover-device', {
447
- signature,
448
- deviceId,
449
- device,
450
- deviceSignature,
451
- memberId: recovery.memberId,
452
- proof: await attestMemberClaim(claimAttestation(this.publicKey, deviceId, localKey), { publicKey: deviceId, secretKey }, recovery.proof)
453
- });
454
- }
455
- else {
456
- join.dispatch('join', {
457
- signature,
458
- deviceId,
459
- device,
460
- claimedMemberId: claim?.memberId,
461
- inviteToken: claim?.inviteNonce
462
- ? deviceInviteToken(claim.inviteNonce, deviceId)
463
- : undefined,
464
- deviceSignature
465
- });
466
- }
730
+ else if (!this.autobee.writable) {
731
+ const join = new MeshBatch(this.autobee, { role: this._role });
732
+ join.dispatch('open-join', {
733
+ signature: this.sign(localKey),
734
+ deviceId,
735
+ device: row ? claimableDevice(row) : undefined,
736
+ deviceSignature,
737
+ member: member?.member,
738
+ memberClaim: member?.proof
739
+ });
467
740
  if (row) {
468
741
  join.dispatch('attest-device', this.attestDevice(deviceId));
469
742
  }
470
- if (row && member && !grantSecret) {
471
- join.dispatch('join-member', member);
472
- }
473
743
  await join.flush({ optimistic: true });
474
744
  }
475
- await this.autobee.update();
476
- this.throwIfInterrupted();
745
+ }
746
+ _bornKind(project) {
747
+ if (project) {
748
+ return 'project';
749
+ }
750
+ return this._role === 'workspace' || this._role === 'public' ? this._role : 'shared';
751
+ }
752
+ _seedDefaults(birth, row, defaults) {
753
+ if (!inRole(this._role, '@qvac/seed-defaults')) {
754
+ for (const key of this._blindPeerKeys) {
755
+ birth.dispatch('put-mesh-blind-peer', { key });
756
+ }
757
+ return;
758
+ }
759
+ if (!defaults) {
760
+ return;
761
+ }
762
+ const peers = inRole(this._role, '@qvac/put-mesh-blind-peer') ? this._blindPeerKeys : [];
763
+ birth.dispatch('seed-defaults', buildDefaultSeed(row, peers));
764
+ birth.dispatch('put-agent', { agent: buildHealthAssistant(row) });
477
765
  }
478
766
  get interrupted() {
479
767
  return this.autobee.interrupted;
@@ -483,17 +771,15 @@ export class Mesh extends ReadyResource {
483
771
  throw this.interrupted;
484
772
  }
485
773
  }
486
- async waitForWritable(timeout) {
774
+ async waitForWritable() {
487
775
  if (this.closing || this.autobee.writable)
488
776
  return;
489
777
  await new Promise((resolve) => {
490
778
  const done = () => {
491
- clearTimeout(timer);
492
779
  this.autobee.off('writable', done);
493
780
  this.off('close', done);
494
781
  resolve();
495
782
  };
496
- const timer = timeout === undefined ? undefined : setTimeout(done, timeout);
497
783
  this.autobee.on('writable', done);
498
784
  this.on('close', done);
499
785
  });
@@ -507,17 +793,20 @@ export class Mesh extends ReadyResource {
507
793
  return Math.max(0, this.autobee.local.length - taken);
508
794
  }
509
795
  async suspend() {
796
+ this._background.pause();
510
797
  await this._files?.suspend();
511
- if (this._blindPeering) {
512
- await this._blindPeering.suspend();
513
- }
514
798
  }
515
- async resume() {
516
- if (this._blindPeering) {
517
- await this._blindPeering.resume();
518
- }
799
+ resume() {
800
+ this._background.resume();
519
801
  this._files?.resume();
520
802
  }
803
+ syncInBackground({ focus } = {}) {
804
+ return this._background.run(focus);
805
+ }
806
+ focusChat(chatId) {
807
+ this._background.focus(chatId);
808
+ }
809
+ pickedFrom = null;
521
810
  syncChatOrder = null;
522
811
  chatOrderBuilt = Promise.resolve();
523
812
  watch(listener) {
@@ -531,9 +820,9 @@ export class Mesh extends ReadyResource {
531
820
  return false;
532
821
  const snap = reference.db.snapshot();
533
822
  try {
534
- const trusted = await isOurTrustedWriter(snap, key, this._ourMemberId);
823
+ const trusted = await isTrustedWriter(snap, key, this);
535
824
  if (!trusted) {
536
- this._log?.debug(this._scope, 'writer is no device of ours', {
825
+ this._log?.debug(this._scope, 'writer is no live device of this mesh', {
537
826
  writerKey: b4a.toHex(key).slice(0, 16)
538
827
  });
539
828
  }
@@ -551,7 +840,8 @@ export class Mesh extends ReadyResource {
551
840
  for await (const writer of target.db.find(LIVE_WRITERS_BY_MOST_RECENT, {
552
841
  reverse: true
553
842
  })) {
554
- if (await isOurTrustedWriter(snap, writer.writerKey, this._ourMemberId)) {
843
+ if ((await isTrustedWriter(snap, writer.writerKey, this)) ||
844
+ (await this._vouchedByItsRow(target, reference, writer.writerKey))) {
555
845
  this._log?.debug(this._scope, 'vouching for a head', {
556
846
  writerKey: b4a.toHex(writer.writerKey).slice(0, 16),
557
847
  length: writer.length
@@ -559,27 +849,63 @@ export class Mesh extends ReadyResource {
559
849
  return { key: writer.writerKey, length: writer.length };
560
850
  }
561
851
  }
562
- this._log?.debug(this._scope, 'vouching for no head: no writer of ours has been active');
852
+ this._log?.debug(this._scope, 'vouching for no head: no live writer has been active');
563
853
  return null;
564
854
  }
565
855
  finally {
566
856
  await snap.close();
567
857
  }
568
858
  }
859
+ async _vouchedByItsRow(target, reference, writerKey) {
860
+ if (!reference ||
861
+ !this._trustedDevice ||
862
+ (await devicesByWriterKey(reference.db, writerKey).one())) {
863
+ return false;
864
+ }
865
+ const device = await liveDeviceForWriter(target.db, writerKey);
866
+ if (!device?.deviceSignature) {
867
+ return false;
868
+ }
869
+ const binding = {
870
+ meshPublicKey: this.publicKey,
871
+ deviceId: device.id,
872
+ writerKey,
873
+ joinedAt: device.joinedAt
874
+ };
875
+ if (!verifyWriterClaim(device.deviceSignature, binding) ||
876
+ !(await this._trustedDevice(device.id))) {
877
+ return false;
878
+ }
879
+ this._log?.debug(this._scope, 'writer vouched by its row', {
880
+ writerKey: b4a.toHex(writerKey).slice(0, 16),
881
+ deviceId: b4a.toHex(device.id).slice(0, 16)
882
+ });
883
+ return true;
884
+ }
569
885
  _autobeeOpen(bee) {
570
886
  const db = HyperDB.bee2(bee, MeshDefinition, { autoUpdate: true });
571
887
  return { db };
572
888
  }
573
889
  async _autobeeWarmup(view) {
890
+ let snap = null;
574
891
  try {
575
- const counts = await Promise.all(WARMUP_COLLECTIONS.map((collection) => drain(view.db.find(collection))));
892
+ const read = view.db.snapshot();
893
+ snap = read;
894
+ const counts = await Promise.all(WARMUP_COLLECTIONS.map((collection) => drain(read.find(collection))));
576
895
  const warmed = counts.reduce((total, count) => total + count, 0);
577
- this._log?.info(this._scope, 'warmed candidate view', { warmed });
896
+ this._log?.info(this._scope, 'warmed candidate view', {
897
+ warmed,
898
+ length: view.db.db.head()?.length ?? 0,
899
+ sinceOpenMs: this._sinceOpen()
900
+ });
578
901
  this.emit('warmup', { warmed });
579
902
  }
580
903
  catch (err) {
581
904
  this._log?.warn(this._scope, 'warmup failed', err);
582
905
  }
906
+ finally {
907
+ await snap?.close();
908
+ }
583
909
  }
584
910
  async _autobeeApply(nodes, view, host) {
585
911
  const tx = view.db.transaction();
@@ -625,28 +951,87 @@ export class Mesh extends ReadyResource {
625
951
  this._log?.warn(this._scope, 'halting apply: the mesh moved past this build', err);
626
952
  return host.interrupt(err);
627
953
  }
628
- async _autobeeUpdate(view, changes) {
629
- if (this.closing)
630
- return;
954
+ async _autobeeUpdate(_view, changes) {
631
955
  const change = changes.get('view');
632
- if (!change) {
956
+ if (this.closing || !change) {
633
957
  return;
634
958
  }
635
959
  const shared = change.shared;
636
960
  const from = shared?.key ? { key: shared.key, length: shared.length } : change.from;
961
+ await this._updates.push({
962
+ from,
963
+ to: change.to?.length ?? 0,
964
+ incremental: !!from?.length && isIncremental(change)
965
+ });
966
+ }
967
+ synced() {
968
+ return this._updates.flush();
969
+ }
970
+ async _syncUpdate({ from, to, incremental }) {
971
+ if (this.closing)
972
+ return null;
973
+ const started = Date.now();
974
+ let reached = null;
637
975
  try {
638
- await syncChanges(this, view, from);
976
+ if (from && incremental) {
977
+ reached = await syncChanges(this, this.autobee.view, from);
978
+ }
979
+ else {
980
+ this._arrive();
981
+ }
639
982
  if (this._admitted && !this.autobee.writable) {
640
983
  await this.checkOwnAdmission();
641
984
  }
642
985
  }
643
986
  catch (err) {
644
- this._log?.warn(this._scope, 'change sync failed', err);
987
+ if (!this.closing) {
988
+ this._log?.warn(this._scope, 'change sync failed', err);
989
+ }
990
+ }
991
+ this._log?.debug(this._scope, 'view synced', {
992
+ from: from?.length ?? 0,
993
+ to: reached?.length ?? to,
994
+ ms: Date.now() - started
995
+ });
996
+ return reached;
997
+ }
998
+ _arrive() {
999
+ this._log?.info(this._scope, 'landed on the view', {
1000
+ fastForwards: this.autobee.stats.fastForwards,
1001
+ ...this._lengths(),
1002
+ sinceOpenMs: this._sinceOpen()
1003
+ });
1004
+ this.emit('landed');
1005
+ this.emit('indexing-dirty');
1006
+ this.emit('agents-dirty');
1007
+ this.emit('skill-secrets-dirty');
1008
+ this.emit('meshes-dirty', true);
1009
+ void this._readLanding().catch((err) => {
1010
+ if (!this.closing) {
1011
+ this._log?.warn(this._scope, 'reading the landed view failed', err);
1012
+ }
1013
+ });
1014
+ }
1015
+ async _readLanding() {
1016
+ const started = Date.now();
1017
+ await Promise.all([this.refreshOurMember(), this.refreshMembership(), this.refreshName()]);
1018
+ if (this.closing) {
1019
+ return;
645
1020
  }
1021
+ await this.kickDirty();
1022
+ if (this._membership.unattested > 0) {
1023
+ this.emit('unattested-device');
1024
+ }
1025
+ this.emit('membership-tallied');
1026
+ this.emit('members-dirty');
1027
+ this.emit('mesh-named');
1028
+ this._log?.debug(this._scope, 'read the landed view', { ms: Date.now() - started });
646
1029
  }
647
1030
  async checkOwnAdmission() {
648
1031
  if (this.autobee.writable) {
649
1032
  this._admitted = true;
1033
+ this._refused = false;
1034
+ await this.kickDirty();
650
1035
  return;
651
1036
  }
652
1037
  if (!this._admitted || !this._deviceId) {
@@ -660,59 +1045,125 @@ export class Mesh extends ReadyResource {
660
1045
  return;
661
1046
  }
662
1047
  this._admitted = false;
663
- this.emit('kicked');
1048
+ this._refused = true;
1049
+ await this.kickDirty();
1050
+ }
1051
+ async kickDirty() {
1052
+ const seen = await this._kickState();
1053
+ if (seen === this._kickSeen || this.closing) {
1054
+ return;
1055
+ }
1056
+ this._kickSeen = seen;
1057
+ this.emit('kick-dirty');
1058
+ }
1059
+ async _kickState() {
1060
+ const writable = this.autobee.writable;
1061
+ const own = await this._ownRow();
1062
+ const member = this.ourMemberId;
1063
+ const devices = !writable && member && this._role === 'workspace'
1064
+ ? await devicesOfMember(this.view, member)
1065
+ : [];
1066
+ return JSON.stringify([
1067
+ writable,
1068
+ this._refused,
1069
+ member && b4a.toHex(member),
1070
+ own && [
1071
+ own.revoked,
1072
+ own.revokedBy && b4a.toHex(own.revokedBy),
1073
+ own.writerKey && b4a.toHex(own.writerKey)
1074
+ ],
1075
+ devices.map((device) => [b4a.toHex(device.id), device.revoked])
1076
+ ]);
1077
+ }
1078
+ async readKicked() {
1079
+ if (this.autobee.writable)
1080
+ return false;
1081
+ const own = await this._ownRow();
1082
+ if (own && isOwnKickedRow(this, own)) {
1083
+ return true;
1084
+ }
1085
+ return this._refused;
1086
+ }
1087
+ async checkResumedRefusal() {
1088
+ if (!(await this.ownWriterRefused())) {
1089
+ return false;
1090
+ }
1091
+ this._refused = true;
1092
+ return true;
664
1093
  }
665
1094
  async checkResumedRevocation() {
666
- const snap = this.view.snapshot();
667
- try {
668
- for await (const device of snap.find(DEVICES)) {
669
- if (!isOwnRevokedRow(this, device))
670
- continue;
671
- return isOwnKickedRow(this, device) ? 'kicked' : 'left';
672
- }
1095
+ const own = await this._ownRow();
1096
+ if (!own || !isOwnRevokedRow(this, own)) {
673
1097
  return null;
674
1098
  }
675
- finally {
676
- await snap.close();
677
- }
1099
+ return isOwnKickedRow(this, own) ? 'kicked' : 'left';
678
1100
  }
679
1101
  async ownWriterRefused() {
680
1102
  const info = await this.autobee.system.get(this.localWriterKey, { unflushed: true });
681
1103
  if (!info?.isRemoved) {
682
1104
  return false;
683
1105
  }
684
- for await (const device of this.view.find(DEVICES)) {
685
- if (device.writerKey && b4a.equals(device.writerKey, this.localWriterKey)) {
686
- return false;
687
- }
688
- }
689
- return true;
1106
+ return !(await liveDeviceForWriter(this.view, this.localWriterKey));
1107
+ }
1108
+ async _ownRow() {
1109
+ return this._deviceId ? this.view.get(DEVICES, { id: this._deviceId }) : null;
690
1110
  }
691
1111
  }
692
1112
  export class MeshBatch {
693
1113
  _autobee;
694
- _compute;
1114
+ _role;
1115
+ _joined;
1116
+ _watch;
695
1117
  _ops = [];
1118
+ _names = [];
696
1119
  constructor(autobee, opts = {}) {
697
1120
  this._autobee = autobee;
698
- this._compute = opts.compute ?? false;
1121
+ this._role = opts.role ?? 'account';
1122
+ this._joined = opts.joined ?? false;
1123
+ this._watch = opts.watch ?? ((_op, write) => write);
699
1124
  }
700
1125
  get size() {
701
1126
  return this._ops.length;
702
1127
  }
703
1128
  dispatch(name, payload) {
704
- assertComputeRoute(this._compute, name);
705
- this._ops.push(encode(`@qvac/${name}`, payload));
1129
+ assertRoleRoute(this._role, name);
1130
+ this._ops.push(encode(`@qvac/${name}`, versionedOp(name, payload)));
1131
+ this._names.push(name);
706
1132
  }
707
1133
  async flush(opts) {
708
1134
  if (this._ops.length === 0)
709
1135
  return;
1136
+ if (this._joined) {
1137
+ assertJoined(this._autobee, opts);
1138
+ }
710
1139
  const ops = this._ops;
1140
+ const names = this._names;
711
1141
  this._ops = [];
712
- await this._autobee.append(ops, opts);
1142
+ this._names = [];
1143
+ await this._watch(names.join(','), this._autobee.append(ops, opts));
713
1144
  }
714
1145
  }
715
1146
  function noop() { }
1147
+ function remoteReach(peer) {
1148
+ if (!isRecord(peer)) {
1149
+ return { length: 0, held: 0 };
1150
+ }
1151
+ return {
1152
+ length: typeof peer.remoteLength === 'number' ? peer.remoteLength : 0,
1153
+ held: typeof peer.remoteContiguousLength === 'number' ? peer.remoteContiguousLength : 0
1154
+ };
1155
+ }
1156
+ function viewCoreOf(head) {
1157
+ return isRecord(head) && b4a.isBuffer(head.key) ? b4a.toHex(head.key).slice(0, 8) : null;
1158
+ }
1159
+ function isIncremental(change) {
1160
+ return isRecord(change) && change.incremental === true;
1161
+ }
1162
+ function assertJoined(autobee, opts) {
1163
+ if (!opts?.optimistic && !autobee.writable) {
1164
+ throw new CoreError('MESH_STILL_JOINING');
1165
+ }
1166
+ }
716
1167
  function openedPart(part, name) {
717
1168
  if (part === null)
718
1169
  throw new Error(`mesh ${name} read before open`);
@@ -730,6 +1181,9 @@ function isInterrupt(err) {
730
1181
  return err instanceof Error && err.message === 'Apply interrupted';
731
1182
  }
732
1183
  function isFromANewerBuild(err) {
1184
+ if (isCoreError(err, 'MESH_RULES_FROM_A_NEWER_BUILD')) {
1185
+ return true;
1186
+ }
733
1187
  if (isRecord(err) && 'code' in err) {
734
1188
  return err.code === ERRORS.HANDLER_NOT_FOUND_BY_ID;
735
1189
  }