@qvac/core 1.0.0-rc.0 → 1.0.0-rc.10

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