@qvac/core 0.4.1 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (417) hide show
  1. package/CHANGELOG.md +262 -1
  2. package/README.md +1 -1
  3. package/dist-lib/lib/api/agents.js +3 -2
  4. package/dist-lib/lib/api/app-config.d.ts +1 -1
  5. package/dist-lib/lib/api/app-config.js +54 -15
  6. package/dist-lib/lib/api/attachments.d.ts +6 -5
  7. package/dist-lib/lib/api/attachments.js +4 -2
  8. package/dist-lib/lib/api/chat.d.ts +7 -3
  9. package/dist-lib/lib/api/chat.js +4 -4
  10. package/dist-lib/lib/api/chunks.js +3 -3
  11. package/dist-lib/lib/api/config.js +2 -2
  12. package/dist-lib/lib/api/current-mesh.d.ts +3 -4
  13. package/dist-lib/lib/api/current-mesh.js +9 -4
  14. package/dist-lib/lib/api/custom-models.d.ts +8 -1
  15. package/dist-lib/lib/api/custom-models.js +53 -16
  16. package/dist-lib/lib/api/devices.d.ts +14 -5
  17. package/dist-lib/lib/api/devices.js +14 -9
  18. package/dist-lib/lib/api/diagnostics.d.ts +1 -0
  19. package/dist-lib/lib/api/diagnostics.js +1 -1
  20. package/dist-lib/lib/api/effective-ctx-size.d.ts +6 -1
  21. package/dist-lib/lib/api/effective-ctx-size.js +5 -3
  22. package/dist-lib/lib/api/execution.js +9 -2
  23. package/dist-lib/lib/api/files.js +2 -1
  24. package/dist-lib/lib/api/index.d.ts +7 -1
  25. package/dist-lib/lib/api/index.js +101 -101
  26. package/dist-lib/lib/api/instrument.js +29 -20
  27. package/dist-lib/lib/api/knowledge.d.ts +6 -5
  28. package/dist-lib/lib/api/knowledge.js +8 -6
  29. package/dist-lib/lib/api/local-files.js +1 -1
  30. package/dist-lib/lib/api/logs.js +6 -3
  31. package/dist-lib/lib/api/members.js +22 -13
  32. package/dist-lib/lib/api/mesh.d.ts +7 -2
  33. package/dist-lib/lib/api/mesh.js +10 -5
  34. package/dist-lib/lib/api/model-download.js +1 -1
  35. package/dist-lib/lib/api/network.d.ts +1 -0
  36. package/dist-lib/lib/api/network.js +1 -1
  37. package/dist-lib/lib/api/notifications.js +23 -13
  38. package/dist-lib/lib/api/rag.d.ts +6 -1
  39. package/dist-lib/lib/api/rag.js +20 -11
  40. package/dist-lib/lib/api/range.d.ts +2 -2
  41. package/dist-lib/lib/api/sync.js +1 -1
  42. package/dist-lib/lib/api/tool-activity.js +7 -3
  43. package/dist-lib/lib/api/tool-configs.js +2 -4
  44. package/dist-lib/lib/api/upload-frames.d.ts +7 -8
  45. package/dist-lib/lib/api/upload-frames.js +2 -1
  46. package/dist-lib/lib/api/watchable.d.ts +6 -1
  47. package/dist-lib/lib/api/watchable.js +23 -14
  48. package/dist-lib/lib/assistant/fetch-body.js +4 -2
  49. package/dist-lib/lib/assistant/harness.d.ts +72 -24
  50. package/dist-lib/lib/assistant/harness.js +53 -15
  51. package/dist-lib/lib/assistant/history.d.ts +9 -7
  52. package/dist-lib/lib/assistant/history.js +77 -42
  53. package/dist-lib/lib/assistant/message.d.ts +5 -3
  54. package/dist-lib/lib/assistant/message.js +4 -2
  55. package/dist-lib/lib/assistant/skill-encode.d.ts +3 -1
  56. package/dist-lib/lib/assistant/skill-encode.js +52 -62
  57. package/dist-lib/lib/assistant-client.js +7 -4
  58. package/dist-lib/lib/assistant-tree.js +36 -11
  59. package/dist-lib/lib/assistant.d.ts +37 -12
  60. package/dist-lib/lib/assistant.js +977 -373
  61. package/dist-lib/lib/audio-format.d.ts +1 -1
  62. package/dist-lib/lib/audio-format.js +1 -1
  63. package/dist-lib/lib/chat-title.js +9 -4
  64. package/dist-lib/lib/client.js +3 -2
  65. package/dist-lib/lib/collabora/convert.d.ts +2 -2
  66. package/dist-lib/lib/collabora/convert.js +6 -3
  67. package/dist-lib/lib/collabora/entry.js +9 -5
  68. package/dist-lib/lib/collabora/protocol.d.ts +2 -2
  69. package/dist-lib/lib/collabora/protocol.js +14 -7
  70. package/dist-lib/lib/collabora/worker.js +14 -7
  71. package/dist-lib/lib/compaction.d.ts +5 -6
  72. package/dist-lib/lib/compaction.js +14 -7
  73. package/dist-lib/lib/constants.d.ts +2 -0
  74. package/dist-lib/lib/constants.js +4 -0
  75. package/dist-lib/lib/core/account-recovery.d.ts +4 -3
  76. package/dist-lib/lib/core/account-recovery.js +7 -4
  77. package/dist-lib/lib/core/agent.d.ts +0 -1
  78. package/dist-lib/lib/core/agent.js +18 -16
  79. package/dist-lib/lib/core/approval-rules.d.ts +7 -6
  80. package/dist-lib/lib/core/approval-rules.js +12 -6
  81. package/dist-lib/lib/core/archived-mesh.d.ts +7 -5
  82. package/dist-lib/lib/core/archived-mesh.js +25 -22
  83. package/dist-lib/lib/core/async-iterable.js +10 -5
  84. package/dist-lib/lib/core/attachment-link.d.ts +2 -2
  85. package/dist-lib/lib/core/audio-attachment.js +1 -1
  86. package/dist-lib/lib/core/blind-peers.d.ts +2 -2
  87. package/dist-lib/lib/core/chat-activity.d.ts +2 -2
  88. package/dist-lib/lib/core/chat-activity.js +42 -21
  89. package/dist-lib/lib/core/chat-group.d.ts +8 -6
  90. package/dist-lib/lib/core/chat-group.js +17 -10
  91. package/dist-lib/lib/core/chat-order.d.ts +11 -9
  92. package/dist-lib/lib/core/chat-order.js +1 -3
  93. package/dist-lib/lib/core/chat-search.d.ts +4 -4
  94. package/dist-lib/lib/core/chat-search.js +38 -24
  95. package/dist-lib/lib/core/chat.d.ts +20 -14
  96. package/dist-lib/lib/core/chat.js +42 -28
  97. package/dist-lib/lib/core/chunk.d.ts +20 -24
  98. package/dist-lib/lib/core/chunk.js +34 -22
  99. package/dist-lib/lib/core/collections.d.ts +69 -0
  100. package/dist-lib/lib/core/collections.js +69 -0
  101. package/dist-lib/lib/core/contract/activity.d.ts +8 -2
  102. package/dist-lib/lib/core/contract/activity.js +39 -11
  103. package/dist-lib/lib/core/contract/agents.js +3 -2
  104. package/dist-lib/lib/core/contract/approval-rules.js +1 -1
  105. package/dist-lib/lib/core/contract/attestations.js +1 -1
  106. package/dist-lib/lib/core/contract/chat-group.js +1 -1
  107. package/dist-lib/lib/core/contract/chat.js +1 -1
  108. package/dist-lib/lib/core/contract/chunks.js +16 -9
  109. package/dist-lib/lib/core/contract/compute.js +2 -1
  110. package/dist-lib/lib/core/contract/devices.js +10 -6
  111. package/dist-lib/lib/core/contract/grant-order.d.ts +5 -0
  112. package/dist-lib/lib/core/contract/grant-order.js +20 -0
  113. package/dist-lib/lib/core/contract/index.d.ts +1 -1
  114. package/dist-lib/lib/core/contract/index.js +27 -2
  115. package/dist-lib/lib/core/contract/indexing.js +3 -3
  116. package/dist-lib/lib/core/contract/invites.d.ts +16 -0
  117. package/dist-lib/lib/core/contract/invites.js +256 -0
  118. package/dist-lib/lib/core/contract/joined-meshes.js +22 -22
  119. package/dist-lib/lib/core/contract/knowledge.js +5 -5
  120. package/dist-lib/lib/core/contract/members.d.ts +15 -0
  121. package/dist-lib/lib/core/contract/members.js +116 -31
  122. package/dist-lib/lib/core/contract/mesh-kind.d.ts +6 -0
  123. package/dist-lib/lib/core/contract/mesh-kind.js +49 -0
  124. package/dist-lib/lib/core/contract/seal.js +1 -1
  125. package/dist-lib/lib/core/contract/seed-defaults.js +1 -3
  126. package/dist-lib/lib/core/contract/tool-activity.js +5 -5
  127. package/dist-lib/lib/core/contract/tool-configs.js +1 -2
  128. package/dist-lib/lib/core/contract/writers.d.ts +13 -0
  129. package/dist-lib/lib/core/contract/writers.js +181 -24
  130. package/dist-lib/lib/core/departures.js +16 -8
  131. package/dist-lib/lib/core/device-attestation.d.ts +5 -0
  132. package/dist-lib/lib/core/device-attestation.js +24 -0
  133. package/dist-lib/lib/core/device-invites.d.ts +2 -0
  134. package/dist-lib/lib/core/device-invites.js +29 -7
  135. package/dist-lib/lib/core/devices.d.ts +2 -1
  136. package/dist-lib/lib/core/devices.js +52 -2
  137. package/dist-lib/lib/core/document-attachment.d.ts +20 -11
  138. package/dist-lib/lib/core/document-attachment.js +10 -5
  139. package/dist-lib/lib/core/files/attachments.d.ts +2 -5
  140. package/dist-lib/lib/core/files/attachments.js +6 -3
  141. package/dist-lib/lib/core/files/index.d.ts +44 -52
  142. package/dist-lib/lib/core/files/index.js +142 -93
  143. package/dist-lib/lib/core/files/knowledge.d.ts +2 -5
  144. package/dist-lib/lib/core/files/local-index.js +9 -5
  145. package/dist-lib/lib/core/files/preview/image.d.ts +0 -1
  146. package/dist-lib/lib/core/files/preview/image.js +9 -6
  147. package/dist-lib/lib/core/files/preview/index.d.ts +8 -11
  148. package/dist-lib/lib/core/files/preview/index.js +20 -15
  149. package/dist-lib/lib/core/files/processing/claims.d.ts +6 -5
  150. package/dist-lib/lib/core/files/processing/claims.js +16 -8
  151. package/dist-lib/lib/core/files/processing/document.d.ts +21 -50
  152. package/dist-lib/lib/core/files/processing/index.d.ts +18 -13
  153. package/dist-lib/lib/core/files/processing/index.js +65 -37
  154. package/dist-lib/lib/core/files/processing/ownership.d.ts +8 -1
  155. package/dist-lib/lib/core/files/processing/ownership.js +1 -1
  156. package/dist-lib/lib/core/files/processing/protocol.d.ts +19 -19
  157. package/dist-lib/lib/core/files/processing/protocol.js +5 -5
  158. package/dist-lib/lib/core/files/processing/settle.d.ts +10 -7
  159. package/dist-lib/lib/core/files/processing/settle.js +22 -11
  160. package/dist-lib/lib/core/files/processing/source.js +14 -8
  161. package/dist-lib/lib/core/files/processing/state.d.ts +13 -11
  162. package/dist-lib/lib/core/files/processing/state.js +19 -12
  163. package/dist-lib/lib/core/files/rag/build.d.ts +2 -2
  164. package/dist-lib/lib/core/files/rag/build.js +2 -1
  165. package/dist-lib/lib/core/files/rag/document-types.d.ts +4 -6
  166. package/dist-lib/lib/core/files/rag/index.d.ts +10 -13
  167. package/dist-lib/lib/core/files/rag/index.js +3 -2
  168. package/dist-lib/lib/core/files/rag/load.d.ts +10 -9
  169. package/dist-lib/lib/core/files/rag/load.js +26 -13
  170. package/dist-lib/lib/core/files/rag/policy.js +2 -1
  171. package/dist-lib/lib/core/files/rag/produce.js +33 -19
  172. package/dist-lib/lib/core/files/rag/retrieval/full-text.d.ts +5 -3
  173. package/dist-lib/lib/core/files/rag/retrieval/full-text.js +30 -10
  174. package/dist-lib/lib/core/files/records/chunks.js +4 -2
  175. package/dist-lib/lib/core/files/records/knowledge.d.ts +10 -11
  176. package/dist-lib/lib/core/files/records/knowledge.js +9 -8
  177. package/dist-lib/lib/core/files/records/linkage.js +6 -3
  178. package/dist-lib/lib/core/files/records/output.d.ts +5 -4
  179. package/dist-lib/lib/core/files/records/output.js +8 -4
  180. package/dist-lib/lib/core/files/records/reclaim.js +41 -25
  181. package/dist-lib/lib/core/files/records/routing.d.ts +4 -6
  182. package/dist-lib/lib/core/files/records/routing.js +15 -8
  183. package/dist-lib/lib/core/files/records/source.d.ts +5 -4
  184. package/dist-lib/lib/core/files/records/source.js +20 -14
  185. package/dist-lib/lib/core/files/records/sweep.js +1 -3
  186. package/dist-lib/lib/core/files/store/blob.d.ts +10 -10
  187. package/dist-lib/lib/core/files/store/blob.js +25 -19
  188. package/dist-lib/lib/core/files/store/index.d.ts +1 -6
  189. package/dist-lib/lib/core/files/store/index.js +4 -4
  190. package/dist-lib/lib/core/files/store/inline.d.ts +2 -2
  191. package/dist-lib/lib/core/files/store/inline.js +6 -3
  192. package/dist-lib/lib/core/files/store/reference.d.ts +6 -9
  193. package/dist-lib/lib/core/files/store/reference.js +25 -17
  194. package/dist-lib/lib/core/files/store/server.d.ts +7 -6
  195. package/dist-lib/lib/core/files/store/server.js +22 -11
  196. package/dist-lib/lib/core/files/store/sweep.js +13 -7
  197. package/dist-lib/lib/core/files/upload-input.d.ts +4 -12
  198. package/dist-lib/lib/core/files/upload-input.js +10 -5
  199. package/dist-lib/lib/core/files/upload.d.ts +27 -24
  200. package/dist-lib/lib/core/files/upload.js +74 -51
  201. package/dist-lib/lib/core/fs-drives.js +4 -2
  202. package/dist-lib/lib/core/grant-expiry.d.ts +11 -0
  203. package/dist-lib/lib/core/grant-expiry.js +66 -0
  204. package/dist-lib/lib/core/handoff-channel.d.ts +2 -1
  205. package/dist-lib/lib/core/handoff-channel.js +36 -15
  206. package/dist-lib/lib/core/harness-loan.js +35 -17
  207. package/dist-lib/lib/core/helpers/index.d.ts +2 -1
  208. package/dist-lib/lib/core/helpers/index.js +11 -6
  209. package/dist-lib/lib/core/hosted-agents.js +19 -12
  210. package/dist-lib/lib/core/id.js +1 -1
  211. package/dist-lib/lib/core/identity.d.ts +1 -0
  212. package/dist-lib/lib/core/identity.js +11 -3
  213. package/dist-lib/lib/core/image/cache-sweep.d.ts +12 -10
  214. package/dist-lib/lib/core/image/cache-sweep.js +11 -6
  215. package/dist-lib/lib/core/image/normalize.d.ts +6 -0
  216. package/dist-lib/lib/core/image/normalize.js +12 -2
  217. package/dist-lib/lib/core/image/sniff.js +42 -21
  218. package/dist-lib/lib/core/inference-calls-receiver.js +7 -4
  219. package/dist-lib/lib/core/invite-grants.d.ts +20 -0
  220. package/dist-lib/lib/core/invite-grants.js +46 -0
  221. package/dist-lib/lib/core/invite.d.ts +3 -0
  222. package/dist-lib/lib/core/invite.js +6 -3
  223. package/dist-lib/lib/core/local/files.js +20 -19
  224. package/dist-lib/lib/core/local/index.d.ts +70 -45
  225. package/dist-lib/lib/core/local/index.js +157 -126
  226. package/dist-lib/lib/core/membership.d.ts +15 -2
  227. package/dist-lib/lib/core/membership.js +95 -20
  228. package/dist-lib/lib/core/mesh-seed-handoff.d.ts +12 -4
  229. package/dist-lib/lib/core/mesh-seed-handoff.js +23 -5
  230. package/dist-lib/lib/core/mesh.d.ts +57 -30
  231. package/dist-lib/lib/core/mesh.js +232 -78
  232. package/dist-lib/lib/core/migrations/agent-catalog.js +3 -2
  233. package/dist-lib/lib/core/migrations/agent-indexes.js +3 -3
  234. package/dist-lib/lib/core/migrations/device-indexes.d.ts +2 -0
  235. package/dist-lib/lib/core/migrations/device-indexes.js +7 -0
  236. package/dist-lib/lib/core/migrations/file-index.js +7 -4
  237. package/dist-lib/lib/core/migrations/index.d.ts +4 -3
  238. package/dist-lib/lib/core/migrations/index.js +3 -1
  239. package/dist-lib/lib/core/migrations/membership.js +9 -4
  240. package/dist-lib/lib/core/migrations/seal.js +3 -2
  241. package/dist-lib/lib/core/min-version.js +4 -2
  242. package/dist-lib/lib/core/next-frame.d.ts +5 -4
  243. package/dist-lib/lib/core/next-frame.js +6 -3
  244. package/dist-lib/lib/core/notification.d.ts +5 -1
  245. package/dist-lib/lib/core/notification.js +89 -77
  246. package/dist-lib/lib/core/ota-config.js +2 -1
  247. package/dist-lib/lib/core/pdf/rasterize.d.ts +18 -0
  248. package/dist-lib/lib/core/pdf/rasterize.js +57 -0
  249. package/dist-lib/lib/core/prompt-history.js +4 -2
  250. package/dist-lib/lib/core/range.d.ts +4 -4
  251. package/dist-lib/lib/core/range.js +12 -6
  252. package/dist-lib/lib/core/rank.d.ts +5 -4
  253. package/dist-lib/lib/core/rank.js +2 -1
  254. package/dist-lib/lib/core/send.d.ts +8 -5
  255. package/dist-lib/lib/core/send.js +9 -3
  256. package/dist-lib/lib/core/skill-overlay-receiver.js +7 -4
  257. package/dist-lib/lib/core/skill-secrets.js +16 -9
  258. package/dist-lib/lib/core/sync/chunks.js +2 -1
  259. package/dist-lib/lib/core/sync/devices.js +3 -1
  260. package/dist-lib/lib/core/sync/files.d.ts +2 -4
  261. package/dist-lib/lib/core/sync/files.js +28 -20
  262. package/dist-lib/lib/core/sync/index.d.ts +2 -5
  263. package/dist-lib/lib/core/sync/index.js +33 -17
  264. package/dist-lib/lib/core/sync-status.js +10 -7
  265. package/dist-lib/lib/core/system-info.js +4 -2
  266. package/dist-lib/lib/core/tool-activity.d.ts +4 -3
  267. package/dist-lib/lib/core/tool-activity.js +1 -1
  268. package/dist-lib/lib/core/tool-configs.js +12 -7
  269. package/dist-lib/lib/core.d.ts +107 -45
  270. package/dist-lib/lib/core.js +1765 -595
  271. package/dist-lib/lib/credential-pusher.d.ts +12 -17
  272. package/dist-lib/lib/credential-pusher.js +61 -42
  273. package/dist-lib/lib/custom-model-pusher.d.ts +7 -3
  274. package/dist-lib/lib/custom-model-pusher.js +15 -7
  275. package/dist-lib/lib/custom-model-task.d.ts +1 -0
  276. package/dist-lib/lib/custom-model-task.js +12 -0
  277. package/dist-lib/lib/deadline.js +4 -2
  278. package/dist-lib/lib/diagnostics.d.ts +1 -5
  279. package/dist-lib/lib/frame-readable.d.ts +3 -0
  280. package/dist-lib/lib/frame-readable.js +3 -0
  281. package/dist-lib/lib/fs-drive-bridge.js +24 -14
  282. package/dist-lib/lib/fs-grants.js +13 -8
  283. package/dist-lib/lib/guards.d.ts +3 -0
  284. package/dist-lib/lib/guards.js +9 -0
  285. package/dist-lib/lib/inference-calls.d.ts +28 -23
  286. package/dist-lib/lib/inference-calls.js +25 -13
  287. package/dist-lib/lib/integrations/mcp-providers.d.ts +2 -2
  288. package/dist-lib/lib/integrations/setup-routes.d.ts +2 -2
  289. package/dist-lib/lib/knowledge-search.d.ts +6 -5
  290. package/dist-lib/lib/knowledge-search.js +26 -18
  291. package/dist-lib/lib/kv-cache-reclaim.js +4 -2
  292. package/dist-lib/lib/kv-cache.js +8 -4
  293. package/dist-lib/lib/legacy-core-domain.d.ts +42 -42
  294. package/dist-lib/lib/log-buffer.js +2 -1
  295. package/dist-lib/lib/log.js +25 -15
  296. package/dist-lib/lib/mesh-scoped-engine.js +14 -4
  297. package/dist-lib/lib/model-catalog-revision.js +6 -3
  298. package/dist-lib/lib/model-clamp.js +6 -3
  299. package/dist-lib/lib/model-context.d.ts +8 -3
  300. package/dist-lib/lib/model-context.js +13 -5
  301. package/dist-lib/lib/model-download-status.js +4 -2
  302. package/dist-lib/lib/model-download.d.ts +12 -4
  303. package/dist-lib/lib/model-download.js +200 -103
  304. package/dist-lib/lib/model-idle-pusher.d.ts +10 -0
  305. package/dist-lib/lib/model-idle-pusher.js +41 -0
  306. package/dist-lib/lib/oauth/browser-connect.d.ts +10 -8
  307. package/dist-lib/lib/oauth/browser-connect.js +33 -20
  308. package/dist-lib/lib/oauth/client.d.ts +2 -2
  309. package/dist-lib/lib/oauth/flow-core.js +23 -9
  310. package/dist-lib/lib/oauth/flow.d.ts +1 -3
  311. package/dist-lib/lib/oauth/flow.js +9 -5
  312. package/dist-lib/lib/oauth/keys.d.ts +4 -4
  313. package/dist-lib/lib/oauth/keys.js +18 -12
  314. package/dist-lib/lib/oauth/loopback.d.ts +2 -2
  315. package/dist-lib/lib/oauth/loopback.js +147 -107
  316. package/dist-lib/lib/oauth/loopback.stub.d.ts +2 -2
  317. package/dist-lib/lib/oauth/loopback.stub.js +1 -1
  318. package/dist-lib/lib/oauth/pkce.d.ts +4 -2
  319. package/dist-lib/lib/oauth/pkce.js +10 -0
  320. package/dist-lib/lib/oauth/providers.js +5 -5
  321. package/dist-lib/lib/oauth/redirect-types.d.ts +12 -7
  322. package/dist-lib/lib/oauth/register.js +3 -2
  323. package/dist-lib/lib/oauth/registry.d.ts +2 -2
  324. package/dist-lib/lib/oauth/registry.js +2 -1
  325. package/dist-lib/lib/oauth/resolve.d.ts +30 -10
  326. package/dist-lib/lib/oauth/resolve.js +12 -6
  327. package/dist-lib/lib/oauth/token-exchange-core.d.ts +12 -4
  328. package/dist-lib/lib/oauth/token-exchange-core.js +1 -1
  329. package/dist-lib/lib/oauth/types.d.ts +12 -12
  330. package/dist-lib/lib/oauth.d.ts +4 -5
  331. package/dist-lib/lib/oauth.js +2 -3
  332. package/dist-lib/lib/reconnecting-watch.d.ts +1 -1
  333. package/dist-lib/lib/reconnecting-watch.js +6 -6
  334. package/dist-lib/lib/retry-budget.js +6 -3
  335. package/dist-lib/lib/run-notices.d.ts +2 -0
  336. package/dist-lib/lib/run-notices.js +7 -3
  337. package/dist-lib/lib/run-transcript.d.ts +2 -4
  338. package/dist-lib/lib/run-transcript.js +4 -2
  339. package/dist-lib/lib/skill-readiness.d.ts +1 -0
  340. package/dist-lib/lib/skill-readiness.js +17 -2
  341. package/dist-lib/lib/skill-types.d.ts +16 -16
  342. package/dist-lib/lib/suspend-lifecycle.d.ts +4 -4
  343. package/dist-lib/lib/suspend-lifecycle.js +10 -5
  344. package/dist-lib/lib/task.d.ts +5 -1
  345. package/dist-lib/lib/task.js +29 -12
  346. package/dist-lib/lib/unref.d.ts +3 -2
  347. package/dist-lib/lib/unref.js +2 -1
  348. package/dist-lib/lib/wav-duration.js +10 -5
  349. package/dist-lib/lib/write-queue.d.ts +5 -4
  350. package/dist-lib/lib/write-queue.js +2 -1
  351. package/dist-lib/schema/build.js +16 -8
  352. package/dist-lib/schema/config.d.ts +3 -4
  353. package/dist-lib/schema/config.js +1 -1
  354. package/dist-lib/schema/content.d.ts +18 -2
  355. package/dist-lib/schema/content.js +4 -1
  356. package/dist-lib/schema/engine.d.ts +3 -2
  357. package/dist-lib/schema/engine.js +192 -12
  358. package/dist-lib/schema/field-order.js +22 -11
  359. package/dist-lib/schema/indexes.d.ts +22 -0
  360. package/dist-lib/schema/{helpers.js → indexes.js} +19 -57
  361. package/dist-lib/schema/local.d.ts +3 -2
  362. package/dist-lib/schema/local.js +9 -3
  363. package/dist-lib/schema/media-kind.d.ts +8 -0
  364. package/dist-lib/schema/media-kind.js +52 -0
  365. package/dist-lib/schema/qvac.d.ts +12 -9
  366. package/dist-lib/schema/qvac.js +132 -7
  367. package/dist-lib/spec/bind.js +21 -0
  368. package/dist-lib/spec/calls.js +7 -0
  369. package/dist-lib/spec/capabilities.d.ts +18 -4
  370. package/dist-lib/spec/config/hyperconf/index.js +1 -1
  371. package/dist-lib/spec/hrpc/hrpc.json +93 -2
  372. package/dist-lib/spec/hrpc/index.js +89 -5
  373. package/dist-lib/spec/hrpc/messages.js +1568 -941
  374. package/dist-lib/spec/hyperschema/index.js +1568 -941
  375. package/dist-lib/spec/hyperschema/schema.json +531 -1
  376. package/dist-lib/spec/hyperschema/types.d.ts +145 -1
  377. package/dist-lib/spec/local/hyperdb/index.js +1 -1
  378. package/dist-lib/spec/local/hyperdb/messages.js +92 -20
  379. package/dist-lib/spec/local/hyperschema/index.js +47 -11
  380. package/dist-lib/spec/local/hyperschema/schema.json +32 -1
  381. package/dist-lib/spec/local/hyperschema/types.d.ts +7 -1
  382. package/dist-lib/spec/mesh/hyperdb/db.json +131 -3
  383. package/dist-lib/spec/mesh/hyperdb/index.js +548 -5
  384. package/dist-lib/spec/mesh/hyperdb/messages.js +2444 -893
  385. package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +37 -1
  386. package/dist-lib/spec/mesh/hyperdispatch/index.js +104 -2
  387. package/dist-lib/spec/mesh/hyperdispatch/messages.js +1035 -11
  388. package/dist-lib/spec/mesh/hyperschema/index.js +1035 -11
  389. package/dist-lib/spec/mesh/hyperschema/schema.json +553 -1
  390. package/dist-lib/spec/mesh/hyperschema/types.d.ts +143 -1
  391. package/package.json +11 -5
  392. package/tui/app.mjs +2624 -986
  393. package/tui/boot.mjs +17 -46
  394. package/tui/index.mjs +10 -5
  395. package/tui/lib/boot-timing.mjs +8 -8
  396. package/tui/lib/busy-words.mjs +21 -22
  397. package/tui/lib/export.mjs +100 -32
  398. package/tui/lib/knowledge-command.mjs +64 -19
  399. package/tui/lib/knowledge-import.mjs +52 -18
  400. package/tui/lib/render.mjs +65 -33
  401. package/tui/lib/save-file.mjs +30 -13
  402. package/tui/lib/select.mjs +23 -5
  403. package/tui/lib/selection.mjs +6 -4
  404. package/tui/lib/setup.mjs +11 -19
  405. package/tui/lib/sidebar.mjs +23 -13
  406. package/tui/lib/splash.mjs +6 -7
  407. package/tui/lib/upload-frames.mjs +1 -2
  408. package/tui/lib/usage.mjs +13 -9
  409. package/tui/lib/wrap.mjs +35 -6
  410. package/tui/main.mjs +54 -78
  411. package/worker/assistant-sidecar.mjs +5 -10
  412. package/worker/stow-entry.mjs +7 -4
  413. package/dist-lib/lib/custom-model-source.d.ts +0 -15
  414. package/dist-lib/lib/custom-model-source.js +0 -46
  415. package/dist-lib/lib/oauth/common.d.ts +0 -38
  416. package/dist-lib/lib/oauth/common.js +0 -34
  417. package/dist-lib/schema/helpers.d.ts +0 -32
@@ -7,6 +7,7 @@ import os from 'os';
7
7
  import path from 'path';
8
8
  import { mediaModelNameFor } from '@qvac/harness/lib/models/model-catalog.ts';
9
9
  import { connectHarness } from '@qvac/harness';
10
+ import * as z from 'zod';
10
11
  import { loanFailureNotice } from './core/harness-loan.js';
11
12
  import { SKILL_STATE } from '@qvac/harness/lib/tools/skills/skill-state.ts';
12
13
  import { toolsetEnabled } from '@qvac/harness/lib/tools/toolsets.ts';
@@ -25,6 +26,7 @@ import { attachTurnDocumentIds, formatRagContext, chooseRagNotice, RAG_KNOWLEDGE
25
26
  import { annotateDocumentAttachments, documentUnreadNotice, documentUnreadReason } from './core/document-attachment.js';
26
27
  import { imageCacheKey, imageCacheName, sweepImageCache } from './core/image/cache-sweep.js';
27
28
  import { IMAGE_MIMES, normalizeImageForModel } from './core/image/normalize.js';
29
+ import { MAX_PDF_PAGE_IMAGES, pdfRasterizer } from './core/pdf/rasterize.js';
28
30
  import { summarizeRuns } from './diagnostics.js';
29
31
  import { resolveFsGrants } from './fs-drive-bridge.js';
30
32
  import { InferenceCallStore } from './inference-calls.js';
@@ -41,11 +43,12 @@ import { RunTranscript } from './run-transcript.js';
41
43
  import { searchKnowledge } from './knowledge-search.js';
42
44
  import { isLifecycleTeardown, knownAbortOutcome, RUN_ABORT_REASONS, RUN_FAILURE_REASONS, runFailureNotice } from './run-notices.js';
43
45
  import { encodeAgentSkillReadiness } from './skill-readiness.js';
44
- import { harnessClearKvCache, harnessDeathReason, harnessDeleteModel, harnessDied, harnessListModels, harnessSkillCatalog, harnessSkillManagement, harnessSpeak, harnessSpeakStream, harnessTools, harnessTranscribe, imageDownloadProgress, modelCatalogChanged, toolReportEntry } from './assistant/harness.js';
46
+ import { harnessClearKvCache, harnessDeathReason, harnessDeleteModel, harnessDied, harnessListModels, harnessPreviewModelSource, harnessSkillCatalog, harnessSkillManagement, harnessDetectLanguage, harnessSpeak, harnessSpeakStream, harnessTools, harnessTranscribe, imageDownloadProgress, isHarnessEvent, modelCatalogChanged, toolReportEntry } from './assistant/harness.js';
45
47
  import { annotateUnreadableImages, audioTranscriptBlock, backfillDocumentOnlyTurn, backfillImageOnlyTurns, silentAudioNotice, unheardAudioNotice, nameSenders } from './assistant/message.js';
46
48
  import { drainBody, raceInactivity } from './assistant/fetch-body.js';
47
49
  import { GENERIC_ACKNOWLEDGEMENTS, HISTORY_TYPES, SKILL_TOOL, TERMINAL_STATES, couldNameSenders, historyFromChunks, inputAvailableToRun, listHistoryChunks, senderAttribution, senderNamesFrom, staleToolRunIds } from './assistant/history.js';
48
- export { harnessClearKvCache, harnessDeleteModel, harnessListModels, harnessSkillCatalog, harnessSkillManagement, harnessSpeak, harnessSpeakStream, harnessTools, harnessTranscribe } from './assistant/harness.js';
50
+ import { errorMessage, isRecord } from './guards.js';
51
+ export { harnessClearKvCache, harnessDeleteModel, harnessListModels, harnessPreviewModelSource, harnessSkillCatalog, harnessSkillManagement, harnessDetectLanguage, harnessSpeak, harnessSpeakStream, harnessTools, harnessTranscribe } from './assistant/harness.js';
49
52
  export {} from './assistant/skill-encode.js';
50
53
  export { annotateUnreadableImages, unheardAudioNotice, unreadableImagesNotice, DOCUMENT_ONLY_USER_PROMPT, IMAGE_AND_DOCUMENT_USER_PROMPT, IMAGE_ONLY_USER_PROMPT, nameSenders } from './assistant/message.js';
51
54
  export { couldNameSenders, historyFromChunks, listHistoryChunks, senderAttribution, senderNamesFrom, staleToolRunIds } from './assistant/history.js';
@@ -65,18 +68,50 @@ function settledWithin(write, ms) {
65
68
  function isDeniedResult(result) {
66
69
  if (!result || typeof result !== 'object')
67
70
  return false;
68
- return result.denied === true;
71
+ return 'denied' in result && result.denied === true;
69
72
  }
70
73
  const TITLE_RUN_ID = 'chat-title';
74
+ const RunOccupancySchema = z.object({
75
+ cacheTokens: z.number().optional().catch(undefined),
76
+ ctxSize: z.number().optional().catch(undefined)
77
+ });
71
78
  export const CAPTURE_INFERENCE_CALLS_KEY = 'inspector.captureInferenceCalls';
72
79
  export const CAPTURE_ON = 'on';
73
80
  export const CAPTURE_OFF = 'off';
81
+ function attachPages(turn, files) {
82
+ if (!files.length)
83
+ return;
84
+ turn.attachments = [...(turn.attachments ?? []), ...files.map((file) => ({ path: file }))];
85
+ }
86
+ function noBytes() {
87
+ return Promise.resolve(null);
88
+ }
89
+ function pdfPageCacheName(pdf, page) {
90
+ return imageCacheName({ blobId: pdf.blobId, fileName: `${pdf.fileName}.p${page}.jpg` });
91
+ }
92
+ async function cacheOrTouch(file, produce) {
93
+ const cached = await fs.promises.stat(file).catch(noop);
94
+ if (cached?.size) {
95
+ const now = new Date();
96
+ await fs.promises.utimes(file, now, now).catch(noop);
97
+ return true;
98
+ }
99
+ const bytes = await produce();
100
+ if (!bytes) {
101
+ return false;
102
+ }
103
+ const staging = `${file}.partial-${b4a.toHex(crypto.randomBytes(4))}`;
104
+ await fs.promises.writeFile(staging, bytes, { mode: 0o600 });
105
+ await fs.promises.rename(staging, file);
106
+ return true;
107
+ }
74
108
  const BLOB_READ_TIMEOUT_MS = 15_000;
75
109
  const SUSPENDED = RUN_ABORT_REASONS.suspended;
76
110
  const OWNERSHIP_LOST = RUN_ABORT_REASONS.ownershipLost;
77
111
  const RUN_SETTLED = RUN_ABORT_REASONS.runSettled;
78
112
  const RUNNER_DIED = RUN_ABORT_REASONS.runnerDied;
79
113
  const RUNNER_CLOSED = RUN_ABORT_REASONS.runnerClosed;
114
+ const MODEL_UNLOADED = RUN_ABORT_REASONS.modelUnloaded;
80
115
  function isSetUpHere(status) {
81
116
  return status === SKILL_STATE.ready || status === SKILL_STATE.incompatibleModel;
82
117
  }
@@ -88,6 +123,11 @@ function withConfigDefaults(agent, config) {
88
123
  return agent;
89
124
  return { ...agent, ctxSize: config.ctxSize };
90
125
  }
126
+ function thinkingDefault(config) {
127
+ if (!config)
128
+ return undefined;
129
+ return config.thinkingEnabled ? 'enabled' : 'disabled';
130
+ }
91
131
  const RAG_SOURCE_BOUNDS = {
92
132
  maxSources: MAX_RAG_SOURCES_PER_CHUNK,
93
133
  excerptChars: RAG_SOURCE_EXCERPT_CHARS
@@ -108,13 +148,17 @@ export class QvacAssistant extends ReadyResource {
108
148
  _preloadCurrentAgentModel;
109
149
  _drives;
110
150
  _blobReadTimeoutMs;
151
+ _pdfRasterizer;
111
152
  _warmAbort = new AbortController();
153
+ _unloading = new Map();
112
154
  _warmedModel = false;
113
155
  _warmedModelName = null;
114
156
  _modelStatusLogged = new Map();
115
157
  _deviceId;
116
158
  _info;
117
- _lastCapabilities;
159
+ _lastCapabilities = new Map();
160
+ _sharedMeshes = new Set();
161
+ _hostedIn = new Map();
118
162
  _readySkills;
119
163
  _recordingReadiness = false;
120
164
  _readinessDirty = false;
@@ -139,6 +183,7 @@ export class QvacAssistant extends ReadyResource {
139
183
  _inferenceCalls;
140
184
  _lifecycle;
141
185
  _audioTranscripts;
186
+ _pdfPages;
142
187
  _chats;
143
188
  _runImageCacheNames;
144
189
  _reconciledCompactions;
@@ -146,7 +191,7 @@ export class QvacAssistant extends ReadyResource {
146
191
  _modelCatalog = new ModelCatalogRevisionHub();
147
192
  _onHarnessStatus;
148
193
  _onModelCatalogEvent;
149
- constructor(engine, harness, { logger = null, imageCacheDir, inferenceCallsDir, imageCacheMaxBytes, imageCacheMaxAgeMs, downloadRetryMs, platform = os.platform(), preloadCurrentAgentModel = true, drives, borrowHarness, blobReadTimeoutMs = BLOB_READ_TIMEOUT_MS } = {}) {
194
+ constructor(engine, harness, { logger = null, imageCacheDir, inferenceCallsDir, imageCacheMaxBytes, imageCacheMaxAgeMs, downloadRetryMs, platform = os.platform(), preloadCurrentAgentModel = true, drives, borrowHarness, blobReadTimeoutMs = BLOB_READ_TIMEOUT_MS, pdfRasterizer: pdfRasterizerOption = pdfRasterizer } = {}) {
150
195
  super();
151
196
  this._engine = engine;
152
197
  this._harness = harness;
@@ -161,9 +206,9 @@ export class QvacAssistant extends ReadyResource {
161
206
  this._mobileClass = isMobilePlatform(platform);
162
207
  this._preloadCurrentAgentModel = preloadCurrentAgentModel;
163
208
  this._blobReadTimeoutMs = blobReadTimeoutMs;
209
+ this._pdfRasterizer = pdfRasterizerOption;
164
210
  this._deviceId = null;
165
211
  this._info = { toolNames: [], skills: [], supportsImages: false };
166
- this._lastCapabilities = null;
167
212
  this._readySkills = null;
168
213
  this._reconciling = false;
169
214
  this._capability = null;
@@ -184,6 +229,7 @@ export class QvacAssistant extends ReadyResource {
184
229
  this._inferenceCalls = new InferenceCallStore(inferenceCallsDir);
185
230
  this._lifecycle = new AbortController();
186
231
  this._audioTranscripts = new Map();
232
+ this._pdfPages = new Map();
187
233
  this._chats = new Map();
188
234
  this._runImageCacheNames = new Map();
189
235
  this._reconciledCompactions = new Map();
@@ -192,8 +238,9 @@ export class QvacAssistant extends ReadyResource {
192
238
  this._log?.warn('assistant', 'harness status', status);
193
239
  if (harnessDied(status)) {
194
240
  this._harnessDeath = harnessDeathReason(status);
195
- for (const run of this._runs.values())
241
+ for (const run of this._runs.values()) {
196
242
  run.controller.abort(RUNNER_DIED);
243
+ }
197
244
  }
198
245
  else {
199
246
  this._harnessDeath = null;
@@ -202,13 +249,20 @@ export class QvacAssistant extends ReadyResource {
202
249
  };
203
250
  this._onModelCatalogEvent = (state) => {
204
251
  this._logModelStatus(state);
205
- if (modelCatalogChanged(state))
252
+ if (modelCatalogChanged(state)) {
206
253
  this._modelCatalog.notify();
254
+ }
207
255
  const progress = imageDownloadProgress(state);
208
- if (progress)
256
+ if (progress) {
209
257
  this._downloads?.observeImageProgress(progress);
258
+ }
210
259
  };
211
260
  }
261
+ _requireDeviceId() {
262
+ if (!this._deviceId)
263
+ throw new Error('assistant device id read before open');
264
+ return this._deviceId;
265
+ }
212
266
  async _open() {
213
267
  this._log?.info('assistant', 'host platform resolved', {
214
268
  platform: this._platform,
@@ -247,21 +301,25 @@ export class QvacAssistant extends ReadyResource {
247
301
  void this._warmCurrentAgentModel().catch((err) => this._log?.warn('assistant', 'current agent model warm failed', err));
248
302
  }
249
303
  async _warmCurrentAgentModel() {
250
- if (this._warmedModel)
304
+ if (this._warmedModel || !this._deviceId)
251
305
  return;
252
306
  const deviceId = this._deviceId;
253
307
  const config = await this._engine.getConfig({}).catch(noop);
254
308
  const agent = await this._currentAgent(config?.activeAgentId ?? null, deviceId);
255
- if (!agent?.modelName || !agent.providerDeviceId?.equals(deviceId) || this.suspended)
309
+ if (!agent?.modelName ||
310
+ !agent.providerDeviceId ||
311
+ !b4a.equals(agent.providerDeviceId, deviceId) ||
312
+ this.suspended) {
256
313
  return;
314
+ }
257
315
  const model = runnableModelName(agent.modelName, await this._ensureCapability(), this._mobileClass);
258
- const rows = await this._harness.list?.({ cachedOnly: true }).catch(noop);
259
- if (!rows?.some((row) => row.name === model && row.isCached)) {
316
+ if (!(await this._isCached(model))) {
260
317
  this._log?.info('models', 'model warm skipped', { model, reason: 'not cached' });
261
318
  return;
262
319
  }
263
- if (this._warmedModel)
320
+ if (this._warmedModel) {
264
321
  return;
322
+ }
265
323
  this._warmedModel = true;
266
324
  this._warmedModelName = model;
267
325
  this._log?.info('models', 'model warm started', { model, agentModel: agent.modelName });
@@ -275,17 +333,20 @@ export class QvacAssistant extends ReadyResource {
275
333
  }
276
334
  async _currentAgent(activeAgentId, deviceId) {
277
335
  const meshId = this._accountMeshId;
278
- if (!meshId)
336
+ if (!meshId) {
279
337
  return null;
338
+ }
280
339
  if (activeAgentId) {
281
340
  const active = await this._engine.getAgent({ agentId: activeAgentId, meshId }).catch(noop);
282
- if (active)
341
+ if (active) {
283
342
  return active;
343
+ }
284
344
  }
285
345
  try {
286
346
  const own = await this._engine.getDefaultAgent({ meshId, providerDeviceId: deviceId });
287
- if (own.agent)
347
+ if (own.agent) {
288
348
  return own.agent;
349
+ }
289
350
  const any = await this._engine.getDefaultAgent({ meshId });
290
351
  return any.agent ?? null;
291
352
  }
@@ -329,8 +390,9 @@ export class QvacAssistant extends ReadyResource {
329
390
  try {
330
391
  const name = model || mediaModelNameFor(category, await this._persistedProfile());
331
392
  const config = await this._engine.getConfig({});
332
- if (!(config.pausedModels ?? []).includes(name))
393
+ if (!(config.pausedModels ?? []).includes(name)) {
333
394
  return true;
395
+ }
334
396
  await this._engine.setModelPaused({ name, paused: false });
335
397
  return true;
336
398
  }
@@ -343,8 +405,9 @@ export class QvacAssistant extends ReadyResource {
343
405
  await this._accountSeen.promise;
344
406
  const deviceId = this._deviceId;
345
407
  const meshId = this._accountMeshId;
346
- if (!deviceId || !meshId)
408
+ if (!deviceId || !meshId) {
347
409
  return DEFAULT_PROFILE;
410
+ }
348
411
  try {
349
412
  const self = await this._engine.getDevice({ meshId, deviceId });
350
413
  return self.capabilityProfile ?? DEFAULT_PROFILE;
@@ -361,7 +424,7 @@ export class QvacAssistant extends ReadyResource {
361
424
  maxBytes: this._imageCacheMaxBytes,
362
425
  maxAgeMs: this._imageCacheMaxAgeMs,
363
426
  inUse: (name) => this._imageCacheInUse(name),
364
- ...(keep ? { keep } : {})
427
+ keep: keep ?? undefined
365
428
  });
366
429
  }
367
430
  catch (err) {
@@ -371,24 +434,27 @@ export class QvacAssistant extends ReadyResource {
371
434
  async _sweepInferenceCalls() {
372
435
  try {
373
436
  const { removed } = await this._inferenceCalls.sweep();
374
- if (removed > 0)
437
+ if (removed > 0) {
375
438
  this._log?.info('assistant', 'inspector records swept', { removed });
439
+ }
376
440
  }
377
441
  catch (err) {
378
442
  this._log?.warn('assistant', 'inspector record sweep failed', err);
379
443
  }
380
444
  }
381
445
  _imageCacheInUse(name) {
382
- for (const names of this._runImageCacheNames.values())
446
+ for (const names of this._runImageCacheNames.values()) {
383
447
  if (names.has(name))
384
448
  return true;
449
+ }
385
450
  return false;
386
451
  }
387
452
  async _referencedImageNames() {
388
453
  try {
389
454
  const { files } = await this._engine.listLocalFiles({});
390
- if (!files?.length)
455
+ if (!files?.length) {
391
456
  return null;
457
+ }
392
458
  return new Set(files.map((file) => imageCacheKey(file.blobId)));
393
459
  }
394
460
  catch (err) {
@@ -477,29 +543,69 @@ export class QvacAssistant extends ReadyResource {
477
543
  this._readySkills,
478
544
  toolCatalog
479
545
  ]);
480
- if (key === this._lastCapabilities)
481
- return;
482
546
  const meshId = this._accountMeshId;
483
- if (!meshId)
547
+ if (!meshId) {
548
+ return;
549
+ }
550
+ const readySkillNames = this._readySkills;
551
+ const report = {
552
+ installedToolNames: this._info.toolNames,
553
+ installedSkillNames: this._info.skills,
554
+ installedModelNames,
555
+ cachedModelNames,
556
+ readySkillNames: readySkillNames ?? undefined,
557
+ toolCatalog: toolCatalog ?? undefined
558
+ };
559
+ await this._reportInto(meshId, key, report);
560
+ if (!readySkillNames) {
561
+ return;
562
+ }
563
+ for (const [shared, hosted] of this._hostedIn) {
564
+ if (shared === meshId)
565
+ continue;
566
+ await this._reportInto(shared, JSON.stringify([readySkillNames, hosted]), report);
567
+ }
568
+ }
569
+ async _reportInto(meshId, key, report) {
570
+ if (this._lastCapabilities.get(meshId) === key)
484
571
  return;
485
572
  try {
486
- await this._engine.saveDeviceCapabilities({
487
- meshId,
488
- installedToolNames: this._info.toolNames,
489
- installedSkillNames: this._info.skills,
490
- installedModelNames,
491
- cachedModelNames,
492
- ...(this._readySkills ? { readySkillNames: this._readySkills } : {}),
493
- ...(toolCatalog ? { toolCatalog } : {})
494
- });
495
- this._lastCapabilities = key;
573
+ await this._engine.saveDeviceCapabilities({ meshId, ...report });
574
+ this._lastCapabilities.set(meshId, key);
496
575
  }
497
576
  catch (err) {
498
- if (isMissingMesh(err))
577
+ if (isMissingMesh(err)) {
499
578
  return;
579
+ }
500
580
  this._log?.error('assistant', 'saveDeviceCapabilities failed', err);
501
581
  }
502
582
  }
583
+ _followHosted(meshId, agents) {
584
+ const deviceId = this._deviceId;
585
+ if (!deviceId || !this._sharedMeshes.has(meshId)) {
586
+ return;
587
+ }
588
+ const hosted = agents
589
+ .filter((agent) => !!agent.providerDeviceId && b4a.equals(agent.providerDeviceId, deviceId))
590
+ .map((agent) => agent.id)
591
+ .sort();
592
+ if (sameNameList(this._hostedIn.get(meshId) ?? [], hosted)) {
593
+ return;
594
+ }
595
+ if (hosted.length > 0) {
596
+ this._hostedIn.set(meshId, hosted);
597
+ }
598
+ else {
599
+ this._hostedIn.delete(meshId);
600
+ }
601
+ void this._reportHosted().catch(noop);
602
+ }
603
+ async _reportHosted() {
604
+ if (this._readySkills) {
605
+ await this._recordSkillReadiness();
606
+ }
607
+ await this._reportCapabilities();
608
+ }
503
609
  async _ensureCapability() {
504
610
  if (this._capability)
505
611
  return this._capability;
@@ -511,35 +617,39 @@ export class QvacAssistant extends ReadyResource {
511
617
  this._log?.warn('assistant', '_reconcileDeviceProfile: harness.capability failed', err);
512
618
  return null;
513
619
  }
514
- if (!probed?.recommendedModelName)
620
+ if (!probed?.recommendedModelName) {
515
621
  return null;
622
+ }
516
623
  this._capability = probed;
517
624
  return probed;
518
625
  }
519
626
  async _reconcileDeviceProfile() {
520
627
  const deviceId = this._deviceId;
521
628
  const meshId = this._accountMeshId;
522
- if (!deviceId || !meshId || this._reconciling)
629
+ if (!deviceId || !meshId || this._reconciling) {
523
630
  return;
631
+ }
524
632
  this._reconciling = true;
525
633
  try {
526
634
  const capability = await this._ensureCapability();
527
- if (!capability)
635
+ if (!capability) {
528
636
  return;
637
+ }
529
638
  const self = await this._engine.getDevice({ meshId, deviceId }).catch(noop);
530
- if (!self)
639
+ if (!self) {
531
640
  return;
641
+ }
532
642
  const profileUpdated = !(self.capabilityProfile === capability.capabilityProfile &&
533
643
  self.recommendedModelName === capability.recommendedModelName &&
534
644
  self.platform === this._platform);
535
645
  const profile = {
536
- publicKey: b4a.toString(deviceId, 'hex'),
646
+ publicKey: b4a.toHex(deviceId),
537
647
  name: self.name,
538
648
  platform: this._platform,
539
649
  mobileClass: this._mobileClass,
540
650
  capabilityProfile: capability.capabilityProfile,
541
651
  recommendedModelName: capability.recommendedModelName,
542
- ...(capability.memoryBudget ? { memoryBudget: capability.memoryBudget } : {})
652
+ memoryBudget: capability.memoryBudget
543
653
  };
544
654
  if (profileUpdated) {
545
655
  try {
@@ -567,8 +677,9 @@ export class QvacAssistant extends ReadyResource {
567
677
  this._harness.off?.('model', this._onModelCatalogEvent);
568
678
  this._warmAbort.abort(RUNNER_CLOSED);
569
679
  this._onlineBoot.destroy(new Error('assistant is closing'));
570
- for (const run of this._runs.values())
680
+ for (const run of this._runs.values()) {
571
681
  run.controller.abort(RUNNER_CLOSED);
682
+ }
572
683
  await this._downloads?.close();
573
684
  this._modelCatalog.close();
574
685
  this._meshList?.destroy();
@@ -587,18 +698,21 @@ export class QvacAssistant extends ReadyResource {
587
698
  }
588
699
  async suspend() {
589
700
  this._log?.info('assistant', 'suspend called');
590
- if (this._state === QvacState.SUSPEND)
701
+ if (this._state === QvacState.SUSPEND) {
591
702
  return;
592
- if (this.closing)
703
+ }
704
+ if (this.closing) {
593
705
  return;
706
+ }
594
707
  this._state = QvacState.SUSPEND;
595
708
  this._log?.info('assistant', 'suspending', { runs: this._runs.size });
596
709
  this._stopMeshIntake();
597
710
  await this._downloads?.suspend();
598
711
  this._meshList?.destroy();
599
712
  this._accountFrames?.destroy();
600
- for (const run of this._runs.values())
713
+ for (const run of this._runs.values()) {
601
714
  run.controller.abort(SUSPENDED);
715
+ }
602
716
  this._warmedModel = false;
603
717
  this._warmedModelName = null;
604
718
  this._modelCatalog.notify();
@@ -613,10 +727,12 @@ export class QvacAssistant extends ReadyResource {
613
727
  }
614
728
  async resume() {
615
729
  this._log?.info('assistant', 'resume called');
616
- if (this._state === QvacState.RESUME)
730
+ if (this._state === QvacState.RESUME) {
617
731
  return;
618
- if (this.closing)
732
+ }
733
+ if (this.closing) {
619
734
  return;
735
+ }
620
736
  if (this._state === QvacState.PRESUSPEND) {
621
737
  this._state = QvacState.RESUME;
622
738
  return;
@@ -625,8 +741,9 @@ export class QvacAssistant extends ReadyResource {
625
741
  this._log?.info('assistant', 'resuming');
626
742
  await Promise.allSettled([...this._runs.values()].map((run) => run.done));
627
743
  this._downloads?.resume();
628
- if (!this.opened)
744
+ if (!this.opened) {
629
745
  return;
746
+ }
630
747
  this._meshListWatch.start();
631
748
  this._accountWatch.start();
632
749
  }
@@ -634,14 +751,92 @@ export class QvacAssistant extends ReadyResource {
634
751
  return (await this._harness.list?.(options)) ?? [];
635
752
  }
636
753
  async deleteModel(name) {
637
- return (await this._harness.deleteModel?.(name)) ?? { name, removed: 0, freedBytes: 0 };
754
+ const removeFiles = async () => (await this._harness.deleteModel?.(name)) ?? { name, removed: 0, freedBytes: 0 };
755
+ let summary;
756
+ if (this._downloads) {
757
+ summary = await this._downloads.release(name, removeFiles);
758
+ }
759
+ else {
760
+ summary = await removeFiles();
761
+ await this._engine.setModelReleased({ name, released: true });
762
+ }
763
+ this._modelCatalog.notify();
764
+ this._reportCapabilities().catch(noop);
765
+ return summary;
638
766
  }
639
767
  modelCatalogWatch() {
640
768
  return this._modelCatalog.watch();
641
769
  }
770
+ async loadModel(name) {
771
+ if (!this._harness.loadModel) {
772
+ throw new Error('the model runtime cannot preload a model');
773
+ }
774
+ if (!(await this._isCached(name))) {
775
+ throw new Error(`${name} is not downloaded on this device`);
776
+ }
777
+ const config = await this._engine.getConfig({}).catch(noop);
778
+ const agent = await this._agentRunning(name, config?.activeAgentId ?? null);
779
+ const knobs = agent
780
+ ? withConfigDefaults(agent, config)
781
+ : { ctxSize: config?.ctxSize || undefined };
782
+ const { loaded, reason } = await this._harness.loadModel({ model: name, agent: knobs });
783
+ this._log?.info('models', 'model load requested', { model: name, loaded, reason });
784
+ return { loaded, reason };
785
+ }
786
+ async _isCached(model) {
787
+ const rows = await this._harness.list?.({ cachedOnly: true }).catch(noop);
788
+ return rows?.some((row) => row.name === model && row.isCached) === true;
789
+ }
790
+ async _agentRunning(model, activeAgentId) {
791
+ const deviceId = this._requireDeviceId();
792
+ const meshId = this._accountMeshId;
793
+ if (!meshId) {
794
+ return null;
795
+ }
796
+ const capability = await this._ensureCapability();
797
+ const runs = (agent) => runnableModelName(agent.modelName, capability, this._mobileClass) === model;
798
+ const active = await this._currentAgent(activeAgentId, deviceId);
799
+ if (active?.providerDeviceId && b4a.equals(active.providerDeviceId, deviceId) && runs(active)) {
800
+ return active;
801
+ }
802
+ const { agents } = await this._engine.listAgents({ meshId, providerDeviceId: deviceId });
803
+ return (agents ?? []).find(runs) ?? null;
804
+ }
805
+ unloadModel(name) {
806
+ const pending = this._unloading.get(name);
807
+ if (pending) {
808
+ return pending;
809
+ }
810
+ const unloading = Promise.resolve()
811
+ .then(() => this._unloadModel(name))
812
+ .finally(() => this._unloading.delete(name));
813
+ this._unloading.set(name, unloading);
814
+ return unloading;
815
+ }
816
+ async _unloadModel(name) {
817
+ if (!this._harness.unloadModel) {
818
+ throw new Error('the model runtime cannot unload a model');
819
+ }
820
+ const holders = [...this._runs.values()].filter((run) => run.executing && !run.borrowed && run.model === name);
821
+ for (const run of holders) {
822
+ run.controller.abort(MODEL_UNLOADED);
823
+ }
824
+ await Promise.allSettled(holders.map((run) => run.done));
825
+ const { unloaded } = await this._harness.unloadModel({ model: name });
826
+ this._log?.info('models', 'model unload requested', {
827
+ model: name,
828
+ unloaded,
829
+ requeued: holders.length
830
+ });
831
+ return { unloaded };
832
+ }
833
+ notifyModelCatalog() {
834
+ this._modelCatalog.notify();
835
+ }
642
836
  async retryModelDownload(name) {
643
- if (!this._downloads)
837
+ if (!this._downloads) {
644
838
  throw new Error('model download reconciler is unavailable');
839
+ }
645
840
  await this._downloads.retry(name);
646
841
  }
647
842
  async skills(agentId, modelName, options) {
@@ -651,7 +846,9 @@ export class QvacAssistant extends ReadyResource {
651
846
  const meshId = options?.meshId ?? this._accountMeshId;
652
847
  const agent = meshId ? await this._engine.getAgent({ agentId, meshId }).catch(noop) : null;
653
848
  const deviceId = this._deviceId;
654
- if (agent && deviceId && !agent.providerDeviceId?.equals(deviceId)) {
849
+ if (agent &&
850
+ deviceId &&
851
+ (!agent.providerDeviceId || !b4a.equals(agent.providerDeviceId, deviceId))) {
655
852
  agentId = null;
656
853
  options = { ...options, skipCredentialProbe: true };
657
854
  }
@@ -662,30 +859,17 @@ export class QvacAssistant extends ReadyResource {
662
859
  }
663
860
  async _recordSkillReadiness() {
664
861
  this._readinessDirty = true;
665
- if (this._recordingReadiness)
862
+ if (this._recordingReadiness) {
666
863
  return;
864
+ }
667
865
  this._recordingReadiness = true;
668
866
  try {
669
867
  while (this._readinessDirty) {
670
868
  this._readinessDirty = false;
671
- const unscoped = (await this._harness.skills?.(null, null, { skipCredentialProbe: true })) ?? [];
672
- const ready = unscoped
673
- .filter((skill) => isSetUpHere(skill.status))
674
- .map((skill) => skill.name);
675
- const deviceId = this._deviceId;
676
- const meshId = this._accountMeshId;
677
- if (deviceId && meshId) {
678
- const { agents } = await this._engine.listAgents({ meshId, providerDeviceId: deviceId });
679
- for (const agent of agents ?? []) {
680
- const resolved = (await this._harness.skills?.(agent.id, agent.modelName, {
681
- skipCredentialProbe: true
682
- })) ?? [];
683
- ready.push(...encodeAgentSkillReadiness(agent.id, resolved.filter((skill) => isSetUpHere(skill.status)).map((skill) => skill.name)));
684
- }
685
- }
686
- ready.sort();
687
- if (this._readySkills && sameNameList(this._readySkills, ready))
869
+ const ready = await this._readySkillNames();
870
+ if (this._readySkills && sameNameList(this._readySkills, ready)) {
688
871
  continue;
872
+ }
689
873
  this._readySkills = ready;
690
874
  await this._reportCapabilities();
691
875
  }
@@ -694,6 +878,29 @@ export class QvacAssistant extends ReadyResource {
694
878
  this._recordingReadiness = false;
695
879
  }
696
880
  }
881
+ async _readySkillNames() {
882
+ const unscoped = (await this._harness.skills?.(null, null, { skipCredentialProbe: true })) ?? [];
883
+ const ready = unscoped.filter((skill) => isSetUpHere(skill.status)).map((skill) => skill.name);
884
+ const deviceId = this._deviceId;
885
+ const meshId = this._accountMeshId;
886
+ if (deviceId && meshId) {
887
+ const { agents } = await this._engine.listAgents({ meshId, providerDeviceId: deviceId });
888
+ const hosted = await Promise.all([...this._hostedIn.keys()]
889
+ .filter((shared) => shared !== meshId)
890
+ .map((shared) => this._engine.listAgents({ meshId: shared, providerDeviceId: deviceId }).catch(noop)));
891
+ const seen = new Set();
892
+ for (const agent of [agents ?? [], ...hosted.map((list) => list?.agents ?? [])].flat()) {
893
+ if (seen.has(agent.id))
894
+ continue;
895
+ seen.add(agent.id);
896
+ const resolved = (await this._harness.skills?.(agent.id, agent.modelName, {
897
+ skipCredentialProbe: true
898
+ })) ?? [];
899
+ ready.push(...encodeAgentSkillReadiness(agent.id, resolved.filter((skill) => isSetUpHere(skill.status)).map((skill) => skill.name)));
900
+ }
901
+ }
902
+ return ready.sort();
903
+ }
697
904
  async tools() {
698
905
  return (await this._harness.tools?.()) ?? [];
699
906
  }
@@ -705,13 +912,15 @@ export class QvacAssistant extends ReadyResource {
705
912
  if (err !== null && !isMissingMesh(err)) {
706
913
  this._log?.error('assistant', `${label} watch failed`, err);
707
914
  }
708
- if (again)
915
+ if (again) {
709
916
  start();
917
+ }
710
918
  };
711
919
  const start = () => {
712
920
  again = false;
713
- if (this.closing || this.suspended)
921
+ if (this.closing || this.suspended) {
714
922
  return;
923
+ }
715
924
  if (running) {
716
925
  again = true;
717
926
  return;
@@ -748,13 +957,12 @@ export class QvacAssistant extends ReadyResource {
748
957
  if (this.closing || this.suspended)
749
958
  return;
750
959
  const agents = frame.agents ?? [];
960
+ this._followHosted(meshId, agents);
751
961
  const next = new Map(agents.map((agent) => [agent.id, agentRekeyFingerprint(agent)]));
752
- if (known !== null) {
753
- for (const agent of agents) {
754
- const previous = known.get(agent.id);
755
- if (previous !== undefined && previous !== next.get(agent.id)) {
756
- void this._reclaimAgentChatCaches(meshId, agent.id);
757
- }
962
+ for (const agent of agents) {
963
+ const previous = known?.get(agent.id);
964
+ if (previous !== undefined && previous !== next.get(agent.id)) {
965
+ void this._reclaimAgentChatCaches(meshId, agent.id);
758
966
  }
759
967
  }
760
968
  known = next;
@@ -764,8 +972,9 @@ export class QvacAssistant extends ReadyResource {
764
972
  try {
765
973
  const { chats } = await this._engine.listChats({ meshId });
766
974
  for (const chat of chats ?? []) {
767
- if (chat.agentId === agentId)
975
+ if (chat.agentId === agentId) {
768
976
  await this._deleteChatKvCache(chat.id, 'agent re-key');
977
+ }
769
978
  }
770
979
  }
771
980
  catch (err) {
@@ -773,25 +982,31 @@ export class QvacAssistant extends ReadyResource {
773
982
  }
774
983
  }
775
984
  async _reconcileCompaction(engine, chatId) {
776
- const deviceId = this._deviceId;
985
+ const deviceId = this._requireDeviceId();
777
986
  const { chunks } = await engine
778
987
  .listChunks({ chatId, type: 'compaction' })
779
988
  .catch(() => ({ chunks: [] }));
780
989
  let newest = null;
781
- for (const chunk of chunks ?? [])
782
- if (!newest || chunk.seq > newest.seq)
990
+ for (const chunk of chunks ?? []) {
991
+ if (!newest || chunk.seq > newest.seq) {
783
992
  newest = chunk;
784
- if (!newest || newest.originDeviceId?.equals(deviceId))
993
+ }
994
+ }
995
+ if (!newest || (newest.originDeviceId && b4a.equals(newest.originDeviceId, deviceId))) {
785
996
  return;
786
- if (this._reconciledCompactions.get(chatId) === newest.id)
997
+ }
998
+ if (this._reconciledCompactions.get(chatId) === newest.id) {
787
999
  return;
1000
+ }
788
1001
  const gone = await this._deleteChatKvCache(chatId, 'compaction from another device');
789
- if (gone)
1002
+ if (gone) {
790
1003
  this._reconciledCompactions.set(chatId, newest.id);
1004
+ }
791
1005
  }
792
1006
  async _senderNames(engine, chunks) {
793
- if (!couldNameSenders(chunks))
1007
+ if (!couldNameSenders(chunks)) {
794
1008
  return new Map();
1009
+ }
795
1010
  const stamped = chunks.every((chunk) => chunk.type !== 'user-message' || chunk.senderMemberId);
796
1011
  const [{ members }, { devices }] = await Promise.all([
797
1012
  engine.listMembers({}).catch(() => ({ members: [] })),
@@ -802,15 +1017,17 @@ export class QvacAssistant extends ReadyResource {
802
1017
  async _reconcileAttribution(chatId, senders) {
803
1018
  const attribution = senderAttribution(senders);
804
1019
  const seen = this._chatAttribution.get(chatId);
805
- if (seen === attribution)
1020
+ if (seen === attribution) {
806
1021
  return;
1022
+ }
807
1023
  if (seen === undefined && !attribution) {
808
1024
  this._chatAttribution.set(chatId, attribution);
809
1025
  return;
810
1026
  }
811
1027
  const gone = await this._deleteChatKvCache(chatId, 'sender attribution changed');
812
- if (gone)
1028
+ if (gone) {
813
1029
  this._chatAttribution.set(chatId, attribution);
1030
+ }
814
1031
  }
815
1032
  async deleteChatKvCache(chatId) {
816
1033
  return ((await this._harness.deleteKvCache?.(chatKvCacheKey(chatId))) ?? {
@@ -860,6 +1077,15 @@ export class QvacAssistant extends ReadyResource {
860
1077
  }
861
1078
  _syncMeshConsumers(meshes) {
862
1079
  const open = new Set(meshes.filter((mesh) => mesh.open).map((mesh) => mesh.meshId));
1080
+ this._sharedMeshes = new Set(meshes
1081
+ .filter((mesh) => mesh.open && !mesh.account && !mesh.compute)
1082
+ .map((mesh) => mesh.meshId));
1083
+ for (const meshId of this._hostedIn.keys()) {
1084
+ if (this._sharedMeshes.has(meshId))
1085
+ continue;
1086
+ this._hostedIn.delete(meshId);
1087
+ this._lastCapabilities.delete(meshId);
1088
+ }
863
1089
  for (const [meshId, consumer] of this._meshConsumers) {
864
1090
  if (open.has(meshId))
865
1091
  continue;
@@ -880,11 +1106,12 @@ export class QvacAssistant extends ReadyResource {
880
1106
  this._advertisedOn = null;
881
1107
  return;
882
1108
  }
883
- if (on === this._advertisedOn)
1109
+ if (on === this._advertisedOn) {
884
1110
  return;
1111
+ }
885
1112
  this._advertisedOn = on;
886
1113
  this._downloads?.meshAdopted();
887
- this._lastCapabilities = null;
1114
+ this._lastCapabilities.clear();
888
1115
  void this._reportCapabilities();
889
1116
  void this._reconcileDeviceProfile();
890
1117
  this._triggerModelWarm();
@@ -919,16 +1146,18 @@ export class QvacAssistant extends ReadyResource {
919
1146
  if (this.closing || this.suspended)
920
1147
  return;
921
1148
  const chunks = frame.chunks ?? [];
922
- for (const chunk of chunks)
1149
+ for (const chunk of chunks) {
923
1150
  this._dispatch(chunk, meshId);
1151
+ }
924
1152
  this._pruneSeen(meshId, new Set(chunks.map((chunk) => chunk.runId)));
925
1153
  }
926
1154
  }
927
1155
  _stopMeshIntake() {
928
1156
  this._meshListWatch.stop();
929
1157
  this._accountWatch.stop();
930
- for (const consumer of this._meshConsumers.values())
1158
+ for (const consumer of this._meshConsumers.values()) {
931
1159
  consumer.stop();
1160
+ }
932
1161
  this._meshConsumers.clear();
933
1162
  this._advertisedOn = null;
934
1163
  }
@@ -941,8 +1170,9 @@ export class QvacAssistant extends ReadyResource {
941
1170
  }
942
1171
  _lenderFor(agent) {
943
1172
  const lender = agent.lenderDeviceId;
944
- if (!lender || !this._borrowHarness)
1173
+ if (!lender || !this._borrowHarness) {
945
1174
  return null;
1175
+ }
946
1176
  const id = b4a.toHex(lender);
947
1177
  const own = this._deviceId;
948
1178
  return own && id === b4a.toHex(own) ? null : id;
@@ -950,41 +1180,47 @@ export class QvacAssistant extends ReadyResource {
950
1180
  _harnessFor(agent) {
951
1181
  const id = this._lenderFor(agent);
952
1182
  const borrow = this._borrowHarness;
953
- if (!id || !borrow)
1183
+ if (!id || !borrow) {
954
1184
  return this._harness;
1185
+ }
955
1186
  const held = this._borrowed.get(id);
956
- if (held)
1187
+ if (held) {
957
1188
  return held;
1189
+ }
958
1190
  const remote = connectHarness(() => borrow(id));
959
- remote.on('status', ((status) => {
960
- if (status.state === 'died')
1191
+ remote.on('status', (status) => {
1192
+ if (status.state === 'died') {
961
1193
  this._borrowed.delete(id);
962
- }));
1194
+ }
1195
+ });
963
1196
  const harness = remote;
964
1197
  this._borrowed.set(id, harness);
965
1198
  return harness;
966
1199
  }
967
1200
  _dispatch(request, meshId) {
968
1201
  const runId = request.runId;
969
- if (!runId || this._runs.has(runId) || this._seen.has(runId))
1202
+ if (!runId || this._runs.has(runId) || this._seen.has(runId)) {
970
1203
  return;
1204
+ }
971
1205
  this._seen.set(runId, meshId);
972
1206
  const engine = meshScoped(this._engine, meshId);
973
1207
  const settle = (async () => {
974
1208
  const agentId = request.runRequest?.agentId;
975
- const agent = await engine.getAgent({ agentId }).catch(noop);
1209
+ const agent = agentId ? await engine.getAgent({ agentId }).catch(noop) : undefined;
976
1210
  if (!agent) {
977
1211
  this._seen.delete(runId);
978
1212
  return;
979
1213
  }
980
- if (!agent.providerDeviceId?.equals(this._deviceId))
1214
+ if (!agent.providerDeviceId || !b4a.equals(agent.providerDeviceId, this._requireDeviceId())) {
981
1215
  return;
982
- if (await this._isHandled(engine, request))
1216
+ }
1217
+ if (await this._isHandled(engine, request)) {
983
1218
  return;
1219
+ }
984
1220
  this._log?.info('runs', 'run claimed', { runId, chatId: request.chatId, agentId });
985
1221
  const controller = new AbortController();
986
1222
  const prior = this._chats.get(request.chatId) ?? Promise.resolve();
987
- const done = prior.then(() => this._execute(request, agent, controller, meshId));
1223
+ const done = prior.then(() => this._execute(request, agent, { controller, meshId, runId }));
988
1224
  const link = done.catch(noop);
989
1225
  this._chats.set(request.chatId, link);
990
1226
  this._runs.set(runId, {
@@ -992,7 +1228,8 @@ export class QvacAssistant extends ReadyResource {
992
1228
  done,
993
1229
  chatId: request.chatId,
994
1230
  model: agent.modelName,
995
- executing: false
1231
+ executing: false,
1232
+ borrowed: false
996
1233
  });
997
1234
  try {
998
1235
  await done;
@@ -1001,17 +1238,23 @@ export class QvacAssistant extends ReadyResource {
1001
1238
  this._runImageCacheNames.delete(runId);
1002
1239
  this._runs.delete(runId);
1003
1240
  this._yieldDownloads();
1004
- if (this._chats.get(request.chatId) === link)
1241
+ if (this._chats.get(request.chatId) === link) {
1005
1242
  this._chats.delete(request.chatId);
1243
+ }
1006
1244
  void this._reportCapabilities();
1245
+ const requeued = controller.signal.reason === MODEL_UNLOADED && !this._seen.has(runId);
1246
+ if (requeued && !this.closing) {
1247
+ this._dispatch(request, meshId);
1248
+ }
1007
1249
  }
1008
1250
  })();
1009
1251
  settle.catch((err) => this._log?.error('assistant', `run ${runId} failed`, err));
1010
1252
  }
1011
1253
  async _citeWebSources(transcript, runId, result) {
1012
1254
  const records = webSourcesFromToolResult(result, WEB_RAG_SOURCE_BOUNDS);
1013
- if (records.length === 0)
1255
+ if (records.length === 0) {
1014
1256
  return;
1257
+ }
1015
1258
  try {
1016
1259
  await transcript.chunk({ type: 'sources', sources: { sources: records } });
1017
1260
  }
@@ -1019,7 +1262,7 @@ export class QvacAssistant extends ReadyResource {
1019
1262
  this._log?.warn('assistant', 'web sources chunk skipped', { runId, err });
1020
1263
  }
1021
1264
  }
1022
- async _searchKnowledge(engine, chatId, messages, attachmentIds, signal, preparing) {
1265
+ async _searchKnowledge(engine, messages, { chatId, attachmentIds, signal, preparing }) {
1023
1266
  let lastUser;
1024
1267
  for (let index = messages.length - 1; index >= 0; index--) {
1025
1268
  if (messages[index].role === 'user') {
@@ -1028,27 +1271,77 @@ export class QvacAssistant extends ReadyResource {
1028
1271
  }
1029
1272
  }
1030
1273
  const query = lastUser?.content.trim();
1031
- if (!lastUser || !query)
1274
+ if (!lastUser || !query) {
1032
1275
  return null;
1276
+ }
1033
1277
  try {
1034
1278
  const search = await searchKnowledge(engine, {
1035
1279
  chatId,
1036
1280
  query,
1037
1281
  attachmentIds: attachmentIds.length > 0 ? [...attachmentIds] : undefined
1038
1282
  }, { signal, preparing });
1039
- if (!search.enabled)
1283
+ if (!search.enabled) {
1040
1284
  return null;
1041
- if (!search.hasKnowledge)
1285
+ }
1286
+ if (!search.hasKnowledge) {
1042
1287
  return null;
1288
+ }
1043
1289
  return { search, lastUser, hits: search.hits ?? [] };
1044
1290
  }
1045
1291
  catch (err) {
1046
- if (err instanceof AbortedError)
1292
+ if (err instanceof AbortedError) {
1047
1293
  return null;
1294
+ }
1048
1295
  this._log?.warn('assistant', 'knowledge search unavailable', err);
1049
1296
  return null;
1050
1297
  }
1051
1298
  }
1299
+ async _injectKnowledge(knowledge, sinks) {
1300
+ const injected = { documentsInjected: false, sourcesWritten: false, ragInjected: false };
1301
+ try {
1302
+ if (knowledge.search.errorMessage) {
1303
+ this._log?.warn('assistant', 'knowledge search failed', {
1304
+ error: knowledge.search.errorMessage
1305
+ });
1306
+ }
1307
+ const fullTextOnly = knowledge.hits.length > 0 && knowledge.hits.every(isFullTextHit);
1308
+ if (fullTextOnly) {
1309
+ knowledge.lastUser.content = `${formatFullDocumentContext(knowledge.hits)}\n\nThe user's message:\n${knowledge.lastUser.content}`;
1310
+ injected.documentsInjected = true;
1311
+ if (knowledge.hits.some(isTruncatedFullTextHit)) {
1312
+ await sinks.transcript.chunk({
1313
+ type: 'sources',
1314
+ sources: { sources: [], notice: RAG_TRUNCATED_NOTICE }
1315
+ });
1316
+ injected.sourcesWritten = true;
1317
+ }
1318
+ }
1319
+ else {
1320
+ await injectRetrievedKnowledge(knowledge, { ...sinks, injected });
1321
+ }
1322
+ if (knowledge.search.pendingFiles?.length) {
1323
+ knowledge.lastUser.content += `\n\n[Files not yet available for this reply: ${knowledge.search.pendingFiles.join(', ')}.]`;
1324
+ }
1325
+ }
1326
+ catch (err) {
1327
+ this._log?.warn('assistant', 'knowledge injection skipped', err);
1328
+ }
1329
+ return injected;
1330
+ }
1331
+ async _writeUnreadNotice(transcript, reason) {
1332
+ try {
1333
+ await transcript.chunk({
1334
+ type: 'sources',
1335
+ sources: {
1336
+ sources: [],
1337
+ notice: reason === 'knowledge-off' ? RAG_KNOWLEDGE_OFF_NOTICE : RAG_UNAVAILABLE_NOTICE
1338
+ }
1339
+ });
1340
+ }
1341
+ catch (err) {
1342
+ this._log?.warn('assistant', 'unread-document notice skipped', err);
1343
+ }
1344
+ }
1052
1345
  async _isHandled(engine, request) {
1053
1346
  const { chunks } = await engine.listChunks({
1054
1347
  chatId: request.chatId,
@@ -1056,24 +1349,41 @@ export class QvacAssistant extends ReadyResource {
1056
1349
  });
1057
1350
  return (chunks ?? []).some((c) => c.runId === request.runId && TERMINAL_STATES.has(c.runStatus?.state ?? ''));
1058
1351
  }
1059
- async _execute(request, agent, controller, meshId) {
1352
+ async _execute(request, agent, { controller, meshId, runId }) {
1060
1353
  const engine = meshScoped(this._engine, meshId);
1061
1354
  const { chatId } = request;
1062
- const runId = request.runId;
1063
- const deviceId = this._deviceId;
1355
+ const deviceId = this._requireDeviceId();
1064
1356
  if (this.suspended) {
1065
1357
  this._seen.delete(runId);
1066
1358
  return;
1067
1359
  }
1068
- if (await this._isHandled(engine, request))
1360
+ if (await this._isHandled(engine, request)) {
1069
1361
  return;
1362
+ }
1070
1363
  const fresh = await engine.getAgent({ agentId: agent.id }).catch(noop);
1071
- if (!fresh || fresh.deletedAt || !fresh.providerDeviceId?.equals(deviceId))
1364
+ if (!fresh ||
1365
+ fresh.deletedAt ||
1366
+ !fresh.providerDeviceId ||
1367
+ !b4a.equals(fresh.providerDeviceId, deviceId)) {
1072
1368
  return;
1369
+ }
1073
1370
  agent = fresh;
1371
+ const capability = request.capability;
1372
+ if (capability) {
1373
+ return this._invokeCapability({
1374
+ engine,
1375
+ request,
1376
+ agent,
1377
+ capability,
1378
+ controller,
1379
+ meshId,
1380
+ runId
1381
+ });
1382
+ }
1074
1383
  await this._reconcileCompaction(engine, chatId);
1075
- if (request.parentId)
1384
+ if (request.parentId) {
1076
1385
  await this._deleteChatKvCache(chatId, 'superseding run');
1386
+ }
1077
1387
  const lender = this._lenderFor(agent);
1078
1388
  const model = lender
1079
1389
  ? agent.modelName
@@ -1085,9 +1395,10 @@ export class QvacAssistant extends ReadyResource {
1085
1395
  requested: agent.modelName,
1086
1396
  model
1087
1397
  });
1088
- const inflight = this._runs.get(runId);
1089
- if (inflight)
1090
- this._runs.set(runId, { ...inflight, model });
1398
+ }
1399
+ const claimed = this._runs.get(runId);
1400
+ if (claimed) {
1401
+ this._runs.set(runId, { ...claimed, model, borrowed: lender !== null });
1091
1402
  }
1092
1403
  const config = await engine.getConfig({}).catch(noop);
1093
1404
  const configuredAgent = withConfigDefaults(agent, config);
@@ -1120,7 +1431,7 @@ export class QvacAssistant extends ReadyResource {
1120
1431
  ? all.find((chunk) => chunk.id === currentRepair.parentId)
1121
1432
  : undefined;
1122
1433
  const acceptedRepairs = acceptedRepairCount(branches, runId);
1123
- const history = all.filter((chunk) => Boolean(chunk.compaction) ||
1434
+ const history = all.filter((chunk) => !!chunk.compaction ||
1124
1435
  (chunk.runId !== runId &&
1125
1436
  inputAvailableToRun(chunk, request.seq) &&
1126
1437
  (!repairParent || chunk.seq <= repairParent.seq || chunk.runId === repairParent.runId) &&
@@ -1139,8 +1450,9 @@ export class QvacAssistant extends ReadyResource {
1139
1450
  await this._reconcileAttribution(chatId, senders);
1140
1451
  }
1141
1452
  const messages = historyFromChunks(history, staleToolRunIds(history, priorAsks ?? [], runId), senders);
1142
- if (currentRepair)
1453
+ if (currentRepair) {
1143
1454
  messages.push({ role: 'user', content: currentRepair.promptText });
1455
+ }
1144
1456
  const diagramRepair = currentRepair !== undefined;
1145
1457
  const runAgent = diagramRepair
1146
1458
  ? {
@@ -1155,6 +1467,7 @@ export class QvacAssistant extends ReadyResource {
1155
1467
  ? this._harness.supportsImagesFor(model)
1156
1468
  : (this._harness.supportsImages ?? this._info.supportsImages) !== false;
1157
1469
  let unreadableImages = new Map();
1470
+ let renderedPdfIds = new Set();
1158
1471
  if (!seesImages) {
1159
1472
  const dropped = messages.reduce((count, turn) => count + (turn.images?.length ?? 0), 0);
1160
1473
  if (dropped && (this._harness.supportsImages ?? this._info.supportsImages)) {
@@ -1166,17 +1479,19 @@ export class QvacAssistant extends ReadyResource {
1166
1479
  turn.content += `\n[Attached image${turn.images.length > 1 ? 's' : ''}: ${names} — not visible to this model]`;
1167
1480
  }
1168
1481
  delete turn.images;
1482
+ delete turn.pdfs;
1169
1483
  }
1170
1484
  }
1171
1485
  else {
1172
1486
  unreadableImages = await this._hydrateImages(messages, controller.signal, runId);
1173
1487
  }
1174
- const settledWatch = this._watchSettled(engine, chatId, runId, controller);
1488
+ const settledWatch = this._watchSettled(engine, { chatId, runId, controller });
1175
1489
  const ownershipWatch = this._watchOwnership(engine, agent.id, controller);
1176
1490
  let readinessError = null;
1177
1491
  try {
1178
- if (!lender)
1492
+ if (!lender) {
1179
1493
  await this._downloads?.waitUntilCached(model, controller.signal);
1494
+ }
1180
1495
  }
1181
1496
  catch (err) {
1182
1497
  readinessError = err instanceof Error ? err : new Error(String(err));
@@ -1184,8 +1499,9 @@ export class QvacAssistant extends ReadyResource {
1184
1499
  if (controller.signal.aborted) {
1185
1500
  try {
1186
1501
  const outcome = this._abortOutcome(runId, controller.signal.reason);
1187
- if (outcome.kind === 'replay')
1502
+ if (outcome.kind === 'replay') {
1188
1503
  this._seen.delete(runId);
1504
+ }
1189
1505
  if (outcome.kind === 'failed') {
1190
1506
  await transcript.status({ state: 'failed', reason: outcome.reason });
1191
1507
  }
@@ -1209,21 +1525,27 @@ export class QvacAssistant extends ReadyResource {
1209
1525
  return;
1210
1526
  }
1211
1527
  const inflight = this._runs.get(runId);
1212
- if (inflight)
1528
+ if (inflight) {
1213
1529
  this._runs.set(runId, { ...inflight, executing: true });
1530
+ }
1214
1531
  this._yieldDownloads();
1215
1532
  const busyChatId = this._modelHolder(runId, chatId, model);
1216
1533
  const executing = await transcript.status(busyChatId
1217
1534
  ? { state: 'executing', progress: { phase: 'queued', busyChatId } }
1218
1535
  : { state: 'executing' });
1219
1536
  const runStatusId = executing.id;
1220
- const approvals = this._createApprove(engine, chatId, runId, deviceId, agent.id, () => ran);
1537
+ const approvals = this._createApprove(engine, {
1538
+ chatId,
1539
+ runId,
1540
+ deviceId,
1541
+ agentId: agent.id,
1542
+ hasRun: () => ran
1543
+ });
1221
1544
  const modelWatch = this._watchModelProgress((runStatus) => transcript.progress(runStatusId, runStatus), busyChatId !== null);
1222
1545
  let metrics;
1223
1546
  const sourceTagFilter = createRagSourceTagFilter();
1224
1547
  const retrievedRecords = [];
1225
1548
  let answerText = '';
1226
- let ragInjected = false;
1227
1549
  const flushTranscript = async () => {
1228
1550
  const tail = sourceTagFilter.flush();
1229
1551
  if (tail) {
@@ -1235,13 +1557,74 @@ export class QvacAssistant extends ReadyResource {
1235
1557
  let titleOutcome = null;
1236
1558
  let runFailure = null;
1237
1559
  let runVerdict = 'completed';
1560
+ const writeText = async (type, raw) => {
1561
+ const text = type === 'content' ? sourceTagFilter.push(raw) : raw;
1562
+ if (!text) {
1563
+ return;
1564
+ }
1565
+ if (type === 'content') {
1566
+ answerText += text;
1567
+ }
1568
+ await transcript.text(type, text);
1569
+ };
1570
+ const failRun = async (message) => {
1571
+ const folded = isContextOverflowFailure(message) &&
1572
+ (await this._runCompaction({
1573
+ chatId,
1574
+ transcript,
1575
+ trigger: 'overflow',
1576
+ signal: controller.signal,
1577
+ meshId
1578
+ }));
1579
+ runFailure = runFailureNotice(message);
1580
+ await transcript.status({ state: 'failed', reason: runFailure, metrics });
1581
+ if (folded) {
1582
+ await engine
1583
+ .runAgent({ chatId, agentId: agent.id, parentRunId: runId })
1584
+ .catch((err) => this._log?.warn('runs', 'compaction retry not queued', { runId, err }));
1585
+ }
1586
+ titleOutcome = 'ended';
1587
+ };
1588
+ const endAbortedRun = async () => {
1589
+ const outcome = this._abortOutcome(runId, controller.signal.reason);
1590
+ if (outcome.kind === 'replay') {
1591
+ runVerdict = 'replay';
1592
+ this._seen.delete(runId);
1593
+ return;
1594
+ }
1595
+ if (outcome.kind === 'handoff') {
1596
+ runVerdict = 'handoff';
1597
+ return;
1598
+ }
1599
+ runFailure = outcome.reason;
1600
+ await transcript.status({ state: 'failed', reason: outcome.reason, metrics });
1601
+ titleOutcome = 'ended';
1602
+ };
1603
+ const recordToolResult = async (event) => {
1604
+ if (isDeniedResult(event.result)) {
1605
+ this._log?.info('approvals', 'tool ran denied', { runId, tool: event.name });
1606
+ }
1607
+ else if (event.name !== SKILL_TOOL) {
1608
+ ran = true;
1609
+ }
1610
+ if (WEB_TOOLS.includes(String(event.name))) {
1611
+ await this._citeWebSources(transcript, runId, event.result);
1612
+ }
1613
+ await transcript.chunk(toolResultChunk(event));
1614
+ };
1238
1615
  try {
1239
1616
  const attachTurnIds = attachTurnDocumentIds(history);
1240
- if (attachTurnIds.length > 0)
1617
+ if (attachTurnIds.length > 0) {
1241
1618
  backfillDocumentOnlyTurn(messages);
1619
+ }
1242
1620
  const knowledge = !diagramRepair &&
1243
1621
  (toolsetEnabled('knowledge', runAgent.disabledToolsets) || attachTurnIds.length > 0)
1244
- ? await this._searchKnowledge(engine, chatId, messages, attachTurnIds, controller.signal, modelWatch.documents)
1622
+ ? await this._searchKnowledge(engine, messages, {
1623
+ chatId,
1624
+ attachmentIds: attachTurnIds,
1625
+ signal: controller.signal,
1626
+ preparing: modelWatch.documents
1627
+ })
1245
1628
  : null;
1246
1629
  if (controller.signal.aborted) {
1247
1630
  const outcome = this._abortOutcome(runId, controller.signal.reason);
@@ -1258,97 +1641,35 @@ export class QvacAssistant extends ReadyResource {
1258
1641
  await transcript.status({ state: 'failed', reason: outcome.reason, metrics });
1259
1642
  return;
1260
1643
  }
1261
- let documentsInjected = false;
1262
- let sourcesWritten = false;
1263
- if (knowledge) {
1264
- try {
1265
- if (knowledge.search.errorMessage) {
1266
- this._log?.warn('assistant', 'knowledge search failed', {
1267
- error: knowledge.search.errorMessage
1268
- });
1269
- }
1270
- const fullTextOnly = knowledge.hits.length > 0 && knowledge.hits.every(isFullTextHit);
1271
- if (fullTextOnly) {
1272
- knowledge.lastUser.content = `${formatFullDocumentContext(knowledge.hits)}\n\nThe user's message:\n${knowledge.lastUser.content}`;
1273
- documentsInjected = true;
1274
- if (knowledge.hits.some(isTruncatedFullTextHit)) {
1275
- await transcript.chunk({
1276
- type: 'sources',
1277
- sources: { sources: [], notice: RAG_TRUNCATED_NOTICE }
1278
- });
1279
- sourcesWritten = true;
1280
- }
1281
- }
1282
- else {
1283
- const records = sourcesFromHits(knowledge.hits, RAG_SOURCE_BOUNDS);
1284
- const notice = chooseRagNotice({
1285
- errorMessage: knowledge.search.errorMessage,
1286
- hitCount: records.length,
1287
- searchedCount: knowledge.search.searchedCount ?? 0,
1288
- indexEmpty: Boolean(knowledge.search.indexEmpty),
1289
- hasKnowledge: Boolean(knowledge.search.hasKnowledge),
1290
- pendingReferenced: Boolean(knowledge.search.pendingReferenced),
1291
- failedFiles: knowledge.search.failedFiles ?? [],
1292
- pendingFiles: knowledge.search.pendingFiles ?? []
1293
- });
1294
- sourceTagFilter.add(records.map((record) => record.title));
1295
- retrievedRecords.push(...records.map((record, index) => ({
1296
- ...record,
1297
- excerpt: knowledge.hits[index]?.content ?? record.excerpt
1298
- })));
1299
- await transcript.chunk({
1300
- type: 'sources',
1301
- sources: { sources: records, notice: notice ?? undefined }
1302
- });
1303
- sourcesWritten = true;
1304
- if (records.length > 0) {
1305
- knowledge.lastUser.content = `${formatRagContext(knowledge.hits, knowledge.search.availableFiles ?? [])}\n\nThe user's message:\n${knowledge.lastUser.content}\n\n${RAG_CITATION_LINE}`;
1306
- ragInjected = true;
1307
- documentsInjected = true;
1308
- }
1309
- else if (knowledge.search.availableFiles?.length) {
1310
- knowledge.lastUser.content += `\n\n[Files indexed and available now: ${knowledge.search.availableFiles.join(', ')}. No relevant excerpts were retrieved for this question. Their current availability supersedes earlier indexing notices.]`;
1311
- }
1312
- }
1313
- if (knowledge.search.pendingFiles?.length) {
1314
- knowledge.lastUser.content += `\n\n[Files not yet available for this reply: ${knowledge.search.pendingFiles.join(', ')}.]`;
1315
- }
1316
- }
1317
- catch (err) {
1318
- this._log?.warn('assistant', 'knowledge injection skipped', err);
1319
- }
1320
- }
1644
+ const injected = knowledge
1645
+ ? await this._injectKnowledge(knowledge, { transcript, sourceTagFilter, retrievedRecords })
1646
+ : { documentsInjected: false, sourcesWritten: false, ragInjected: false };
1321
1647
  const hasAudio = messages.some((turn) => turn.audioAttachments?.length);
1322
- if (hasAudio)
1648
+ if (hasAudio) {
1323
1649
  modelWatch.transcribe(true);
1650
+ }
1324
1651
  await this._hydrateAudio(messages, controller.signal);
1325
- if (hasAudio)
1652
+ if (hasAudio) {
1326
1653
  modelWatch.transcribe(false);
1654
+ }
1327
1655
  backfillImageOnlyTurns(messages);
1656
+ if (seesImages) {
1657
+ renderedPdfIds = await this._hydratePdfPages(messages, controller.signal, runId);
1658
+ }
1328
1659
  annotateDocumentAttachments(messages);
1329
- if (!diagramRepair && attachTurnIds.length > 0 && !documentsInjected) {
1660
+ const unreadIds = attachTurnIds.filter((id) => !renderedPdfIds.has(id));
1661
+ if (!diagramRepair && unreadIds.length > 0 && !injected.documentsInjected) {
1330
1662
  const reason = documentUnreadReason({
1331
1663
  knowledgeOn: toolsetEnabled('knowledge', runAgent.disabledToolsets),
1332
1664
  failedFiles: knowledge?.search?.failedFiles,
1333
- attachedCount: attachTurnIds.length
1665
+ attachedCount: unreadIds.length
1334
1666
  });
1335
1667
  const lastUser = messages.findLast((turn) => turn.role === 'user');
1336
1668
  if (lastUser) {
1337
- lastUser.content = `${lastUser.content}\n${documentUnreadNotice(reason, attachTurnIds.length > 1)}`;
1669
+ lastUser.content = `${lastUser.content}\n${documentUnreadNotice(reason, unreadIds.length > 1)}`;
1338
1670
  }
1339
- if (!sourcesWritten) {
1340
- try {
1341
- await transcript.chunk({
1342
- type: 'sources',
1343
- sources: {
1344
- sources: [],
1345
- notice: reason === 'knowledge-off' ? RAG_KNOWLEDGE_OFF_NOTICE : RAG_UNAVAILABLE_NOTICE
1346
- }
1347
- });
1348
- }
1349
- catch (err) {
1350
- this._log?.warn('assistant', 'unread-document notice skipped', err);
1351
- }
1671
+ if (!injected.sourcesWritten) {
1672
+ await this._writeUnreadNotice(transcript, reason);
1352
1673
  }
1353
1674
  }
1354
1675
  annotateUnreadableImages(unreadableImages);
@@ -1356,11 +1677,16 @@ export class QvacAssistant extends ReadyResource {
1356
1677
  const { roots, write } = await fsGrantsPromise;
1357
1678
  const { grantedDirs, grantedDrives, driveOp } = await resolveFsGrants(roots, this._drives, (key, err) => this._log?.warn('assistant', 'drive grant dropped', {
1358
1679
  key,
1359
- error: err instanceof Error ? err.message : String(err)
1680
+ error: errorMessage(err)
1360
1681
  }));
1361
- if (await this._isHandled(engine, request))
1682
+ if (await this._isHandled(engine, request)) {
1362
1683
  return;
1684
+ }
1363
1685
  const cacheKey = diagramRepair ? undefined : chatRepairKvCacheKey(chatId, acceptedRepairs);
1686
+ const unloading = lender ? undefined : this._unloading.get(model);
1687
+ if (unloading) {
1688
+ await raceAbort(unloading.catch(noop), controller.signal);
1689
+ }
1364
1690
  this._log?.info('runs', 'completion requested', {
1365
1691
  runId,
1366
1692
  model,
@@ -1372,14 +1698,14 @@ export class QvacAssistant extends ReadyResource {
1372
1698
  agent: runAgent,
1373
1699
  messages: nameSenders(messages),
1374
1700
  cacheKey,
1375
- thinkingDefault: config ? (config.thinkingEnabled ? 'enabled' : 'disabled') : undefined,
1701
+ thinkingDefault: thinkingDefault(config),
1376
1702
  toolConfigs,
1377
1703
  grantedDirs,
1378
1704
  grantedDrives,
1379
1705
  driveOp,
1380
1706
  grantedWriteDirs: write,
1381
1707
  approve: approvals.approve,
1382
- attach: (input) => this._uploadGenerated(meshId, chatId, runId, deviceId, input),
1708
+ attach: (input) => this._uploadGenerated(input, { meshId, chatId, runId, deviceId }),
1383
1709
  fetchAttachment: (query) => this._fetchAttachment(engine, chatId, query),
1384
1710
  ragSearch: async (input) => {
1385
1711
  const found = await raceAbort(engine.ragSearch({
@@ -1424,15 +1750,8 @@ export class QvacAssistant extends ReadyResource {
1424
1750
  for await (const event of events) {
1425
1751
  harnessEvents++;
1426
1752
  modelWatch.clear();
1427
- if (event.type === 'content' || event.type === 'thinking') {
1428
- const text = event.type === 'content'
1429
- ? sourceTagFilter.push(event.text)
1430
- : event.text;
1431
- if (text) {
1432
- if (event.type === 'content')
1433
- answerText += text;
1434
- await transcript.text(event.type, text);
1435
- }
1753
+ if (isHarnessEvent(event, 'content') || isHarnessEvent(event, 'thinking')) {
1754
+ await writeText(event.type, event.text);
1436
1755
  continue;
1437
1756
  }
1438
1757
  if (event.type === 'metrics') {
@@ -1462,69 +1781,21 @@ export class QvacAssistant extends ReadyResource {
1462
1781
  }
1463
1782
  await flushTranscript();
1464
1783
  if (event.type === 'error') {
1465
- const folded = isContextOverflowFailure(event.message) &&
1466
- (await this._runCompaction({
1467
- chatId,
1468
- transcript,
1469
- trigger: 'overflow',
1470
- signal: controller.signal,
1471
- meshId
1472
- }));
1473
- runFailure = runFailureNotice(event.message);
1474
- await transcript.status({ state: 'failed', reason: runFailure, metrics });
1475
- if (folded) {
1476
- await engine
1477
- .runAgent({ chatId, agentId: agent.id, parentRunId: runId })
1478
- .catch((err) => this._log?.warn('runs', 'compaction retry not queued', { runId, err }));
1479
- }
1480
- titleOutcome = 'ended';
1784
+ await failRun(event.message);
1481
1785
  return;
1482
1786
  }
1483
1787
  if (event.type === 'aborted') {
1484
- const outcome = this._abortOutcome(runId, controller.signal.reason);
1485
- if (outcome.kind === 'replay') {
1486
- runVerdict = 'replay';
1487
- this._seen.delete(runId);
1488
- return;
1489
- }
1490
- if (outcome.kind === 'handoff') {
1491
- runVerdict = 'handoff';
1492
- return;
1493
- }
1494
- runFailure = outcome.reason;
1495
- await transcript.status({ state: 'failed', reason: outcome.reason, metrics });
1496
- titleOutcome = 'ended';
1788
+ await endAbortedRun();
1497
1789
  return;
1498
1790
  }
1499
- if (event.type === 'tool-call') {
1791
+ if (isHarnessEvent(event, 'tool-call')) {
1500
1792
  this._log?.info('runs', 'tool call', { runId, name: event.name });
1501
- await transcript.chunk({
1502
- type: 'tool-call',
1503
- toolCall: {
1504
- name: event.name,
1505
- arguments: event.args
1506
- },
1507
- effect: event.effect === 'read' ? 'read' : 'write',
1508
- display: event.display,
1509
- rawCall: typeof event.rawCall === 'string' ? event.rawCall : undefined
1510
- });
1793
+ await transcript.chunk(toolCallChunk(event));
1511
1794
  }
1512
- else if (event.type === 'tool-result') {
1513
- if (isDeniedResult(event.result)) {
1514
- this._log?.info('approvals', 'tool ran denied', { runId, tool: event.name });
1515
- }
1516
- else if (event.name !== SKILL_TOOL) {
1517
- ran = true;
1518
- }
1519
- if (WEB_TOOLS.includes(String(event.name))) {
1520
- await this._citeWebSources(transcript, runId, event.result);
1521
- }
1522
- await transcript.chunk({
1523
- type: 'tool-result',
1524
- toolResult: { name: event.name, result: withoutDataChannel(event.result) }
1525
- });
1795
+ else if (isHarnessEvent(event, 'tool-result')) {
1796
+ await recordToolResult(event);
1526
1797
  }
1527
- else if (event.type === 'tool-activity') {
1798
+ else if (isHarnessEvent(event, 'tool-activity')) {
1528
1799
  void engine
1529
1800
  .recordToolActivity({
1530
1801
  agentId: runAgent.id,
@@ -1553,7 +1824,9 @@ export class QvacAssistant extends ReadyResource {
1553
1824
  pending: transcript.pending()
1554
1825
  });
1555
1826
  }
1556
- const citedTitles = ragInjected ? citedByPhraseOverlap(answerText, retrievedRecords) : null;
1827
+ const citedTitles = injected.ragInjected
1828
+ ? citedByPhraseOverlap(answerText, retrievedRecords)
1829
+ : null;
1557
1830
  await transcript.status({
1558
1831
  state: 'completed',
1559
1832
  metrics: citedTitles ? { ...metrics, citedTitles } : metrics
@@ -1593,8 +1866,120 @@ export class QvacAssistant extends ReadyResource {
1593
1866
  await approvals.settle({ preservePending: isLifecycleTeardown(controller.signal.reason) });
1594
1867
  settledWatch.stop();
1595
1868
  if (titleOutcome && !diagramRepair) {
1596
- void this._autoTitle(engine, chatId, messages, model, runAgent, titleOutcome === 'completed');
1869
+ void this._autoTitle(engine, {
1870
+ chatId,
1871
+ messages,
1872
+ model,
1873
+ agent: runAgent,
1874
+ probe: titleOutcome === 'completed'
1875
+ });
1876
+ }
1877
+ }
1878
+ }
1879
+ async _invokeCapability({ engine, request, agent, capability, controller, meshId, runId }) {
1880
+ const { chatId } = request;
1881
+ const deviceId = this._requireDeviceId();
1882
+ const harness = this._harnessFor(agent);
1883
+ const transcript = new RunTranscript(engine, {
1884
+ chatId,
1885
+ runId,
1886
+ deviceId,
1887
+ model: '',
1888
+ log: this._log
1889
+ });
1890
+ const inflight = this._runs.get(runId);
1891
+ if (inflight) {
1892
+ this._runs.set(runId, { ...inflight, executing: true, model: '' });
1893
+ }
1894
+ this._yieldDownloads();
1895
+ const executing = await transcript.status({ state: 'executing' });
1896
+ const modelWatch = this._watchModelProgress((runStatus) => transcript.progress(executing.id, runStatus));
1897
+ const settledWatch = this._watchSettled(engine, { chatId, runId, controller });
1898
+ const ownershipWatch = this._watchOwnership(engine, agent.id, controller);
1899
+ let metrics;
1900
+ const endAbortedCapability = async () => {
1901
+ const outcome = this._abortOutcome(runId, controller.signal.reason);
1902
+ if (outcome.kind === 'replay') {
1903
+ this._seen.delete(runId);
1904
+ }
1905
+ if (outcome.kind === 'failed') {
1906
+ await transcript.status({ state: 'failed', reason: outcome.reason, metrics });
1907
+ }
1908
+ };
1909
+ this._log?.info('runs', 'capability requested', { runId, capability });
1910
+ try {
1911
+ if (!harness.invoke) {
1912
+ await transcript.status({
1913
+ state: 'failed',
1914
+ reason: RUN_FAILURE_REASONS.capabilityUnavailable
1915
+ });
1916
+ return;
1917
+ }
1918
+ const events = harness.invoke({
1919
+ capability,
1920
+ args: isRecord(request.capabilityArgs) ? request.capabilityArgs : {},
1921
+ enhance: request.enhance ?? 'never',
1922
+ agent,
1923
+ attach: (input) => this._uploadGenerated(input, { meshId, chatId, runId, deviceId }),
1924
+ fetchAttachment: (query) => this._fetchAttachment(engine, chatId, query),
1925
+ progress: (update) => modelWatch.image(update),
1926
+ signal: controller.signal
1927
+ });
1928
+ for await (const event of events) {
1929
+ if (event.type === 'metrics') {
1930
+ const { type, ...rest } = event;
1931
+ metrics = rest;
1932
+ this._log?.info('runs', 'capability metrics', { runId, capability, ...rest });
1933
+ continue;
1934
+ }
1935
+ if (event.type === 'error') {
1936
+ await transcript.status({
1937
+ state: 'failed',
1938
+ reason: runFailureNotice(event.message),
1939
+ metrics
1940
+ });
1941
+ return;
1942
+ }
1943
+ if (event.type === 'aborted') {
1944
+ await endAbortedCapability();
1945
+ return;
1946
+ }
1947
+ if (isHarnessEvent(event, 'tool-call')) {
1948
+ await transcript.chunk(toolCallChunk(event));
1949
+ }
1950
+ else if (isHarnessEvent(event, 'tool-result')) {
1951
+ await transcript.chunk(toolResultChunk(event));
1952
+ }
1953
+ else {
1954
+ this._log?.warn('assistant', 'unknown capability event dropped', { type: event.type });
1955
+ }
1956
+ }
1957
+ await transcript.status({ state: 'completed', metrics });
1958
+ }
1959
+ catch (err) {
1960
+ const outcome = controller.signal.aborted
1961
+ ? this._abortOutcome(runId, controller.signal.reason)
1962
+ : null;
1963
+ if (outcome?.kind === 'replay') {
1964
+ this._seen.delete(runId);
1965
+ return;
1597
1966
  }
1967
+ if (outcome?.kind === 'handoff') {
1968
+ return;
1969
+ }
1970
+ await transcript.status({
1971
+ state: 'failed',
1972
+ reason: outcome?.reason ??
1973
+ (this._lenderFor(agent) ? loanFailureNotice(err) : null) ??
1974
+ runFailureNotice(err),
1975
+ metrics
1976
+ });
1977
+ }
1978
+ finally {
1979
+ transcript.close();
1980
+ modelWatch.stop();
1981
+ settledWatch.stop();
1982
+ ownershipWatch.stop();
1598
1983
  }
1599
1984
  }
1600
1985
  async _loadToolConfigs(engine, agentId) {
@@ -1604,7 +1989,7 @@ export class QvacAssistant extends ReadyResource {
1604
1989
  });
1605
1990
  const map = {};
1606
1991
  for (const row of configs ?? []) {
1607
- if (row?.toolName && row.settings && typeof row.settings === 'object') {
1992
+ if (row?.toolName && isRecord(row.settings)) {
1608
1993
  map[row.toolName] = row.settings;
1609
1994
  }
1610
1995
  }
@@ -1645,8 +2030,9 @@ export class QvacAssistant extends ReadyResource {
1645
2030
  })
1646
2031
  .at(-1)?.id;
1647
2032
  }
1648
- if (!attachmentId)
2033
+ if (!attachmentId) {
1649
2034
  return null;
2035
+ }
1650
2036
  const stream = engine.fileRead({ chatId, fileId: attachmentId, metadataOnly: meta });
1651
2037
  const abort = () => stream.destroy();
1652
2038
  const pieces = [];
@@ -1654,19 +2040,22 @@ export class QvacAssistant extends ReadyResource {
1654
2040
  stream.on('error', noop);
1655
2041
  signal?.addEventListener('abort', abort, { once: true });
1656
2042
  try {
1657
- if (signal?.aborted)
2043
+ if (signal?.aborted) {
1658
2044
  throw new AbortedError();
2045
+ }
1659
2046
  for await (const frame of stream) {
1660
2047
  if (frame.metadata) {
1661
2048
  const { id, fileName, mimeType, byteLength } = frame.metadata;
1662
2049
  head = { id, fileName, mimeType, byteLength };
1663
2050
  }
1664
- if (frame.data)
2051
+ if (frame.data) {
1665
2052
  pieces.push(frame.data);
2053
+ }
1666
2054
  }
1667
- if (signal?.aborted)
2055
+ if (signal?.aborted) {
1668
2056
  throw new AbortedError();
1669
- return head && (meta ? head : { ...head, bytes: Buffer.concat(pieces) });
2057
+ }
2058
+ return head && (meta ? head : { ...head, bytes: b4a.concat(pieces) });
1670
2059
  }
1671
2060
  finally {
1672
2061
  signal?.removeEventListener('abort', abort);
@@ -1689,29 +2078,30 @@ export class QvacAssistant extends ReadyResource {
1689
2078
  return null;
1690
2079
  }
1691
2080
  }
1692
- async _uploadGenerated(meshId, chatId, runId, deviceId, { fileName, mimeType, bytes, raw }) {
2081
+ async _uploadGenerated({ fileName, mimeType, bytes, raw }, { meshId, chatId, runId, deviceId }) {
1693
2082
  const duplex = this._engine.uploadAttachment();
1694
2083
  duplex.write({ chatId, runId, originDeviceId: deviceId, fileName, mimeType, raw, meshId });
1695
- const buf = Buffer.isBuffer(bytes)
1696
- ? bytes
1697
- : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength);
2084
+ const buf = b4a.toBuffer(bytes);
1698
2085
  for (let i = 0; i < buf.length; i += UPLOAD_FRAME_BYTES) {
1699
2086
  duplex.write({ data: buf.subarray(i, i + UPLOAD_FRAME_BYTES) });
1700
2087
  }
1701
2088
  duplex.end();
1702
2089
  let attachment = null;
1703
2090
  for await (const frame of duplex) {
1704
- if (frame.attachment)
2091
+ if (frame.attachment) {
1705
2092
  attachment = frame.attachment;
2093
+ }
1706
2094
  }
1707
- if (!attachment)
2095
+ if (!attachment) {
1708
2096
  throw new Error('attachment upload ended without a record');
2097
+ }
1709
2098
  return attachment;
1710
2099
  }
1711
2100
  async _hydrateAudio(messages, signal) {
1712
2101
  const turns = messages.filter((turn) => turn.audioAttachments?.length);
1713
- if (!turns.length)
2102
+ if (!turns.length) {
1714
2103
  return;
2104
+ }
1715
2105
  for (const turn of turns) {
1716
2106
  for (const ref of turn.audioAttachments ?? []) {
1717
2107
  if (signal.aborted)
@@ -1741,15 +2131,17 @@ export class QvacAssistant extends ReadyResource {
1741
2131
  const model = mediaModelNameFor('transcription', await this._persistedProfile());
1742
2132
  await this.ensureMediaModelUnpaused('transcription', model);
1743
2133
  await this._downloads?.waitUntilCached(model, signal);
1744
- const text = (await this._harness.transcribe({ model: '', audio: bytes, signal })).trim();
2134
+ const text = (await this._harness.transcribe({ model: '', audio: b4a.toBuffer(bytes), signal })).trim();
1745
2135
  this._audioTranscripts.set(ref.blobId, text);
1746
- if (!text)
2136
+ if (!text) {
1747
2137
  return silentAudioNotice(ref.fileName);
2138
+ }
1748
2139
  return audioTranscriptBlock(ref.fileName, text);
1749
2140
  }
1750
2141
  catch (err) {
1751
- if (signal.aborted)
2142
+ if (signal.aborted) {
1752
2143
  throw err;
2144
+ }
1753
2145
  this._log?.warn('assistant', `audio transcription failed — ${ref.fileName}`, err);
1754
2146
  return unheardAudioNotice(label, 'transcription failed on this device');
1755
2147
  }
@@ -1757,57 +2149,177 @@ export class QvacAssistant extends ReadyResource {
1757
2149
  async _hydrateImages(messages, signal, runId) {
1758
2150
  const unreadable = new Map();
1759
2151
  const turns = messages.filter((turn) => turn.images?.length);
1760
- if (!turns.length)
2152
+ if (!turns.length) {
1761
2153
  return unreadable;
2154
+ }
1762
2155
  const inUse = new Set(turns.flatMap((turn) => (turn.images ?? []).map(imageCacheName)));
1763
2156
  this._runImageCacheNames.set(runId, inUse);
1764
2157
  const dir = this._imageCacheDir;
1765
2158
  await fs.promises.mkdir(dir, { recursive: true, mode: 0o700 });
1766
2159
  for (const turn of turns) {
1767
- const dropped = [];
1768
- for (const image of turn.images ?? []) {
1769
- try {
1770
- const file = await this._hydrateImage(dir, image, signal);
1771
- turn.attachments = [...(turn.attachments ?? []), { path: file }];
1772
- }
1773
- catch (err) {
1774
- if (signal.aborted)
1775
- throw err;
1776
- this._log?.error('assistant', `image hydration failed — skipped ${image.fileName}`, err);
1777
- dropped.push(image.fileName);
1778
- }
1779
- }
1780
- if (dropped.length)
2160
+ const dropped = await this._hydrateTurnImages(turn, { dir, signal });
2161
+ if (dropped.length) {
1781
2162
  unreadable.set(turn, dropped);
2163
+ }
1782
2164
  delete turn.images;
1783
2165
  }
1784
2166
  return unreadable;
1785
2167
  }
2168
+ async _hydrateTurnImages(turn, { dir, signal }) {
2169
+ const dropped = [];
2170
+ for (const image of turn.images ?? []) {
2171
+ try {
2172
+ const file = await this._hydrateImage(dir, image, signal);
2173
+ turn.attachments = [...(turn.attachments ?? []), { path: file }];
2174
+ }
2175
+ catch (err) {
2176
+ if (signal.aborted) {
2177
+ throw err;
2178
+ }
2179
+ this._log?.error('assistant', `image hydration failed — skipped ${image.fileName}`, err);
2180
+ dropped.push(image.fileName);
2181
+ }
2182
+ }
2183
+ return dropped;
2184
+ }
1786
2185
  async _hydrateImage(dir, image, signal) {
1787
2186
  const file = path.join(dir, imageCacheName(image));
1788
- const cached = await fs.promises.stat(file).catch(noop);
1789
- if (!cached?.size) {
2187
+ await cacheOrTouch(file, async () => {
1790
2188
  const raw = await this._readBlob(image.url, signal);
1791
- if (signal.aborted)
2189
+ if (signal.aborted) {
1792
2190
  throw new Error('hydration aborted');
1793
- if (!raw)
2191
+ }
2192
+ if (!raw) {
1794
2193
  throw new Error(`empty download for ${image.fileName}`);
1795
- const bytes = await normalizeImageForModel(raw);
1796
- const staging = `${file}.partial-${b4a.toHex(crypto.randomBytes(4))}`;
1797
- await fs.promises.writeFile(staging, bytes, { mode: 0o600 });
1798
- await fs.promises.rename(staging, file);
2194
+ }
2195
+ return normalizeImageForModel(raw);
2196
+ });
2197
+ return file;
2198
+ }
2199
+ async _hydratePdfPages(messages, signal, runId) {
2200
+ const rendered = new Set();
2201
+ const turns = messages.filter((turn) => turn.pdfs?.length);
2202
+ if (!turns.length) {
2203
+ return rendered;
1799
2204
  }
1800
- else {
1801
- const now = new Date();
1802
- await fs.promises.utimes(file, now, now).catch(noop);
2205
+ const dir = this._imageCacheDir;
2206
+ await fs.promises.mkdir(dir, { recursive: true, mode: 0o700 });
2207
+ const pinned = this._runImageCacheNames.get(runId) ?? new Set();
2208
+ this._runImageCacheNames.set(runId, pinned);
2209
+ const latestUser = messages.findLast((turn) => turn.role === 'user');
2210
+ for (const turn of turns) {
2211
+ let budget = MAX_PDF_PAGE_IMAGES;
2212
+ for (const pdf of turn.pdfs ?? []) {
2213
+ if (budget <= 0)
2214
+ break;
2215
+ const pages = await this._tryHydratePdf(dir, turn, {
2216
+ pdf,
2217
+ pinned,
2218
+ maxPages: budget,
2219
+ signal
2220
+ });
2221
+ budget -= pages.tried;
2222
+ if (turn === latestUser && pages.names.length) {
2223
+ rendered.add(pdf.id);
2224
+ }
2225
+ }
2226
+ delete turn.pdfs;
2227
+ }
2228
+ return rendered;
2229
+ }
2230
+ async _tryHydratePdf(dir, turn, opts) {
2231
+ try {
2232
+ return await this._hydratePdf(dir, turn, opts);
2233
+ }
2234
+ catch (err) {
2235
+ if (opts.signal.aborted) {
2236
+ throw err;
2237
+ }
2238
+ this._log?.warn('assistant', `pdf rasterization failed — ${opts.pdf.fileName}`, err);
2239
+ return { names: [], tried: 0 };
2240
+ }
2241
+ }
2242
+ async _hydratePdf(dir, turn, { pdf, pinned, maxPages, signal }) {
2243
+ const key = `${pdf.blobId}:${maxPages}`;
2244
+ const memo = this._pdfPages.get(key);
2245
+ if (memo) {
2246
+ for (const name of memo.names) {
2247
+ pinned.add(name);
2248
+ }
2249
+ const files = memo.names.map((name) => path.join(dir, name));
2250
+ if ((await Promise.all(files.map((file) => cacheOrTouch(file, noBytes)))).every(Boolean)) {
2251
+ attachPages(turn, files);
2252
+ return memo;
2253
+ }
2254
+ }
2255
+ const pages = await this._renderPdf(dir, { pdf, pinned, maxPages, signal });
2256
+ if (!pages) {
2257
+ return { names: [], tried: 0 };
2258
+ }
2259
+ this._pdfPages.set(key, pages);
2260
+ attachPages(turn, pages.names.map((name) => path.join(dir, name)));
2261
+ return pages;
2262
+ }
2263
+ async _renderPdf(dir, { pdf, pinned, maxPages, signal }) {
2264
+ const bytes = await this._readBlob(pdf.url, signal);
2265
+ if (signal.aborted) {
2266
+ throw new Error('hydration aborted');
2267
+ }
2268
+ if (!bytes) {
2269
+ return null;
2270
+ }
2271
+ const doc = await this._pdfRasterizer.open(bytes);
2272
+ if (!doc) {
2273
+ return { names: [], tried: 0 };
2274
+ }
2275
+ try {
2276
+ const names = [];
2277
+ let tried = 0;
2278
+ const count = doc.pageCount();
2279
+ for (let page = 0; page < count && tried < maxPages; page++) {
2280
+ if (signal.aborted)
2281
+ throw new Error('hydration aborted');
2282
+ const outcome = await this._renderPdfPage(doc, { dir, pdf, page, pinned, signal });
2283
+ if (outcome.tried) {
2284
+ tried++;
2285
+ }
2286
+ if (outcome.name) {
2287
+ names.push(outcome.name);
2288
+ }
2289
+ }
2290
+ return { names, tried };
2291
+ }
2292
+ finally {
2293
+ doc.close();
2294
+ }
2295
+ }
2296
+ async _renderPdfPage(doc, { dir, pdf, page, pinned, signal }) {
2297
+ let tried = false;
2298
+ try {
2299
+ const flags = doc.pageFlags(page);
2300
+ if (flags.hasText && !flags.hasImage) {
2301
+ return { tried };
2302
+ }
2303
+ tried = true;
2304
+ const name = pdfPageCacheName(pdf, page);
2305
+ pinned.add(name);
2306
+ const cached = await cacheOrTouch(path.join(dir, name), () => doc.renderPage(page));
2307
+ return { tried, name: cached ? name : undefined };
2308
+ }
2309
+ catch (err) {
2310
+ if (signal.aborted) {
2311
+ throw err;
2312
+ }
2313
+ this._log?.warn('assistant', `pdf page skipped — ${pdf.fileName} p${page}`, err);
2314
+ return { tried };
1803
2315
  }
1804
- return file;
1805
2316
  }
1806
2317
  async _compactIfNearFull(params) {
1807
2318
  const { chatId, transcript, trigger, signal, meshId } = params;
1808
2319
  const metrics = await this._lastRunMetrics(chatId, meshId);
1809
- if (!nearContextLimit(metrics.cacheTokens, metrics.ctxSize))
2320
+ if (!nearContextLimit(metrics.cacheTokens, metrics.ctxSize)) {
1810
2321
  return false;
2322
+ }
1811
2323
  if (!canCompact(metrics.ctxSize)) {
1812
2324
  this._log?.debug('runs', 'chat near its window but under the compaction floor', {
1813
2325
  chatId,
@@ -1864,7 +2376,7 @@ export class QvacAssistant extends ReadyResource {
1864
2376
  }
1865
2377
  }
1866
2378
  async _lastRunMetrics(chatId, meshId) {
1867
- const deviceId = this._deviceId;
2379
+ const deviceId = this._requireDeviceId();
1868
2380
  const { chunks } = await meshScoped(this._engine, meshId)
1869
2381
  .listChunks({ chatId, type: 'run-status' })
1870
2382
  .catch(() => ({ chunks: [] }));
@@ -1872,12 +2384,17 @@ export class QvacAssistant extends ReadyResource {
1872
2384
  for (let index = statuses.length - 1; index >= 0; index--) {
1873
2385
  const chunk = statuses[index];
1874
2386
  const metrics = chunk.runStatus?.metrics;
1875
- if (chunk.runStatus?.state !== 'completed' || !metrics)
2387
+ if (chunk.runStatus?.state !== 'completed' || !metrics) {
1876
2388
  continue;
1877
- if (!chunk.originDeviceId?.equals(deviceId))
2389
+ }
2390
+ if (!chunk.originDeviceId || !b4a.equals(chunk.originDeviceId, deviceId)) {
2391
+ return {};
2392
+ }
2393
+ const parsed = RunOccupancySchema.safeParse(metrics);
2394
+ if (!parsed.success) {
1878
2395
  return {};
1879
- const parsed = metrics;
1880
- return { cacheTokens: parsed.cacheTokens, ctxSize: parsed.ctxSize };
2396
+ }
2397
+ return { cacheTokens: parsed.data.cacheTokens, ctxSize: parsed.data.ctxSize };
1881
2398
  }
1882
2399
  return {};
1883
2400
  }
@@ -1885,12 +2402,13 @@ export class QvacAssistant extends ReadyResource {
1885
2402
  if (!this.opened)
1886
2403
  await this.ready();
1887
2404
  const engine = meshScoped(this._engine, options.meshId);
1888
- const deviceId = this._deviceId;
2405
+ const deviceId = this._requireDeviceId();
1889
2406
  const chat = await engine.getChat({ chatId }).catch(noop);
1890
- if (!chat)
2407
+ if (!chat) {
1891
2408
  return { compacted: false, reason: 'unknown chat' };
2409
+ }
1892
2410
  const agent = await engine.getAgent({ agentId: chat.agentId }).catch(noop);
1893
- if (!agent || !agent.providerDeviceId?.equals(deviceId)) {
2411
+ if (!agent || !agent.providerDeviceId || !b4a.equals(agent.providerDeviceId, deviceId)) {
1894
2412
  return { compacted: false, reason: 'not the provider device' };
1895
2413
  }
1896
2414
  const model = runnableModelName(agent.modelName, await this._ensureCapability(), this._mobileClass);
@@ -1913,20 +2431,27 @@ export class QvacAssistant extends ReadyResource {
1913
2431
  const senders = await this._senderNames(engine, chunks);
1914
2432
  for (;;) {
1915
2433
  const boundary = compactionBoundary(chunks, keepTurns);
1916
- if (!boundary)
2434
+ if (!boundary) {
1917
2435
  return { compacted: false, reason: 'nothing-to-fold' };
2436
+ }
1918
2437
  const through = chunks.find((chunk) => chunk.id === boundary.throughId);
1919
- if (!through)
2438
+ if (!through) {
1920
2439
  return { compacted: false, reason: 'nothing-to-fold' };
1921
- if (through.seq <= lastThroughSeq)
2440
+ }
2441
+ if (through.seq <= lastThroughSeq) {
1922
2442
  return { compacted: false, reason: 'already-compacted' };
2443
+ }
1923
2444
  const inRange = chunks.filter((chunk) => chunk.seq <= through.seq);
1924
2445
  const folded = historyFromChunks(inRange, staleToolRunIds(inRange, priorAsks ?? [], ''), senders);
1925
2446
  if (!announced) {
1926
2447
  await options.onFold?.();
1927
2448
  announced = true;
1928
2449
  }
1929
- const attempt = await this._summarize(folded, model, withConfigDefaults(agent, config), options.signal);
2450
+ const attempt = await this._summarize(folded, {
2451
+ model,
2452
+ agent: withConfigDefaults(agent, config),
2453
+ signal: options.signal
2454
+ });
1930
2455
  if (!attempt.summary) {
1931
2456
  if (attempt.overflowed && boundary.foldedTurns > 1) {
1932
2457
  keepTurns = userTurns - Math.floor(boundary.foldedTurns / 2);
@@ -1938,8 +2463,9 @@ export class QvacAssistant extends ReadyResource {
1938
2463
  if (!foldReducesPrompt(folded, summary)) {
1939
2464
  return { compacted: false, reason: 'summary-not-smaller' };
1940
2465
  }
1941
- if (options.signal?.aborted)
2466
+ if (options.signal?.aborted) {
1942
2467
  return { compacted: false, reason: 'canceled' };
2468
+ }
1943
2469
  await engine.appendChunk({
1944
2470
  chatId,
1945
2471
  type: 'compaction',
@@ -1955,7 +2481,7 @@ export class QvacAssistant extends ReadyResource {
1955
2481
  return { compacted: true, foldedTurns: boundary.foldedTurns };
1956
2482
  }
1957
2483
  }
1958
- async _summarize(turns, model, agent, signal) {
2484
+ async _summarize(turns, { model, agent, signal }) {
1959
2485
  const hasPriorSummary = turns[0]?.content.startsWith(COMPACTION_TURN_PREFIX) === true;
1960
2486
  const events = this._harnessFor(agent).completion({
1961
2487
  model,
@@ -1978,8 +2504,9 @@ export class QvacAssistant extends ReadyResource {
1978
2504
  let text = '';
1979
2505
  let failed = null;
1980
2506
  for await (const event of events) {
1981
- if (event.type === 'content' && typeof event.text === 'string')
2507
+ if (event.type === 'content' && typeof event.text === 'string') {
1982
2508
  text += event.text;
2509
+ }
1983
2510
  else if (event.type === 'error') {
1984
2511
  failed = typeof event.message === 'string' ? event.message : 'summarizer failed';
1985
2512
  break;
@@ -1988,8 +2515,9 @@ export class QvacAssistant extends ReadyResource {
1988
2515
  failed = 'summarizer aborted';
1989
2516
  break;
1990
2517
  }
1991
- else if (event.type === 'done')
2518
+ else if (event.type === 'done') {
1992
2519
  break;
2520
+ }
1993
2521
  }
1994
2522
  if (failed !== null) {
1995
2523
  this._log?.warn('runs', 'summarizer failed mid-stream', { chars: text.length, failed });
@@ -1997,14 +2525,16 @@ export class QvacAssistant extends ReadyResource {
1997
2525
  }
1998
2526
  return { summary: parseCompactionSummary(text), overflowed: false };
1999
2527
  }
2000
- async _autoTitle(engine, chatId, messages, model, agent, probe) {
2528
+ async _autoTitle(engine, { chatId, messages, model, agent, probe }) {
2001
2529
  try {
2002
2530
  const first = messages.find((turn) => turn.role === 'user' && turn.content.trim());
2003
- if (!first)
2531
+ if (!first) {
2004
2532
  return;
2533
+ }
2005
2534
  const chat = await engine.getChat({ chatId });
2006
- if (!isDefaultChatTitle(chat?.title))
2535
+ if (!isDefaultChatTitle(chat?.title)) {
2007
2536
  return;
2537
+ }
2008
2538
  await this._generateChatTitle({ engine, chatId, input: first.content, model, agent, probe });
2009
2539
  }
2010
2540
  catch (err) {
@@ -2014,8 +2544,9 @@ export class QvacAssistant extends ReadyResource {
2014
2544
  async _generateChatTitle(params) {
2015
2545
  const { engine, chatId, input, model, agent, probe } = params;
2016
2546
  const prompt = input.trim();
2017
- if (!prompt)
2547
+ if (!prompt) {
2018
2548
  return null;
2549
+ }
2019
2550
  let generated = null;
2020
2551
  if (probe && !this.closing) {
2021
2552
  try {
@@ -2032,8 +2563,9 @@ export class QvacAssistant extends ReadyResource {
2032
2563
  }
2033
2564
  }
2034
2565
  const title = generated ?? deriveChatTitle(prompt);
2035
- if (!title)
2566
+ if (!title) {
2036
2567
  return null;
2568
+ }
2037
2569
  await engine.renameChat({ chatId, config: { title } });
2038
2570
  this._log?.info('runs', 'chat titled', {
2039
2571
  chatId,
@@ -2072,19 +2604,21 @@ export class QvacAssistant extends ReadyResource {
2072
2604
  });
2073
2605
  let text = '';
2074
2606
  for await (const event of events) {
2075
- if (event.type === 'content' && typeof event.text === 'string')
2607
+ if (event.type === 'content' && typeof event.text === 'string') {
2076
2608
  text += event.text;
2609
+ }
2077
2610
  else if (event.type === 'inference-call') {
2078
2611
  void this._inferenceCalls
2079
2612
  .record(chatId, { runId: TITLE_RUN_ID, at: Date.now(), call: asRecord(event.call) })
2080
2613
  .catch((err) => this._log?.warn('assistant', 'title call not recorded', { err }));
2081
2614
  }
2082
- else if (event.type === 'error' || event.type === 'aborted' || event.type === 'done')
2615
+ else if (event.type === 'error' || event.type === 'aborted' || event.type === 'done') {
2083
2616
  break;
2617
+ }
2084
2618
  }
2085
2619
  return parseTitleResponse(text);
2086
2620
  }
2087
- _createApprove(engine, chatId, runId, deviceId, agentId, hasRun) {
2621
+ _createApprove(engine, { chatId, runId, deviceId, agentId, hasRun }) {
2088
2622
  const waiters = new Map();
2089
2623
  let latestChunks = [];
2090
2624
  let watcher = null;
@@ -2092,16 +2626,19 @@ export class QvacAssistant extends ReadyResource {
2092
2626
  const pump = async (source) => {
2093
2627
  while (true) {
2094
2628
  const { value, done } = await source.next();
2095
- if (done)
2629
+ if (done) {
2096
2630
  return;
2631
+ }
2097
2632
  latestChunks = value.chunks ?? [];
2098
2633
  for (const chunk of latestChunks) {
2099
2634
  const status = chunk.approvalRequest?.status;
2100
- if (!status || status === 'pending')
2635
+ if (!status || status === 'pending') {
2101
2636
  continue;
2637
+ }
2102
2638
  const waiter = waiters.get(chunk.id);
2103
- if (!waiter)
2639
+ if (!waiter) {
2104
2640
  continue;
2641
+ }
2105
2642
  waiters.delete(chunk.id);
2106
2643
  this._log?.info('approvals', `answer ${status}`, {
2107
2644
  runId,
@@ -2121,10 +2658,12 @@ export class QvacAssistant extends ReadyResource {
2121
2658
  watcher = opened;
2122
2659
  pump(opened)
2123
2660
  .catch((err) => {
2124
- if (closing)
2661
+ if (closing) {
2125
2662
  this._log?.debug('assistant', 'approval watch closed', err);
2126
- else
2663
+ }
2664
+ else {
2127
2665
  this._log?.error('assistant', 'approval watch failed', err);
2666
+ }
2128
2667
  })
2129
2668
  .finally(() => {
2130
2669
  for (const [id, waiter] of waiters) {
@@ -2135,8 +2674,9 @@ export class QvacAssistant extends ReadyResource {
2135
2674
  });
2136
2675
  waiter.resolve(false);
2137
2676
  }
2138
- if (watcher === opened)
2677
+ if (watcher === opened) {
2139
2678
  watcher = null;
2679
+ }
2140
2680
  });
2141
2681
  };
2142
2682
  const asksForRun = async () => {
@@ -2147,8 +2687,9 @@ export class QvacAssistant extends ReadyResource {
2147
2687
  };
2148
2688
  const sameCall = (chunk, call) => {
2149
2689
  const ask = chunk.approvalRequest;
2150
- if (!ask || ask.name !== call.name)
2690
+ if (!ask || ask.name !== call.name) {
2151
2691
  return false;
2692
+ }
2152
2693
  return ((ask.resource?.kind ?? null) === (call.resource?.kind ?? null) &&
2153
2694
  (ask.resource?.label ?? null) === (call.resource?.label ?? null) &&
2154
2695
  JSON.stringify(ask.arguments) === JSON.stringify(call.arguments));
@@ -2248,8 +2789,9 @@ export class QvacAssistant extends ReadyResource {
2248
2789
  });
2249
2790
  await engine.resolveApproval({ chunkId: ask.id, approved: false });
2250
2791
  }
2251
- for (const waiter of waiters.values())
2792
+ for (const waiter of waiters.values()) {
2252
2793
  waiter.resolve(false);
2794
+ }
2253
2795
  }
2254
2796
  waiters.clear();
2255
2797
  closing = true;
@@ -2263,22 +2805,25 @@ export class QvacAssistant extends ReadyResource {
2263
2805
  if (reason === RUNNER_DIED && this._harnessDeath) {
2264
2806
  return { kind: 'failed', reason: this._harnessDeath };
2265
2807
  }
2266
- if (known)
2808
+ if (known) {
2267
2809
  return known;
2268
- if (this.suspended)
2810
+ }
2811
+ if (this.suspended) {
2269
2812
  return { kind: 'replay' };
2813
+ }
2270
2814
  if (reason !== undefined) {
2271
2815
  this._log?.warn('runs', 'run aborted for an unrecognised reason', { runId, reason });
2272
2816
  }
2273
2817
  return { kind: 'failed', reason: RUN_FAILURE_REASONS.runtimeEndedTurn };
2274
2818
  }
2275
- _watchSettled(engine, chatId, runId, controller) {
2819
+ _watchSettled(engine, { chatId, runId, controller }) {
2276
2820
  const it = engine.chunksWatch({ chatId, type: 'run-status' })[Symbol.asyncIterator]();
2277
2821
  const watching = (async () => {
2278
2822
  while (true) {
2279
2823
  const { value, done } = await it.next();
2280
- if (done)
2824
+ if (done) {
2281
2825
  return;
2826
+ }
2282
2827
  const settled = (value.chunks ?? []).some((c) => c.runId === runId && TERMINAL_STATES.has(c.runStatus?.state ?? ''));
2283
2828
  if (settled) {
2284
2829
  controller.abort(RUN_SETTLED);
@@ -2298,15 +2843,19 @@ export class QvacAssistant extends ReadyResource {
2298
2843
  };
2299
2844
  }
2300
2845
  _watchOwnership(engine, agentId, controller) {
2301
- const deviceId = this._deviceId;
2846
+ const deviceId = this._requireDeviceId();
2302
2847
  const it = engine.agentsWatch({})[Symbol.asyncIterator]();
2303
2848
  const watching = (async () => {
2304
2849
  while (true) {
2305
2850
  const { value, done } = await it.next();
2306
- if (done)
2851
+ if (done) {
2307
2852
  return;
2853
+ }
2308
2854
  const row = (value.agents ?? []).find((a) => a.id === agentId);
2309
- if (!row || row.deletedAt || !row.providerDeviceId?.equals(deviceId)) {
2855
+ if (!row ||
2856
+ row.deletedAt ||
2857
+ !row.providerDeviceId ||
2858
+ !b4a.equals(row.providerDeviceId, deviceId)) {
2310
2859
  controller.abort(OWNERSHIP_LOST);
2311
2860
  return;
2312
2861
  }
@@ -2331,11 +2880,13 @@ export class QvacAssistant extends ReadyResource {
2331
2880
  return;
2332
2881
  if (!('status' in state) || typeof state.status !== 'string')
2333
2882
  return;
2334
- if (!('model' in state) || typeof state.model !== 'string')
2883
+ if (!('model' in state) || typeof state.model !== 'string') {
2335
2884
  return;
2885
+ }
2336
2886
  const { status, model } = state;
2337
- if (status === 'downloading' || this._modelStatusLogged.get(model) === status)
2887
+ if (status === 'downloading' || this._modelStatusLogged.get(model) === status) {
2338
2888
  return;
2889
+ }
2339
2890
  this._modelStatusLogged.set(model, status);
2340
2891
  this._log?.info('models', `model ${status}`, { model });
2341
2892
  }
@@ -2357,11 +2908,13 @@ export class QvacAssistant extends ReadyResource {
2357
2908
  const runStatus = progress
2358
2909
  ? { state: 'executing', progress }
2359
2910
  : { state: 'executing' };
2360
- if (documents)
2911
+ if (documents) {
2361
2912
  runStatus.reason = 'preparing-documents';
2913
+ }
2362
2914
  const key = JSON.stringify(runStatus);
2363
- if (key === last)
2915
+ if (key === last) {
2364
2916
  return;
2917
+ }
2365
2918
  last = key;
2366
2919
  showing = progress !== null || documents;
2367
2920
  update(runStatus).catch(noop);
@@ -2369,17 +2922,20 @@ export class QvacAssistant extends ReadyResource {
2369
2922
  const onModel = (state) => {
2370
2923
  if (sampling)
2371
2924
  return;
2372
- if (state.status === 'loading')
2925
+ if (state.status === 'loading') {
2373
2926
  apply({ phase: 'loading', model: state.model });
2927
+ }
2374
2928
  else if (state.status === 'downloading') {
2375
2929
  const percentage = Math.floor((state.percentage ?? 0) / 10) * 10;
2376
2930
  apply({ phase: 'downloading', model: state.model, percentage });
2377
2931
  }
2378
- else
2932
+ else {
2379
2933
  apply(transcribing ? { phase: 'transcribing' } : null);
2934
+ }
2380
2935
  };
2381
- if (this._harness.on && this._harness.off)
2936
+ if (this._harness.on && this._harness.off) {
2382
2937
  this._harness.on('model', onModel);
2938
+ }
2383
2939
  return {
2384
2940
  documents: (active) => {
2385
2941
  documents = active;
@@ -2391,15 +2947,17 @@ export class QvacAssistant extends ReadyResource {
2391
2947
  },
2392
2948
  image: (progress) => {
2393
2949
  sampling = progress !== null;
2394
- if (!progress)
2950
+ if (!progress) {
2395
2951
  return apply(null);
2952
+ }
2396
2953
  apply({ phase: 'generating', percentage: Math.floor(progress.percentage / 10) * 10 });
2397
2954
  },
2398
2955
  stop: () => this._harness.off?.('model', onModel),
2399
2956
  clear: () => {
2400
2957
  sampling = false;
2401
- if (!showing)
2958
+ if (!showing) {
2402
2959
  return;
2960
+ }
2403
2961
  showing = false;
2404
2962
  last = JSON.stringify({ state: 'executing' });
2405
2963
  update({ state: 'executing' }).catch(noop);
@@ -2407,7 +2965,53 @@ export class QvacAssistant extends ReadyResource {
2407
2965
  };
2408
2966
  }
2409
2967
  }
2968
+ async function injectRetrievedKnowledge(knowledge, { transcript, sourceTagFilter, retrievedRecords, injected }) {
2969
+ const records = sourcesFromHits(knowledge.hits, RAG_SOURCE_BOUNDS);
2970
+ const notice = chooseRagNotice({
2971
+ errorMessage: knowledge.search.errorMessage,
2972
+ hitCount: records.length,
2973
+ searchedCount: knowledge.search.searchedCount ?? 0,
2974
+ indexEmpty: Boolean(knowledge.search.indexEmpty),
2975
+ hasKnowledge: Boolean(knowledge.search.hasKnowledge),
2976
+ pendingReferenced: Boolean(knowledge.search.pendingReferenced),
2977
+ failedFiles: knowledge.search.failedFiles ?? [],
2978
+ pendingFiles: knowledge.search.pendingFiles ?? []
2979
+ });
2980
+ sourceTagFilter.add(records.map((record) => record.title));
2981
+ retrievedRecords.push(...records.map((record, index) => ({
2982
+ ...record,
2983
+ excerpt: knowledge.hits[index]?.content ?? record.excerpt
2984
+ })));
2985
+ await transcript.chunk({
2986
+ type: 'sources',
2987
+ sources: { sources: records, notice: notice ?? undefined }
2988
+ });
2989
+ injected.sourcesWritten = true;
2990
+ if (records.length > 0) {
2991
+ knowledge.lastUser.content = `${formatRagContext(knowledge.hits, knowledge.search.availableFiles ?? [])}\n\nThe user's message:\n${knowledge.lastUser.content}\n\n${RAG_CITATION_LINE}`;
2992
+ injected.ragInjected = true;
2993
+ injected.documentsInjected = true;
2994
+ }
2995
+ else if (knowledge.search.availableFiles?.length) {
2996
+ knowledge.lastUser.content += `\n\n[Files indexed and available now: ${knowledge.search.availableFiles.join(', ')}. No relevant excerpts were retrieved for this question. Their current availability supersedes earlier indexing notices.]`;
2997
+ }
2998
+ }
2410
2999
  function noop() { }
3000
+ function toolCallChunk(event) {
3001
+ return {
3002
+ type: 'tool-call',
3003
+ toolCall: { name: event.name, arguments: event.args },
3004
+ effect: event.effect === 'read' ? 'read' : 'write',
3005
+ display: event.display,
3006
+ rawCall: typeof event.rawCall === 'string' ? event.rawCall : undefined
3007
+ };
3008
+ }
3009
+ function toolResultChunk(event) {
3010
+ return {
3011
+ type: 'tool-result',
3012
+ toolResult: { name: event.name, result: withoutDataChannel(event.result) }
3013
+ };
3014
+ }
2411
3015
  function runMetricsSummary(metrics) {
2412
3016
  const { timeToFirstToken, promptTokens, promptTokensPerSecond, cacheTokens, tokensPerSecond, totalTokens, durationMs, stopReason } = metrics;
2413
3017
  return {
@@ -2422,8 +3026,8 @@ function runMetricsSummary(metrics) {
2422
3026
  };
2423
3027
  }
2424
3028
  function asRecord(value) {
2425
- return typeof value === 'object' && value !== null ? { ...value } : {};
3029
+ return isRecord(value) ? { ...value } : {};
2426
3030
  }
2427
3031
  function isStreamDestroyed(err) {
2428
- return typeof err === 'object' && err !== null && 'code' in err && err.code === 'STREAM_DESTROYED';
3032
+ return isRecord(err) && err.code === 'STREAM_DESTROYED';
2429
3033
  }