@qvac/core 1.0.0-rc.7 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (237) hide show
  1. package/CHANGELOG.md +241 -3
  2. package/README.md +1 -0
  3. package/dist-lib/lib/api/attachments.d.ts +1 -1
  4. package/dist-lib/lib/api/attachments.js +11 -0
  5. package/dist-lib/lib/api/chunks.d.ts +4 -2
  6. package/dist-lib/lib/api/chunks.js +6 -3
  7. package/dist-lib/lib/api/device-routes.d.ts +6 -1
  8. package/dist-lib/lib/api/device-routes.js +20 -1
  9. package/dist-lib/lib/api/devices.js +6 -18
  10. package/dist-lib/lib/api/execution.d.ts +6 -2
  11. package/dist-lib/lib/api/execution.js +29 -3
  12. package/dist-lib/lib/api/host-run-log.d.ts +7 -1
  13. package/dist-lib/lib/api/host-run-log.js +3 -1
  14. package/dist-lib/lib/api/index.d.ts +3 -1
  15. package/dist-lib/lib/api/index.js +17 -6
  16. package/dist-lib/lib/api/members.js +13 -29
  17. package/dist-lib/lib/api/mesh.d.ts +2 -1
  18. package/dist-lib/lib/api/projects.d.ts +1 -1
  19. package/dist-lib/lib/api/projects.js +9 -5
  20. package/dist-lib/lib/api/rag-label.d.ts +4 -0
  21. package/dist-lib/lib/api/rag-label.js +21 -0
  22. package/dist-lib/lib/api/rag-lexical.d.ts +22 -0
  23. package/dist-lib/lib/api/rag-lexical.js +1058 -0
  24. package/dist-lib/lib/api/rag-manifest.d.ts +13 -0
  25. package/dist-lib/lib/api/rag-manifest.js +46 -0
  26. package/dist-lib/lib/api/rag-outline.d.ts +36 -0
  27. package/dist-lib/lib/api/rag-outline.js +426 -0
  28. package/dist-lib/lib/api/rag-plugin.d.ts +20 -0
  29. package/dist-lib/lib/api/rag-plugin.js +70 -0
  30. package/dist-lib/lib/api/rag-synthesis.d.ts +7 -0
  31. package/dist-lib/lib/api/rag-synthesis.js +102 -0
  32. package/dist-lib/lib/api/rag-targets.d.ts +23 -0
  33. package/dist-lib/lib/api/rag-targets.js +215 -0
  34. package/dist-lib/lib/api/rag-window.d.ts +5 -0
  35. package/dist-lib/lib/api/rag-window.js +46 -0
  36. package/dist-lib/lib/api/rag.d.ts +3 -2
  37. package/dist-lib/lib/api/rag.js +421 -184
  38. package/dist-lib/lib/api/served.js +8 -14
  39. package/dist-lib/lib/api/workspace.d.ts +8 -1
  40. package/dist-lib/lib/api/workspace.js +27 -13
  41. package/dist-lib/lib/assistant/harness.d.ts +9 -1
  42. package/dist-lib/lib/assistant/history.d.ts +11 -2
  43. package/dist-lib/lib/assistant/history.js +74 -4
  44. package/dist-lib/lib/assistant/media-round.d.ts +61 -0
  45. package/dist-lib/lib/assistant/media-round.js +111 -0
  46. package/dist-lib/lib/assistant/message.d.ts +7 -0
  47. package/dist-lib/lib/assistant/model-round.d.ts +19 -1
  48. package/dist-lib/lib/assistant/model-round.js +163 -28
  49. package/dist-lib/lib/assistant/pdf-pages.d.ts +16 -1
  50. package/dist-lib/lib/assistant/pdf-pages.js +67 -3
  51. package/dist-lib/lib/assistant/round-host.d.ts +22 -0
  52. package/dist-lib/lib/assistant/round-host.js +142 -8
  53. package/dist-lib/lib/assistant/upload.d.ts +12 -0
  54. package/dist-lib/lib/assistant/upload.js +36 -0
  55. package/dist-lib/lib/assistant-client.d.ts +1 -0
  56. package/dist-lib/lib/assistant-client.js +40 -19
  57. package/dist-lib/lib/assistant-tree.d.ts +2 -0
  58. package/dist-lib/lib/assistant-tree.js +48 -1
  59. package/dist-lib/lib/assistant.d.ts +18 -1
  60. package/dist-lib/lib/assistant.js +484 -95
  61. package/dist-lib/lib/client.d.ts +1 -0
  62. package/dist-lib/lib/client.js +4 -1
  63. package/dist-lib/lib/compaction.js +2 -2
  64. package/dist-lib/lib/core/agent.js +12 -45
  65. package/dist-lib/lib/core/approval-rules.js +1 -8
  66. package/dist-lib/lib/core/archived-mesh.d.ts +2 -1
  67. package/dist-lib/lib/core/archived-mesh.js +12 -8
  68. package/dist-lib/lib/core/chat-copy.d.ts +18 -3
  69. package/dist-lib/lib/core/chat-copy.js +74 -25
  70. package/dist-lib/lib/core/chat-fork.js +15 -6
  71. package/dist-lib/lib/core/chat-group.js +16 -24
  72. package/dist-lib/lib/core/chat-move.d.ts +9 -0
  73. package/dist-lib/lib/core/chat-move.js +94 -3
  74. package/dist-lib/lib/core/chat-order.js +12 -18
  75. package/dist-lib/lib/core/chat.js +38 -31
  76. package/dist-lib/lib/core/chunk.d.ts +2 -2
  77. package/dist-lib/lib/core/chunk.js +18 -25
  78. package/dist-lib/lib/core/collections.d.ts +2 -0
  79. package/dist-lib/lib/core/collections.js +2 -0
  80. package/dist-lib/lib/core/contract/activity.d.ts +4 -0
  81. package/dist-lib/lib/core/contract/activity.js +34 -4
  82. package/dist-lib/lib/core/contract/chat-group.d.ts +1 -0
  83. package/dist-lib/lib/core/contract/chat-group.js +21 -1
  84. package/dist-lib/lib/core/contract/chat.d.ts +9 -0
  85. package/dist-lib/lib/core/contract/chat.js +127 -5
  86. package/dist-lib/lib/core/contract/chunks.d.ts +1 -0
  87. package/dist-lib/lib/core/contract/chunks.js +37 -6
  88. package/dist-lib/lib/core/contract/features.d.ts +21 -0
  89. package/dist-lib/lib/core/contract/features.js +22 -0
  90. package/dist-lib/lib/core/contract/index.js +2 -0
  91. package/dist-lib/lib/core/contract/indexing.js +4 -2
  92. package/dist-lib/lib/core/contract/knowledge.js +5 -4
  93. package/dist-lib/lib/core/contract/members.js +16 -10
  94. package/dist-lib/lib/core/contract/mesh-kind.d.ts +1 -0
  95. package/dist-lib/lib/core/contract/mesh-kind.js +3 -1
  96. package/dist-lib/lib/core/contract/projects.d.ts +5 -0
  97. package/dist-lib/lib/core/contract/projects.js +13 -5
  98. package/dist-lib/lib/core/contract/roles.js +32 -4
  99. package/dist-lib/lib/core/contract/run-requests.d.ts +1 -0
  100. package/dist-lib/lib/core/contract/run-requests.js +35 -2
  101. package/dist-lib/lib/core/contract/seed-defaults.js +2 -2
  102. package/dist-lib/lib/core/contract/tool-configs.js +3 -2
  103. package/dist-lib/lib/core/contract/workspace.d.ts +2 -0
  104. package/dist-lib/lib/core/contract/workspace.js +40 -7
  105. package/dist-lib/lib/core/contract/writers.js +2 -2
  106. package/dist-lib/lib/core/departures.d.ts +3 -1
  107. package/dist-lib/lib/core/departures.js +3 -0
  108. package/dist-lib/lib/core/devices.js +1 -8
  109. package/dist-lib/lib/core/files/attachments.d.ts +2 -0
  110. package/dist-lib/lib/core/files/attachments.js +19 -1
  111. package/dist-lib/lib/core/files/index.d.ts +20 -4
  112. package/dist-lib/lib/core/files/index.js +132 -21
  113. package/dist-lib/lib/core/files/processing/document.d.ts +4 -0
  114. package/dist-lib/lib/core/files/processing/document.js +7 -0
  115. package/dist-lib/lib/core/files/processing/index.d.ts +13 -0
  116. package/dist-lib/lib/core/files/processing/index.js +144 -12
  117. package/dist-lib/lib/core/files/processing/protocol.js +13 -34
  118. package/dist-lib/lib/core/files/processing/state.d.ts +2 -0
  119. package/dist-lib/lib/core/files/rag/build.d.ts +19 -2
  120. package/dist-lib/lib/core/files/rag/build.js +123 -19
  121. package/dist-lib/lib/core/files/rag/failure.js +2 -1
  122. package/dist-lib/lib/core/files/rag/index.d.ts +11 -3
  123. package/dist-lib/lib/core/files/rag/index.js +13 -1
  124. package/dist-lib/lib/core/files/rag/load.d.ts +6 -3
  125. package/dist-lib/lib/core/files/rag/load.js +27 -6
  126. package/dist-lib/lib/core/files/rag/plugin-index.d.ts +32 -0
  127. package/dist-lib/lib/core/files/rag/plugin-index.js +127 -0
  128. package/dist-lib/lib/core/files/rag/produce.d.ts +5 -3
  129. package/dist-lib/lib/core/files/rag/produce.js +104 -16
  130. package/dist-lib/lib/core/files/rag/retrieval/full-text.d.ts +3 -1
  131. package/dist-lib/lib/core/files/rag/retrieval/full-text.js +62 -13
  132. package/dist-lib/lib/core/files/rag/text-cache.d.ts +28 -0
  133. package/dist-lib/lib/core/files/rag/text-cache.js +141 -0
  134. package/dist-lib/lib/core/files/records/knowledge.d.ts +1 -3
  135. package/dist-lib/lib/core/files/records/knowledge.js +7 -40
  136. package/dist-lib/lib/core/files/records/reclaim.js +5 -10
  137. package/dist-lib/lib/core/files/upload.js +68 -41
  138. package/dist-lib/lib/core/fs-drives.js +14 -26
  139. package/dist-lib/lib/core/invite.d.ts +1 -0
  140. package/dist-lib/lib/core/invite.js +4 -2
  141. package/dist-lib/lib/core/local/files.d.ts +1 -1
  142. package/dist-lib/lib/core/local/files.js +5 -5
  143. package/dist-lib/lib/core/local/index.d.ts +13 -6
  144. package/dist-lib/lib/core/local/index.js +103 -69
  145. package/dist-lib/lib/core/local/run-logs.d.ts +1 -1
  146. package/dist-lib/lib/core/local/run-logs.js +5 -5
  147. package/dist-lib/lib/core/mesh-processor.d.ts +19 -0
  148. package/dist-lib/lib/core/mesh-processor.js +87 -0
  149. package/dist-lib/lib/core/mesh.d.ts +48 -5
  150. package/dist-lib/lib/core/mesh.js +318 -129
  151. package/dist-lib/lib/core/migrations/agent-catalog.js +2 -8
  152. package/dist-lib/lib/core/migrations/agent-tools-scope.js +5 -11
  153. package/dist-lib/lib/core/migrations/workspace-from-0-21/index.d.ts +88 -0
  154. package/dist-lib/lib/core/migrations/workspace-from-0-21/index.js +731 -0
  155. package/dist-lib/lib/core/migrations/writer-links.d.ts +2 -0
  156. package/dist-lib/lib/core/migrations/writer-links.js +32 -0
  157. package/dist-lib/lib/core/project-keys.d.ts +6 -0
  158. package/dist-lib/lib/core/project-keys.js +11 -0
  159. package/dist-lib/lib/core/public-mesh.d.ts +3 -0
  160. package/dist-lib/lib/core/public-mesh.js +10 -8
  161. package/dist-lib/lib/core/run-requests.d.ts +3 -0
  162. package/dist-lib/lib/core/run-requests.js +21 -1
  163. package/dist-lib/lib/core/skill-availability.d.ts +33 -0
  164. package/dist-lib/lib/core/skill-availability.js +67 -0
  165. package/dist-lib/lib/core/sync/background.d.ts +0 -1
  166. package/dist-lib/lib/core/sync/background.js +2 -1
  167. package/dist-lib/lib/core/sync/changed.d.ts +4 -2
  168. package/dist-lib/lib/core/sync/changed.js +1 -1
  169. package/dist-lib/lib/core/sync/index.js +52 -10
  170. package/dist-lib/lib/core/sync/view-updates.d.ts +18 -12
  171. package/dist-lib/lib/core/sync/view-updates.js +82 -91
  172. package/dist-lib/lib/core/sync-status.js +97 -41
  173. package/dist-lib/lib/core/tool-activity.js +6 -12
  174. package/dist-lib/lib/core/tool-configs.js +17 -29
  175. package/dist-lib/lib/core/upgrade-summary.d.ts +19 -0
  176. package/dist-lib/lib/core/upgrade-summary.js +67 -0
  177. package/dist-lib/lib/core/view-read.d.ts +4 -0
  178. package/dist-lib/lib/core/view-read.js +23 -0
  179. package/dist-lib/lib/core/workspace-upgrade.d.ts +7 -0
  180. package/dist-lib/lib/core/workspace-upgrade.js +27 -16
  181. package/dist-lib/lib/core.d.ts +45 -42
  182. package/dist-lib/lib/core.js +675 -640
  183. package/dist-lib/lib/errors.d.ts +24 -0
  184. package/dist-lib/lib/errors.js +24 -0
  185. package/dist-lib/lib/llm-model-names.d.ts +1 -1
  186. package/dist-lib/lib/llm-model-names.js +1 -1
  187. package/dist-lib/lib/mesh-scoped-engine.js +4 -0
  188. package/dist-lib/lib/model-context.js +1 -2
  189. package/dist-lib/lib/model-download.d.ts +6 -1
  190. package/dist-lib/lib/model-download.js +16 -3
  191. package/dist-lib/lib/oauth/resolve.js +29 -8
  192. package/dist-lib/lib/oauth/token-exchange-core.js +11 -0
  193. package/dist-lib/lib/probe-rounds.d.ts +7 -2
  194. package/dist-lib/lib/probe-rounds.js +6 -1
  195. package/dist-lib/lib/run-transcript.d.ts +4 -1
  196. package/dist-lib/lib/run-transcript.js +15 -6
  197. package/dist-lib/schema/engine.js +83 -6
  198. package/dist-lib/schema/indexes.d.ts +3 -0
  199. package/dist-lib/schema/indexes.js +5 -0
  200. package/dist-lib/schema/local.js +11 -2
  201. package/dist-lib/schema/qvac.js +49 -5
  202. package/dist-lib/spec/bind.js +34 -0
  203. package/dist-lib/spec/calls.js +5 -0
  204. package/dist-lib/spec/capabilities.d.ts +17 -0
  205. package/dist-lib/spec/hrpc/hrpc.json +65 -0
  206. package/dist-lib/spec/hrpc/index.js +68 -5
  207. package/dist-lib/spec/hrpc/messages.js +260 -23
  208. package/dist-lib/spec/hyperschema/index.js +260 -23
  209. package/dist-lib/spec/hyperschema/schema.json +181 -0
  210. package/dist-lib/spec/hyperschema/types.d.ts +48 -0
  211. package/dist-lib/spec/local/hyperdb/messages.js +270 -224
  212. package/dist-lib/spec/local/hyperschema/index.js +73 -38
  213. package/dist-lib/spec/local/hyperschema/schema.json +31 -0
  214. package/dist-lib/spec/local/hyperschema/types.d.ts +8 -0
  215. package/dist-lib/spec/mesh/hyperdb/db.json +39 -2
  216. package/dist-lib/spec/mesh/hyperdb/index.js +120 -3
  217. package/dist-lib/spec/mesh/hyperdb/messages.js +1328 -838
  218. package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +30 -0
  219. package/dist-lib/spec/mesh/hyperdispatch/index.js +86 -1
  220. package/dist-lib/spec/mesh/hyperdispatch/messages.js +682 -287
  221. package/dist-lib/spec/mesh/hyperschema/index.js +682 -287
  222. package/dist-lib/spec/mesh/hyperschema/schema.json +201 -0
  223. package/dist-lib/spec/mesh/hyperschema/types.d.ts +52 -0
  224. package/package.json +82 -71
  225. package/tui/README.md +52 -0
  226. package/tui/app.mjs +69 -12
  227. package/tui/boot.mjs +27 -6
  228. package/tui/cli.mjs +93 -0
  229. package/tui/index.mjs +2 -87
  230. package/tui/lib/attachment-files.mjs +82 -0
  231. package/tui/lib/reopen-meshes.mjs +15 -0
  232. package/tui/lib/updater.mjs +86 -0
  233. package/tui/main.mjs +29 -57
  234. package/worker/assistant-sidecar.mjs +10 -3
  235. package/worker/stow-entry.mjs +3 -52
  236. package/dist-lib/lib/core/chat-project.d.ts +0 -2
  237. package/dist-lib/lib/core/chat-project.js +0 -15
@@ -5,9 +5,10 @@ import fs from 'fs';
5
5
  import crypto from 'hypercore-crypto';
6
6
  import os from 'os';
7
7
  import path from 'path';
8
- import { isMobilePlatform, mediaModelNameFor, modelsForAgent, runnableModelName } from '@qvac/harness/lib/models/model-catalog.ts';
8
+ import { isMobilePlatform, mediaModelNameFor, modelsForAgent, runnableModelName, skillModelsForAgent } from '@qvac/harness/lib/models/model-catalog.ts';
9
9
  import * as z from 'zod';
10
10
  import { createHostRunLogAdapter, loggedMessages } from './assistant/host-run-log.js';
11
+ import { readUploadAttachment, writeUploadFrame } from './assistant/upload.js';
11
12
  import { SKILL_STATE } from '@qvac/harness/skill-state';
12
13
  import { toolsetEnabled } from '@qvac/harness/lib/tools/toolsets.ts';
13
14
  import { RAG_CITATION_LINE } from '@qvac/harness/lib/prompt.ts';
@@ -44,6 +45,7 @@ import { buildCompactionPrompt, canCompact, foldReducesPrompt, COMPACTION_KEEP_T
44
45
  import { ModelDownloadReconciler, musicModelChoiceOf } from './model-download.js';
45
46
  import { AbortedError, raceAbort } from '@qvac/harness/race-abort';
46
47
  import { RunTranscript } from './run-transcript.js';
48
+ import { enabledSkillsOf, loadableSkills } from './core/skill-availability.js';
47
49
  import { OwnerRounds, listRoundChunks, wholeImageId } from './assistant/model-round.js';
48
50
  import { RoundHost } from './assistant/round-host.js';
49
51
  import { searchKnowledge } from './knowledge-search.js';
@@ -57,9 +59,11 @@ import { packVideoDigest, parseVideoDigestHeader, unpackVideoDigest, VIDEO_DIGES
57
59
  import { VIDEO_FRAME_EDGE, videoFrameCount } from './assistant/video-budget.js';
58
60
  import { drainBody, raceInactivity } from './assistant/fetch-body.js';
59
61
  import { pdfImageRoom } from './assistant/pdf-budget.js';
60
- import { packPdfSelection, parsePdfSelection, PDF_PAGES_MIME, pdfSelectionFileName, selectPdfPages } from './assistant/pdf-pages.js';
62
+ import { asksForPdfVisuals, citedPdfPages, packPdfSelection, parsePdfSelection, PDF_PAGES_MIME, pdfSelectionFileName, readablePdfTextPages, selectPdfPages } from './assistant/pdf-pages.js';
61
63
  import { effectiveCtxSize } from './api/effective-ctx-size.js';
62
- import { GENERIC_ACKNOWLEDGEMENTS, HISTORY_TYPES, SKILL_TOOL, TERMINAL_STATES, foldableView, historyFromChunks, listHistoryChunks, replayFromChunks, runPromptView, runView, senderOf, staleToolRunIds, viewOutgrewCache } from './assistant/history.js';
64
+ import { shortLabels } from './api/rag-label.js';
65
+ import { UNREAD } from './api/rag-manifest.js';
66
+ import { GENERIC_ACKNOWLEDGEMENTS, HISTORY_TYPES, SKILL_TOOL, TERMINAL_STATES, carriedInput, carriedRun, foldableView, historyFromChunks, listHistoryChunks, replayFromChunks, runPromptView, runView, senderOf, staleToolRunIds, viewOutgrewCache } from './assistant/history.js';
63
67
  import { errorMessage, isRecord } from './guards.js';
64
68
  import { CoreError, isCoreError } from './errors.js';
65
69
  export { harnessClearKvCache, harnessDeleteModel, harnessListModels, harnessPreviewModelSource, harnessSkillCatalog, harnessSkillManagement, syncedSkills, harnessDetectLanguage, harnessSpeak, harnessSpeakStream, harnessTools, harnessTranscribe } from './assistant/harness.js';
@@ -134,6 +138,7 @@ export const SUGGEST_PROMPTS_KEY = 'assistant.suggestPrompts';
134
138
  export const SUGGEST_PROMPTS_ON = 'on';
135
139
  const SUGGESTION_SUPERSEDED = "superseded by the chat's next run";
136
140
  const NO_PDF_PAGES = { selection: { pageCount: 0, shots: [] }, names: [], labels: [] };
141
+ const NO_CITED_PAGES = new Set();
137
142
  function pdfNotices(fileName, pages, wholeText) {
138
143
  const { cutAt, pageCount } = pages.selection;
139
144
  const shown = pages.names.length;
@@ -211,6 +216,7 @@ const AGENT_REMOVED = RUN_ABORT_REASONS.agentRemoved;
211
216
  const RUNNER_DIED = RUN_ABORT_REASONS.runnerDied;
212
217
  const RUNNER_CLOSED = RUN_ABORT_REASONS.runnerClosed;
213
218
  const MODEL_UNLOADED = RUN_ABORT_REASONS.modelUnloaded;
219
+ const AGENT_READ_CUT = 'agent-read-cut';
214
220
  const RUN_PAUSED = 'paused on a saved checkpoint; it resumes from there';
215
221
  function recoveryReason(event) {
216
222
  const operation = isRecord(event.operation) ? event.operation.name : undefined;
@@ -237,6 +243,39 @@ function modelLeft(before, after, deviceId) {
237
243
  const stays = after && runsHere(after, deviceId) && after.modelName === before.modelName;
238
244
  return stays ? null : before.modelName;
239
245
  }
246
+ function agentRefused(err) {
247
+ return isCoreError(err, 'AGENT_NOT_FOUND') || isCoreError(err, 'CHAT_AGENT_UNAVAILABLE');
248
+ }
249
+ function watchEdge(streams) {
250
+ const armed = Promise.withResolvers();
251
+ const fired = Promise.withResolvers();
252
+ let opening = streams.length;
253
+ for (const frames of streams) {
254
+ void (async () => {
255
+ let first = true;
256
+ for await (const _ of frames) {
257
+ if (!first) {
258
+ fired.resolve(true);
259
+ return;
260
+ }
261
+ first = false;
262
+ if (--opening === 0) {
263
+ armed.resolve(true);
264
+ }
265
+ }
266
+ })()
267
+ .catch(noop)
268
+ .finally(() => {
269
+ armed.resolve(false);
270
+ fired.resolve(false);
271
+ });
272
+ }
273
+ return {
274
+ armed: armed.promise,
275
+ fired: fired.promise,
276
+ stop: () => streams.forEach((frames) => frames.destroy())
277
+ };
278
+ }
240
279
  function runsHere(agent, deviceId) {
241
280
  return (!agent.deletedAt && !!agent.providerDeviceId && b4a.equals(agent.providerDeviceId, deviceId));
242
281
  }
@@ -284,6 +323,7 @@ export class QvacAssistant extends ReadyResource {
284
323
  _listed = [];
285
324
  _accountHosted = [];
286
325
  _readySkills;
326
+ _skillsHere = null;
287
327
  _recordingReadiness = false;
288
328
  _readinessDirty = false;
289
329
  _reconciling;
@@ -310,6 +350,7 @@ export class QvacAssistant extends ReadyResource {
310
350
  _idleWaiters = new Set();
311
351
  _seen;
312
352
  _parked;
353
+ _retakes;
313
354
  _handedOff = new Map();
314
355
  _backgroundReplays;
315
356
  _inferenceCalls;
@@ -329,6 +370,7 @@ export class QvacAssistant extends ReadyResource {
329
370
  _runImageCacheNames;
330
371
  _reconciledCompactions;
331
372
  _knowledgeCaps = new Map();
373
+ _excerptsOnly = new Set();
332
374
  _modelCatalog = new ModelCatalogRevisionHub();
333
375
  _onHarnessStatus;
334
376
  _onModelCatalogEvent;
@@ -350,6 +392,7 @@ export class QvacAssistant extends ReadyResource {
350
392
  this._modelReloadDelayMs = modelReloadDelayMs;
351
393
  this._pdfRasterizer = pdfRasterizerOption ?? createPdfRasterizer(logger);
352
394
  const serve = harness.serveModelRound?.bind(harness);
395
+ const generate = harness.serveMediaRound?.bind(harness);
353
396
  this._roundHost = serve
354
397
  ? new RoundHost({
355
398
  engine,
@@ -359,7 +402,14 @@ export class QvacAssistant extends ReadyResource {
359
402
  resolve: (sources, served) => this._servedMedia(sources, served),
360
403
  release: (id) => this._runImageCacheNames.delete(`round:${id}`)
361
404
  },
405
+ load: (scoped, agentId) => this._servedLoad(scoped, agentId),
362
406
  built: (meshId, chatId) => this._recordKvCache(meshId, chatId),
407
+ generation: generate
408
+ ? {
409
+ serve: generate,
410
+ ready: (wait) => this._generationReady(wait)
411
+ }
412
+ : null,
363
413
  log: logger
364
414
  })
365
415
  : null;
@@ -383,6 +433,7 @@ export class QvacAssistant extends ReadyResource {
383
433
  this._runs = new Map();
384
434
  this._seen = new Map();
385
435
  this._parked = new Map();
436
+ this._retakes = new Map();
386
437
  this._backgroundReplays = new Set();
387
438
  this._inferenceCalls = new InferenceCallStore(inferenceCallsDir ?? persistentDir('inspector'));
388
439
  this._runReplay = new RunReplayStore(runReplayDir ?? persistentDir('run-replay'));
@@ -969,6 +1020,17 @@ export class QvacAssistant extends ReadyResource {
969
1020
  this._log?.info('models', 'model load requested', { model: name, loaded, reason });
970
1021
  return { loaded, reason };
971
1022
  }
1023
+ async _servedLoad(engine, agentId) {
1024
+ const meshId = this._accountMeshId;
1025
+ const agent = (await engine.getAgent({ agentId }).catch(noop)) ??
1026
+ (meshId ? await this._engine.getAgent({ agentId, meshId }).catch(noop) : undefined);
1027
+ if (!agent) {
1028
+ return undefined;
1029
+ }
1030
+ const config = await this._engine.getConfig({}).catch(noop);
1031
+ const { ctxSize, kvCacheK, kvCacheV } = withConfigDefaults(agent, config);
1032
+ return { ctxSize, kvCacheK, kvCacheV };
1033
+ }
972
1034
  async _isCached(model) {
973
1035
  const rows = await this._harness.list?.({ cachedOnly: true }).catch(noop);
974
1036
  return rows?.some((row) => row.name === model && row.isCached) === true;
@@ -1115,6 +1177,26 @@ export class QvacAssistant extends ReadyResource {
1115
1177
  }));
1116
1178
  return perAgent.flat();
1117
1179
  }
1180
+ async _generationReady({ engine, agentId, kind, signal }) {
1181
+ const agent = await engine.getAgent({ agentId }).catch(() => null);
1182
+ if (!agent) {
1183
+ return false;
1184
+ }
1185
+ const configs = kind === 'music'
1186
+ ? await engine
1187
+ .listToolConfigs({ agentId })
1188
+ .then((result) => result.configs ?? [])
1189
+ .catch(() => [])
1190
+ : [];
1191
+ const musicModelChoice = musicModelChoiceOf(configs);
1192
+ const host = await this._runnableHost();
1193
+ const model = skillModelsForAgent({ ...agent, musicModelChoice }, host).find((entry) => entry.use === kind);
1194
+ if (!model) {
1195
+ return false;
1196
+ }
1197
+ await this._downloads?.waitUntilCached(model.name, signal);
1198
+ return !signal.aborted;
1199
+ }
1118
1200
  async _runModel(agent, deviceId) {
1119
1201
  if (!b4a.equals(agent.providerDeviceId, deviceId))
1120
1202
  return agent.modelName;
@@ -1131,11 +1213,14 @@ export class QvacAssistant extends ReadyResource {
1131
1213
  this._modelCatalog.notify();
1132
1214
  this._reportCapabilities().catch(noop);
1133
1215
  }
1134
- async retryModelDownload(name) {
1216
+ async retryModelDownload(name, options) {
1135
1217
  if (!this._downloads) {
1136
1218
  throw new CoreError('MODEL_DOWNLOAD_UNAVAILABLE');
1137
1219
  }
1138
- await this._downloads.retry(name);
1220
+ await this._downloads.retry(name, options);
1221
+ }
1222
+ siblingResumedDownload(name) {
1223
+ this._downloads?.siblingResumed(name);
1139
1224
  }
1140
1225
  async skills(agentId, modelName, options) {
1141
1226
  if (!this.opened)
@@ -1155,6 +1240,36 @@ export class QvacAssistant extends ReadyResource {
1155
1240
  this._recordSkillReadiness().catch(noop);
1156
1241
  return resolved;
1157
1242
  }
1243
+ async _skillsNotHere(agent) {
1244
+ const all = agent.skillsScope === 'all';
1245
+ if (!this._mobileClass || !this._harness.skills || (!all && !agent.enabledSkills?.length)) {
1246
+ return [];
1247
+ }
1248
+ const here = this._skillsHere ?? (await this._readSkillsHere());
1249
+ if (!here) {
1250
+ return [];
1251
+ }
1252
+ const provider = all ? await this._skillsOn(agent.providerDeviceId) : null;
1253
+ return enabledSkillsOf(agent, here, provider).filter((name) => !here.has(name));
1254
+ }
1255
+ async _skillsOn(deviceId) {
1256
+ const meshId = this._accountMeshId;
1257
+ const device = meshId
1258
+ ? await this._engine.getDevice({ meshId, deviceId }).catch(noop)
1259
+ : undefined;
1260
+ const names = device?.installedSkillNames ?? [];
1261
+ return names.length ? new Set(names) : null;
1262
+ }
1263
+ async _readSkillsHere() {
1264
+ try {
1265
+ const catalog = await this._harness.skills?.(null, null, { skipCredentialProbe: true });
1266
+ this._skillsHere = catalog ? loadableSkills(catalog) : null;
1267
+ }
1268
+ catch (err) {
1269
+ this._log?.warn('runs', 'skill catalog unread, the run records no missing skill', err);
1270
+ }
1271
+ return this._skillsHere;
1272
+ }
1158
1273
  async _recordSkillReadiness() {
1159
1274
  this._readinessDirty = true;
1160
1275
  if (this._recordingReadiness) {
@@ -1178,6 +1293,7 @@ export class QvacAssistant extends ReadyResource {
1178
1293
  }
1179
1294
  async _readySkillNames() {
1180
1295
  const unscoped = (await this._harness.skills?.(null, null, { skipCredentialProbe: true })) ?? [];
1296
+ this._skillsHere = loadableSkills(unscoped);
1181
1297
  const ready = unscoped.filter((skill) => isSetUpHere(skill.status)).map((skill) => skill.name);
1182
1298
  const deviceId = this._deviceId;
1183
1299
  const meshId = this._accountMeshId;
@@ -1330,11 +1446,19 @@ export class QvacAssistant extends ReadyResource {
1330
1446
  }
1331
1447
  async _reconcileCompaction(engine, chatId) {
1332
1448
  const deviceId = this._requireDeviceId();
1333
- const { chunks } = await engine
1334
- .listChunks({ chatId, type: 'compaction' })
1335
- .catch(() => ({ chunks: [] }));
1449
+ const chunks = await engine.listChunks({ chatId, type: 'compaction' }).then((listed) => listed.chunks ?? [], (err) => {
1450
+ this._log?.warn('assistant', 'compaction state unreadable, dropping the cache', {
1451
+ chatId,
1452
+ err
1453
+ });
1454
+ return null;
1455
+ });
1456
+ if (!chunks) {
1457
+ await this._deleteChatKvCache(chatId, 'compaction state unreadable');
1458
+ return;
1459
+ }
1336
1460
  let newest = null;
1337
- for (const chunk of chunks ?? []) {
1461
+ for (const chunk of chunks) {
1338
1462
  if (!newest || chunk.seq > newest.seq) {
1339
1463
  newest = chunk;
1340
1464
  }
@@ -1468,6 +1592,7 @@ export class QvacAssistant extends ReadyResource {
1468
1592
  sentRuns,
1469
1593
  askedRuns,
1470
1594
  readRuns: served.readRuns,
1595
+ shownRuns: served.shownRuns,
1471
1596
  rebuild: await this._runReplay.rebuild({ holder: LOCAL_HOLDER, chatId })
1472
1597
  };
1473
1598
  }
@@ -1478,6 +1603,7 @@ export class QvacAssistant extends ReadyResource {
1478
1603
  sentRuns: new Map(),
1479
1604
  askedRuns: new Map(),
1480
1605
  readRuns: new Map(),
1606
+ shownRuns: new Map(),
1481
1607
  rebuild: await this._runReplay.rebuild({ holder: LOCAL_HOLDER, chatId })
1482
1608
  };
1483
1609
  }
@@ -1556,6 +1682,7 @@ export class QvacAssistant extends ReadyResource {
1556
1682
  if (consumed.has(meshId))
1557
1683
  continue;
1558
1684
  consumer.stop();
1685
+ this._roundHost?.drop(meshId);
1559
1686
  this._meshConsumers.delete(meshId);
1560
1687
  this._pruneSeen(meshId, new Set());
1561
1688
  for (const chatId of this._servedChats.get(meshId) ?? []) {
@@ -1660,6 +1787,9 @@ export class QvacAssistant extends ReadyResource {
1660
1787
  for (const consumer of this._meshConsumers.values()) {
1661
1788
  consumer.stop();
1662
1789
  }
1790
+ for (const edge of this._retakes.values()) {
1791
+ edge.stop();
1792
+ }
1663
1793
  this._meshConsumers.clear();
1664
1794
  this._advertisedOn = null;
1665
1795
  }
@@ -1702,7 +1832,11 @@ export class QvacAssistant extends ReadyResource {
1702
1832
  this._chats.delete(chat);
1703
1833
  }
1704
1834
  void this._reportCapabilities();
1705
- this._requeue(runId, run.controller.signal.reason, () => this._dispatch(run.request, meshId));
1835
+ this._requeue(runId, {
1836
+ meshId,
1837
+ reason: run.controller.signal.reason,
1838
+ replay: () => this._dispatch(run.request, meshId)
1839
+ });
1706
1840
  }
1707
1841
  })();
1708
1842
  settle.catch((err) => this._log?.error('assistant', `run ${runId} failed`, err));
@@ -1731,7 +1865,17 @@ export class QvacAssistant extends ReadyResource {
1731
1865
  }
1732
1866
  async _claim(engine, request, { runId, meshId }) {
1733
1867
  const agentId = request.runRequest?.agentId;
1734
- const agent = agentId ? await engine.getAgent({ agentId }).catch(noop) : undefined;
1868
+ let agent;
1869
+ try {
1870
+ agent = agentId ? await engine.getAgent({ agentId }) : undefined;
1871
+ }
1872
+ catch (err) {
1873
+ this._seen.delete(runId);
1874
+ if (!agentRefused(err)) {
1875
+ this._retakeOnSync(runId, meshId, () => this._dispatch(request, meshId));
1876
+ }
1877
+ return undefined;
1878
+ }
1735
1879
  if (!agent) {
1736
1880
  this._seen.delete(runId);
1737
1881
  return undefined;
@@ -1777,7 +1921,7 @@ export class QvacAssistant extends ReadyResource {
1777
1921
  running.steering.push({ mode: 'steer', text: turn.content, id: request.runId });
1778
1922
  }
1779
1923
  }
1780
- _requeue(runId, reason, replay) {
1924
+ _requeue(runId, { meshId, reason, replay }) {
1781
1925
  const background = this._backgroundReplays.delete(runId);
1782
1926
  if (this._seen.has(runId) || this.closing) {
1783
1927
  return;
@@ -1788,6 +1932,31 @@ export class QvacAssistant extends ReadyResource {
1788
1932
  else if ((background && !this._backgrounded) || reason === MODEL_UNLOADED) {
1789
1933
  replay();
1790
1934
  }
1935
+ else if (reason === AGENT_READ_CUT) {
1936
+ this._retakeOnSync(runId, meshId, replay);
1937
+ }
1938
+ }
1939
+ _retakeOnSync(runId, meshId, replay) {
1940
+ const probe = !this._retakes.has(runId);
1941
+ this._retakes.get(runId)?.stop();
1942
+ const edge = watchEdge([
1943
+ this._engine.syncStatusWatch({ meshId }),
1944
+ this._engine.meshStatusWatch({})
1945
+ ]);
1946
+ this._retakes.set(runId, edge);
1947
+ void (async () => {
1948
+ if (probe && (await edge.armed)) {
1949
+ replay();
1950
+ }
1951
+ const fired = await edge.fired;
1952
+ edge.stop();
1953
+ if (this._retakes.get(runId) === edge) {
1954
+ this._retakes.delete(runId);
1955
+ }
1956
+ if (fired) {
1957
+ replay();
1958
+ }
1959
+ })();
1791
1960
  }
1792
1961
  async _citeWebSources(transcript, runId, result) {
1793
1962
  const records = webSourcesFromToolResult(result, WEB_RAG_SOURCE_BOUNDS);
@@ -1801,14 +1970,9 @@ export class QvacAssistant extends ReadyResource {
1801
1970
  this._log?.warn('assistant', 'web sources chunk skipped', { runId, err });
1802
1971
  }
1803
1972
  }
1804
- async _searchKnowledge(engine, messages, { chatId, attachmentIds, signal, preparing }) {
1805
- let lastUser;
1806
- for (let index = messages.length - 1; index >= 0; index--) {
1807
- if (messages[index].role === 'user') {
1808
- lastUser = messages[index];
1809
- break;
1810
- }
1811
- }
1973
+ async _searchKnowledge(engine, messages, { chatId, attachmentIds, signal, preparing, previousQuery, excerptsOnly }) {
1974
+ const users = messages.filter((turn) => turn.role === 'user');
1975
+ const lastUser = users.at(-1);
1812
1976
  const query = lastUser?.content.trim();
1813
1977
  if (!lastUser || !query) {
1814
1978
  return null;
@@ -1817,6 +1981,8 @@ export class QvacAssistant extends ReadyResource {
1817
1981
  const search = await searchKnowledge(engine, {
1818
1982
  chatId,
1819
1983
  query,
1984
+ previousQuery: attachmentIds.length === 0 ? previousQuery : undefined,
1985
+ excerptsOnly,
1820
1986
  attachmentIds: attachmentIds.length > 0 ? [...attachmentIds] : undefined
1821
1987
  }, { signal, preparing });
1822
1988
  if (!search.enabled) {
@@ -1896,6 +2062,10 @@ export class QvacAssistant extends ReadyResource {
1896
2062
  return null;
1897
2063
  }
1898
2064
  }
2065
+ async _callsOf(engine, chatId, runId) {
2066
+ const { chunks } = await engine.listChunks({ chatId, type: 'tool-call' });
2067
+ return (chunks ?? []).filter((chunk) => chunk.runId === runId);
2068
+ }
1899
2069
  async _isHandled(engine, request) {
1900
2070
  const { chunks } = await engine.listChunks({
1901
2071
  chatId: request.chatId,
@@ -1916,8 +2086,29 @@ export class QvacAssistant extends ReadyResource {
1916
2086
  if (await this._isHandled(engine, request)) {
1917
2087
  return;
1918
2088
  }
1919
- const fresh = await engine.getAgent({ agentId: agent.id }).catch(noop);
1920
- if (!fresh || fresh.deletedAt || !drivenBy({ request, agent: fresh }, deviceId)) {
2089
+ let fresh;
2090
+ try {
2091
+ fresh = await engine.getAgent({ agentId: agent.id });
2092
+ }
2093
+ catch (err) {
2094
+ if (agentRefused(err)) {
2095
+ const refused = new RunTranscript(engine, {
2096
+ chatId,
2097
+ runId,
2098
+ deviceId,
2099
+ model: agent.modelName,
2100
+ log: this._log
2101
+ });
2102
+ refused.close();
2103
+ await refused.status({ state: 'failed', reason: runFailureNotice(err) });
2104
+ return;
2105
+ }
2106
+ this._log?.debug('runs', 'agent read cut short', { runId, err });
2107
+ this._seen.delete(runId);
2108
+ controller.abort(AGENT_READ_CUT);
2109
+ return;
2110
+ }
2111
+ if (fresh.deletedAt || !drivenBy({ request, agent: fresh }, deviceId)) {
1921
2112
  return;
1922
2113
  }
1923
2114
  agent = fresh;
@@ -1949,7 +2140,11 @@ export class QvacAssistant extends ReadyResource {
1949
2140
  }
1950
2141
  const hold = await this._openRunLog(engine, runId);
1951
2142
  const runLog = hold?.runLog;
1952
- const resumed = Boolean(hold?.lease.boot?.metadata);
2143
+ const logged = Boolean(hold?.lease.boot?.metadata);
2144
+ const carried = logged || !request.runnerDeviceId
2145
+ ? null
2146
+ : carriedRun(await listRoundChunks(engine, chatId), runId);
2147
+ const resumed = logged || carried !== null;
1953
2148
  const releaseRunLog = async () => {
1954
2149
  await hold?.runLog
1955
2150
  .release(runId, hold.lease.generation)
@@ -1988,6 +2183,7 @@ export class QvacAssistant extends ReadyResource {
1988
2183
  runId,
1989
2184
  deviceId,
1990
2185
  model,
2186
+ unavailableSkills: await this._skillsNotHere(agent),
1991
2187
  log: this._log
1992
2188
  });
1993
2189
  const rounds = new OwnerRounds({
@@ -1998,7 +2194,10 @@ export class QvacAssistant extends ReadyResource {
1998
2194
  deviceId,
1999
2195
  from: request.seq,
2000
2196
  media: (file) => this._mediaRefs.get(path.basename(file)),
2001
- inspect: (call) => void this._inspectRound(chatId, { runId, at: Date.now(), call })
2197
+ meshId,
2198
+ local: this._harness.serveMediaRound?.bind(this._harness),
2199
+ inspect: (call) => void this._inspectRound(chatId, { runId, at: Date.now(), call }),
2200
+ log: this._log
2002
2201
  });
2003
2202
  await rounds.retire();
2004
2203
  const folded = !resumed &&
@@ -2035,6 +2234,7 @@ export class QvacAssistant extends ReadyResource {
2035
2234
  sentRuns: new Map(),
2036
2235
  askedRuns: new Map(),
2037
2236
  readRuns: new Map(),
2237
+ shownRuns: new Map(),
2038
2238
  rebuild: await this._runReplay.rebuild({ holder: LOCAL_HOLDER, chatId })
2039
2239
  };
2040
2240
  }
@@ -2065,7 +2265,7 @@ export class QvacAssistant extends ReadyResource {
2065
2265
  await this._storeVideoDigests(sampled.splice(0), { meshId, chatId, runId, deviceId });
2066
2266
  }
2067
2267
  };
2068
- let pdfPages = { rendered: new Set(), notices: [] };
2268
+ let pdfPages = { rendered: new Set(), shown: false, notices: [] };
2069
2269
  const selectedPdfs = [];
2070
2270
  if (!seesImages) {
2071
2271
  const dropped = messages.reduce((count, turn) => count + (turn.images?.length ?? 0), 0);
@@ -2079,6 +2279,7 @@ export class QvacAssistant extends ReadyResource {
2079
2279
  }
2080
2280
  delete turn.images;
2081
2281
  delete turn.pdfs;
2282
+ delete turn.pdfShots;
2082
2283
  }
2083
2284
  }
2084
2285
  else {
@@ -2159,12 +2360,15 @@ export class QvacAssistant extends ReadyResource {
2159
2360
  if (held.status) {
2160
2361
  await transcript.progress(runStatusId, runStatus);
2161
2362
  }
2363
+ const earlierCalls = carried ? await this._callsOf(engine, chatId, runId) : [];
2364
+ const roundBase = carried?.firstRound ?? 0;
2162
2365
  const approvals = this._createApprove(engine, {
2163
2366
  chatId,
2164
2367
  runId,
2165
2368
  deviceId,
2166
2369
  agentId: agent.id,
2167
- hasRun: () => ran
2370
+ hasRun: () => ran,
2371
+ repeats: (call) => earlierCalls.some((chunk) => sameToolCall(chunk, call))
2168
2372
  });
2169
2373
  const idle = idleWatch(controller, this._runIdleTimeoutMs);
2170
2374
  const modelWatch = this._watchModelProgress((runStatus) => transcript.progress(runStatusId, runStatus), busyChatId !== null);
@@ -2195,6 +2399,7 @@ export class QvacAssistant extends ReadyResource {
2195
2399
  let primeRound;
2196
2400
  let primeTokens;
2197
2401
  let injectedHits = 0;
2402
+ let injectedWhole = false;
2198
2403
  const writeText = async (type, raw) => {
2199
2404
  const text = type === 'content' ? sourceTagFilter.push(raw) : raw;
2200
2405
  if (!text) {
@@ -2206,11 +2411,12 @@ export class QvacAssistant extends ReadyResource {
2206
2411
  await transcript.text(type, text, round);
2207
2412
  };
2208
2413
  const writeRoundText = async (type, text) => {
2209
- if (!rounds.echoed(type, text)) {
2210
- await writeText(type, text);
2414
+ const own = rounds.unechoed(type, text);
2415
+ if (type === 'content' && own.length < text.length) {
2416
+ answer(text.slice(0, text.length - own.length));
2211
2417
  }
2212
- else if (type === 'content') {
2213
- answer(text);
2418
+ if (own) {
2419
+ await writeText(type, own);
2214
2420
  }
2215
2421
  };
2216
2422
  const failRun = async (message) => {
@@ -2230,9 +2436,14 @@ export class QvacAssistant extends ReadyResource {
2230
2436
  this._knowledgeCaps.set(chatId, Math.floor(injectedHits / 2));
2231
2437
  this._log?.info('runs', 'knowledge shrunk for retry', { runId, hits: injectedHits });
2232
2438
  }
2439
+ const excerpted = overflowed && injectedWhole;
2440
+ if (excerpted) {
2441
+ this._excerptsOnly.add(chatId);
2442
+ this._log?.info('runs', 'whole read replaced by excerpts for retry', { runId });
2443
+ }
2233
2444
  runFailure = runFailureNotice(message);
2234
2445
  await transcript.status({ state: 'failed', reason: runFailure, metrics });
2235
- if (folded || shrunk) {
2446
+ if (folded || shrunk || excerpted) {
2236
2447
  await engine
2237
2448
  .runAgent({ chatId, agentId: agent.id, parentRunId: runId })
2238
2449
  .catch((err) => this._log?.warn('runs', 'compaction retry not queued', { runId, err }));
@@ -2301,6 +2512,8 @@ export class QvacAssistant extends ReadyResource {
2301
2512
  ? await this._searchKnowledge(engine, messages, {
2302
2513
  chatId,
2303
2514
  attachmentIds: attachTurnIds,
2515
+ previousQuery: previousQuestion(history),
2516
+ excerptsOnly: this._excerptsOnly.delete(chatId),
2304
2517
  signal: controller.signal,
2305
2518
  preparing: (active) => {
2306
2519
  this._markPreparing(runId, active);
@@ -2326,15 +2539,22 @@ export class QvacAssistant extends ReadyResource {
2326
2539
  const cap = this._knowledgeCaps.get(chatId);
2327
2540
  this._knowledgeCaps.delete(chatId);
2328
2541
  const fitted = knowledge && cap ? { ...knowledge, hits: knowledge.hits.slice(0, cap) } : knowledge;
2542
+ const userQuestion = messages.findLast((turn) => turn.role === 'user')?.content ?? '';
2543
+ const visualRequest = asksForPdfVisuals(userQuestion);
2329
2544
  const injected = fitted
2330
2545
  ? await this._injectKnowledge(fitted, {
2331
2546
  transcript,
2332
2547
  sourceTagFilter,
2333
- retrievedRecords,
2334
- pictured: seesImages && messages.some((turn) => turn.pdfs?.length)
2548
+ retrievedRecords
2335
2549
  })
2336
2550
  : { documentsInjected: false, sourcesWritten: false, ragInjected: false };
2337
2551
  injectedHits = injected.ragInjected && fitted ? fitted.hits.length : 0;
2552
+ injectedWhole =
2553
+ toolsetEnabled('knowledge', runAgent.disabledToolsets) &&
2554
+ injected.documentsInjected &&
2555
+ !injected.ragInjected &&
2556
+ !!fitted &&
2557
+ !fitted.hits.some(isTruncatedFullTextHit);
2338
2558
  const hasAudio = messages.some((turn) => turn.audioAttachments?.length || turn.videos?.length);
2339
2559
  if (hasAudio) {
2340
2560
  modelWatch.transcribe(true);
@@ -2365,9 +2585,18 @@ export class QvacAssistant extends ReadyResource {
2365
2585
  .filter((hit) => !isTruncatedFullTextHit(hit))
2366
2586
  .map(fullTextAttachmentId)
2367
2587
  .filter((id) => id !== null)),
2588
+ citedPages: injected.ragInjected ? citedPdfPages(hits) : null,
2589
+ readablePages: readablePdfTextPages(hits),
2590
+ visualRequest,
2368
2591
  selected: selectedPdfs
2369
2592
  });
2370
2593
  }
2594
+ if (pdfPages.shown) {
2595
+ const lastUser = messages.findLast((turn) => turn.role === 'user');
2596
+ if (lastUser && !lastUser.content.includes(PDF_PAGES_SOURCE_LINE)) {
2597
+ lastUser.content = `${lastUser.content}\n\n${PDF_PAGES_SOURCE_LINE}`;
2598
+ }
2599
+ }
2371
2600
  annotateDocumentAttachments(messages);
2372
2601
  for (const { turn, notice } of pdfPages.notices) {
2373
2602
  turn.content = `${turn.content}\n${notice}`;
@@ -2422,9 +2651,11 @@ export class QvacAssistant extends ReadyResource {
2422
2651
  restoreSentContent(messages);
2423
2652
  asked = messages.findLast((turn) => turn.role === 'user')?.content;
2424
2653
  }
2425
- const input = resent ?? nameSenders(messages);
2426
- inputLength = input.length;
2654
+ const named = nameSenders(messages);
2655
+ const input = resent ?? (carried ? carriedInput(named, carried) : named);
2656
+ inputLength = resent ? input.length : named.length;
2427
2657
  rounds.inputLength = inputLength;
2658
+ rounds.roundBase = roundBase;
2428
2659
  const sentImages = earlierImages(messages);
2429
2660
  const inspecting = await this._capturingInferenceCalls();
2430
2661
  steering = new RunInputQueue();
@@ -2444,6 +2675,9 @@ export class QvacAssistant extends ReadyResource {
2444
2675
  replaysContext: true,
2445
2676
  deferTools: runAgent.shared === true || !!request.requesterMemberId,
2446
2677
  modelRound: idle.parks(rounds.port),
2678
+ mediaRound: b4a.equals(agent.providerDeviceId, deviceId)
2679
+ ? undefined
2680
+ : idle.parks(rounds.media),
2447
2681
  session: chatKvCacheKey(chatId),
2448
2682
  thinkingDefault: thinkingDefault(config),
2449
2683
  toolConfigs,
@@ -2452,7 +2686,13 @@ export class QvacAssistant extends ReadyResource {
2452
2686
  driveOp,
2453
2687
  grantedWriteDirs: write,
2454
2688
  approve: idle.parks(approvals.approve),
2455
- attach: (input) => this._uploadGenerated(input, { meshId, chatId, runId, deviceId }),
2689
+ attach: (input) => this._uploadGenerated(input, {
2690
+ meshId,
2691
+ chatId,
2692
+ runId,
2693
+ deviceId,
2694
+ blobId: rounds.generated(input.bytes)
2695
+ }),
2456
2696
  fetchAttachment: (query) => this._fetchAttachment(engine, chatId, query),
2457
2697
  showImage: ({ attachmentId }) => this._showImage(engine, { chatId, runId, model, signal: controller.signal }, attachmentId),
2458
2698
  progress: (update) => {
@@ -2465,7 +2705,8 @@ export class QvacAssistant extends ReadyResource {
2465
2705
  signal: controller.signal
2466
2706
  });
2467
2707
  let harnessEvents = 0;
2468
- for await (const event of events) {
2708
+ for await (const raw of events) {
2709
+ const event = afterRounds(raw, roundBase);
2469
2710
  harnessEvents++;
2470
2711
  idle.touch();
2471
2712
  modelWatch.clear();
@@ -2630,7 +2871,10 @@ export class QvacAssistant extends ReadyResource {
2630
2871
  idle.stop();
2631
2872
  modelWatch.stop();
2632
2873
  ownershipWatch.stop();
2633
- await approvals.settle({ preservePending: isLifecycleTeardown(controller.signal.reason) });
2874
+ const handedOn = controller.signal.reason === OWNERSHIP_LOST;
2875
+ await approvals.settle({
2876
+ preservePending: handedOn || isLifecycleTeardown(controller.signal.reason)
2877
+ });
2634
2878
  settledWatch.stop();
2635
2879
  await releaseRunLog();
2636
2880
  if (titleOutcome && !diagramRepair) {
@@ -2870,29 +3114,35 @@ export class QvacAssistant extends ReadyResource {
2870
3114
  return null;
2871
3115
  }
2872
3116
  }
2873
- async _uploadGenerated({ fileName, mimeType, bytes, raw }, { meshId, chatId, runId, deviceId, sourceId }) {
3117
+ async _uploadGenerated({ fileName, mimeType, bytes, raw }, { meshId, chatId, runId, deviceId, sourceId, blobId }) {
2874
3118
  const duplex = this._engine.uploadAttachment();
2875
- duplex.write({
2876
- chatId,
2877
- runId,
2878
- originDeviceId: deviceId,
2879
- fileName,
2880
- mimeType,
2881
- raw,
2882
- meshId,
2883
- sourceId
2884
- });
2885
- const buf = b4a.toBuffer(bytes);
2886
- for (let i = 0; i < buf.length; i += UPLOAD_FRAME_BYTES) {
2887
- duplex.write({ data: buf.subarray(i, i + UPLOAD_FRAME_BYTES) });
2888
- }
2889
- duplex.end();
2890
- let attachment = null;
2891
- for await (const frame of duplex) {
2892
- if (frame.attachment) {
2893
- attachment = frame.attachment;
3119
+ const response = readUploadAttachment(duplex);
3120
+ void response.catch(() => duplex.destroy());
3121
+ try {
3122
+ await writeUploadFrame(duplex, {
3123
+ chatId,
3124
+ runId,
3125
+ originDeviceId: deviceId,
3126
+ fileName,
3127
+ mimeType,
3128
+ raw,
3129
+ meshId,
3130
+ sourceId,
3131
+ blobId,
3132
+ totalBytes: blobId ? bytes.byteLength : undefined
3133
+ });
3134
+ const buf = blobId ? b4a.alloc(0) : b4a.toBuffer(bytes);
3135
+ for (let i = 0; i < buf.length; i += UPLOAD_FRAME_BYTES) {
3136
+ await writeUploadFrame(duplex, { data: buf.subarray(i, i + UPLOAD_FRAME_BYTES) });
2894
3137
  }
3138
+ duplex.end();
3139
+ }
3140
+ catch (error) {
3141
+ duplex.destroy();
3142
+ await response;
3143
+ throw error;
2895
3144
  }
3145
+ const attachment = await response;
2896
3146
  if (!attachment) {
2897
3147
  throw new Error('attachment upload ended without a record');
2898
3148
  }
@@ -3037,6 +3287,9 @@ export class QvacAssistant extends ReadyResource {
3037
3287
  if (!probe) {
3038
3288
  return 'unsynced';
3039
3289
  }
3290
+ if (!probe.hasVideo && !probe.hasAudio) {
3291
+ throw new Error('no video or audio stream');
3292
+ }
3040
3293
  if (!probe.hasVideo) {
3041
3294
  return 'audio';
3042
3295
  }
@@ -3338,35 +3591,74 @@ export class QvacAssistant extends ReadyResource {
3338
3591
  });
3339
3592
  return file;
3340
3593
  }
3341
- async _hydratePdfPages(messages, { signal, runId, window, reading, textChars, wholeIds, selected }) {
3594
+ async _hydratePdfPages(messages, { signal, runId, window, reading, textChars, wholeIds, citedPages, readablePages, visualRequest, selected }) {
3342
3595
  const rendered = new Set();
3596
+ let shown = false;
3343
3597
  const notices = [];
3344
3598
  const turns = messages.filter((turn) => turn.pdfs?.length);
3345
3599
  if (!turns.length) {
3346
- return { rendered, notices };
3600
+ return { rendered, shown, notices };
3347
3601
  }
3348
3602
  const dir = this._imageCacheDir;
3349
3603
  await fs.promises.mkdir(dir, { recursive: true, mode: 0o700 });
3350
3604
  const pinned = this._runImageCacheNames.get(runId) ?? new Set();
3351
3605
  this._runImageCacheNames.set(runId, pinned);
3352
3606
  const latestUser = messages.findLast((turn) => turn.role === 'user');
3607
+ const refs = new Map(turns.flatMap((turn) => turn.pdfs ?? []).map((pdf) => [pdf.id, pdf]));
3608
+ const replayed = new Set();
3609
+ for (const turn of messages) {
3610
+ const shots = turn === latestUser ? undefined : turn.pdfShots;
3611
+ delete turn.pdfShots;
3612
+ if (shots) {
3613
+ const files = await this._replayPdfShots(shots, refs, { dir, pinned, signal });
3614
+ attachPages(turn, files);
3615
+ shown ||= files.length > 0;
3616
+ replayed.add(turn);
3617
+ }
3618
+ }
3353
3619
  const measured = await this._pdfReading(window, reading);
3354
3620
  for (const turn of turns) {
3355
3621
  const room = pdfImageRoom(measured, turn === latestUser ? textChars : 0);
3356
3622
  for (const pdf of turn.pdfs ?? []) {
3357
- const pages = await this._tryHydratePdf({ dir, pdf, pinned, room, signal, selected });
3358
- attachPages(turn, pages.names.map((name) => path.join(dir, name)));
3623
+ const pagesWithText = readablePages.get(pdf.id);
3624
+ const textFirst = !visualRequest && pagesWithText !== undefined;
3625
+ const cited = citedPages && citedPages.get(pdf.id) !== null
3626
+ ? (citedPages.get(pdf.id) ?? NO_CITED_PAGES)
3627
+ : undefined;
3628
+ if (replayed.has(turn) && !visualRequest && !cited?.size) {
3629
+ continue;
3630
+ }
3631
+ const pages = await this._tryHydratePdf({
3632
+ dir,
3633
+ pdf,
3634
+ pinned,
3635
+ room,
3636
+ signal,
3637
+ selected,
3638
+ cited,
3639
+ textFirst,
3640
+ readablePages: pagesWithText,
3641
+ visualRequest,
3642
+ reselect: citedPages?.has(pdf.id) ?? false,
3643
+ earlier: turn !== latestUser
3644
+ });
3645
+ if (replayed.has(turn) && !pages.forLatest) {
3646
+ continue;
3647
+ }
3648
+ const shownOn = pages.forLatest && latestUser ? latestUser : turn;
3649
+ attachPages(shownOn, pages.names.map((name) => path.join(dir, name)));
3650
+ shown ||= pages.names.length > 0;
3359
3651
  if (pages.names.length && turn === latestUser) {
3360
3652
  rendered.add(pdf.id);
3361
3653
  }
3362
3654
  const whole = turn === latestUser && wholeIds.has(pdf.id);
3363
3655
  for (const notice of pdfNotices(pdf.fileName, pages, whole)) {
3364
- notices.push({ turn, notice });
3656
+ notices.push({ turn: shownOn, notice });
3365
3657
  }
3366
3658
  }
3367
3659
  delete turn.pdfs;
3368
3660
  }
3369
- return { rendered, notices };
3661
+ return { rendered, shown, notices };
3370
3662
  }
3371
3663
  async _pdfReading({ ctxSize, modelName }, options) {
3372
3664
  const custom = await this._customModel(modelName);
@@ -3382,6 +3674,12 @@ export class QvacAssistant extends ReadyResource {
3382
3674
  }
3383
3675
  async _tryHydratePdf(opts) {
3384
3676
  try {
3677
+ if (opts.cited?.size === 0) {
3678
+ if (opts.signal.aborted) {
3679
+ throw new Error('hydration aborted');
3680
+ }
3681
+ return NO_PDF_PAGES;
3682
+ }
3385
3683
  return await this._hydratePdf(opts);
3386
3684
  }
3387
3685
  catch (err) {
@@ -3393,18 +3691,23 @@ export class QvacAssistant extends ReadyResource {
3393
3691
  }
3394
3692
  }
3395
3693
  async _hydratePdf(hydration) {
3396
- const { dir, pdf, pinned, room, signal, selected } = hydration;
3694
+ const { dir, pdf, pinned, room, signal, selected, cited, textFirst, readablePages, visualRequest, reselect, earlier = false } = hydration;
3397
3695
  const key = pdf.pages?.blobId ?? pdf.id;
3398
- const remember = (selection) => {
3399
- this._pdfSelections.set(key, selection);
3696
+ const remember = (selection, persist = true) => {
3697
+ if (persist) {
3698
+ this._pdfSelections.set(key, selection);
3699
+ }
3400
3700
  for (const { page, clip } of selection.shots) {
3401
3701
  this._mediaRefs.set(pdfShotCacheName(pdf, { page, clip }), { id: pdf.id, page, clip });
3402
3702
  }
3403
- if (!pdf.pages) {
3703
+ if (persist && !pdf.pages) {
3404
3704
  selected.push({ ref: pdf, selection });
3405
3705
  }
3406
3706
  };
3407
- const known = this._pdfSelections.get(key) ?? (await this._storedPdfSelection(pdf, signal));
3707
+ const stored = !earlier && (textFirst || reselect || cited !== undefined)
3708
+ ? null
3709
+ : (this._pdfSelections.get(key) ?? (await this._storedPdfSelection(pdf, signal)));
3710
+ const known = visualRequest && stored?.shots.length === 0 ? null : stored;
3408
3711
  if (known) {
3409
3712
  remember(known);
3410
3713
  const cached = await cachedPdfPages(known, { dir, pdf, pinned });
@@ -3412,6 +3715,9 @@ export class QvacAssistant extends ReadyResource {
3412
3715
  return cached;
3413
3716
  }
3414
3717
  }
3718
+ if (earlier && !known && !visualRequest && !cited?.size) {
3719
+ return NO_PDF_PAGES;
3720
+ }
3415
3721
  const bytes = await this._readBlob(pdf.url, signal);
3416
3722
  if (signal.aborted) {
3417
3723
  throw new Error('hydration aborted');
@@ -3421,10 +3727,49 @@ export class QvacAssistant extends ReadyResource {
3421
3727
  return NO_PDF_PAGES;
3422
3728
  }
3423
3729
  try {
3424
- const selection = known ?? (await selectPdfPages(doc, room));
3730
+ const selection = known ?? (await selectPdfPages(doc, room, { cited, textFirst, readablePages }));
3425
3731
  if (!known) {
3426
- remember(selection);
3732
+ remember(selection, !textFirst && !earlier);
3733
+ }
3734
+ const pages = await this._renderPdfShots(doc, selection, hydration);
3735
+ return known || !earlier ? pages : { ...pages, forLatest: true };
3736
+ }
3737
+ finally {
3738
+ doc.close();
3739
+ }
3740
+ }
3741
+ async _replayPdfShots(shots, refs, { dir, pinned, signal }) {
3742
+ const files = [];
3743
+ for (const id of new Set(shots.map((shot) => shot.id))) {
3744
+ const pdf = refs.get(id);
3745
+ if (!pdf) {
3746
+ continue;
3427
3747
  }
3748
+ const selection = {
3749
+ pageCount: 0,
3750
+ shots: shots
3751
+ .filter((shot) => shot.id === id)
3752
+ .map(({ page, clip }) => ({ page, clip, label: '' }))
3753
+ };
3754
+ for (const { page, clip } of selection.shots) {
3755
+ this._mediaRefs.set(pdfShotCacheName(pdf, { page, clip }), { id, page, clip });
3756
+ }
3757
+ const pages = (await cachedPdfPages(selection, { dir, pdf, pinned })) ??
3758
+ (await this._renderStoredShots(selection, { dir, pdf, pinned, signal }));
3759
+ files.push(...pages.names.map((name) => path.join(dir, name)));
3760
+ }
3761
+ return files;
3762
+ }
3763
+ async _renderStoredShots(selection, hydration) {
3764
+ const bytes = await this._readBlob(hydration.pdf.url, hydration.signal);
3765
+ if (hydration.signal.aborted) {
3766
+ throw new Error('hydration aborted');
3767
+ }
3768
+ const doc = bytes ? await this._pdfRasterizer.open(bytes) : null;
3769
+ if (!doc) {
3770
+ return NO_PDF_PAGES;
3771
+ }
3772
+ try {
3428
3773
  return await this._renderPdfShots(doc, selection, hydration);
3429
3774
  }
3430
3775
  finally {
@@ -3815,7 +4160,7 @@ export class QvacAssistant extends ReadyResource {
3815
4160
  }
3816
4161
  }
3817
4162
  const title = generated ?? deriveChatTitle(prompt);
3818
- if (!title) {
4163
+ if (!title || !isDefaultChatTitle((await engine.getChat({ chatId }))?.title)) {
3819
4164
  return null;
3820
4165
  }
3821
4166
  await engine.renameChat({ chatId, config: { title } });
@@ -3880,7 +4225,7 @@ export class QvacAssistant extends ReadyResource {
3880
4225
  }
3881
4226
  return text;
3882
4227
  }
3883
- _createApprove(engine, { chatId, runId, deviceId, agentId, hasRun }) {
4228
+ _createApprove(engine, { chatId, runId, deviceId, agentId, hasRun, repeats }) {
3884
4229
  const waiters = new Map();
3885
4230
  let latestChunks = [];
3886
4231
  let watcher = null;
@@ -3969,13 +4314,16 @@ export class QvacAssistant extends ReadyResource {
3969
4314
  this._log?.warn('approvals', 'rule lookup failed, asking instead', { runId, err });
3970
4315
  return { rules: [], ourMemberId: undefined };
3971
4316
  });
3972
- const rule = matchApprovalRule(rules ?? [], {
3973
- toolName: call.name,
3974
- resource: call.resource,
3975
- agentId,
3976
- chatId,
3977
- memberId: ourMemberId
3978
- });
4317
+ const repeated = repeats(call);
4318
+ const rule = repeated
4319
+ ? null
4320
+ : matchApprovalRule(rules ?? [], {
4321
+ toolName: call.name,
4322
+ resource: call.resource,
4323
+ agentId,
4324
+ chatId,
4325
+ memberId: ourMemberId
4326
+ });
3979
4327
  if (rule) {
3980
4328
  this._log?.info('approvals', `rule ${rule.decision}`, {
3981
4329
  runId,
@@ -3998,7 +4346,7 @@ export class QvacAssistant extends ReadyResource {
3998
4346
  });
3999
4347
  return park(prior.id, call.name);
4000
4348
  }
4001
- if (!hasRun()) {
4349
+ if (!hasRun() && !repeated) {
4002
4350
  this._log?.info('approvals', `answer ${status} (from before the app closed)`, {
4003
4351
  runId,
4004
4352
  chatId,
@@ -4289,6 +4637,10 @@ export class QvacAssistant extends ReadyResource {
4289
4637
  };
4290
4638
  }
4291
4639
  }
4640
+ function previousQuestion(history) {
4641
+ const asked = history.flatMap((chunk) => chunk.userMessage?.text?.trim() || []);
4642
+ return asked.at(-2);
4643
+ }
4292
4644
  function notReady(search) {
4293
4645
  return {
4294
4646
  chatFilesNotReady: search.chatFilesNotReady ?? 0,
@@ -4299,19 +4651,28 @@ function notReadyCount(search) {
4299
4651
  const { chatFilesNotReady, projectFilesNotReady } = notReady(search);
4300
4652
  return chatFilesNotReady + projectFilesNotReady;
4301
4653
  }
4302
- function notReadyLine(search) {
4654
+ export function notReadyLine(search) {
4303
4655
  const { projectFilesNotReady } = notReady(search);
4304
- const chat = search.pendingFiles ?? [];
4305
- const parts = [];
4656
+ const gaps = search.uncoveredItems ?? [];
4657
+ const [remote, chat] = partition(search.pendingFiles ?? [], (name) => gaps.includes(`${name}: ${UNREAD.remote}`));
4658
+ const lines = [];
4659
+ const indexing = [];
4306
4660
  if (chat.length) {
4307
- parts.push(`${filesCount(chat.length, 'chat')} (${chat.join(', ')})`);
4661
+ indexing.push(`${filesCount(chat.length, 'chat')} (${chat.join(', ')})`);
4308
4662
  }
4309
4663
  if (projectFilesNotReady) {
4310
- parts.push(filesCount(projectFilesNotReady, 'project'));
4664
+ indexing.push(filesCount(projectFilesNotReady, 'project'));
4311
4665
  }
4312
- return parts.length
4313
- ? `[Still indexing and not used for this reply: ${parts.join(' and ')}.]`
4314
- : null;
4666
+ if (indexing.length) {
4667
+ lines.push(`[Still indexing and not used for this reply: ${indexing.join(' and ')}.]`);
4668
+ }
4669
+ if (remote.length) {
4670
+ lines.push(`[Not available on this device and not used for this reply: ${filesCount(remote.length, 'chat')} (${remote.join(', ')}).]`);
4671
+ }
4672
+ return lines.length ? lines.join('\n') : null;
4673
+ }
4674
+ function partition(items, test) {
4675
+ return [items.filter(test), items.filter((item) => !test(item))];
4315
4676
  }
4316
4677
  function filesCount(count, source) {
4317
4678
  return `${count} ${source} file${count === 1 ? '' : 's'}`;
@@ -4322,7 +4683,7 @@ function chunkSources(records) {
4322
4683
  function downloadingProgress(model, percentage) {
4323
4684
  return { phase: 'downloading', model, percentage: Math.floor(percentage / 10) * 10 };
4324
4685
  }
4325
- async function injectRetrievedKnowledge(knowledge, { transcript, sourceTagFilter, retrievedRecords, pictured, injected }) {
4686
+ async function injectRetrievedKnowledge(knowledge, { transcript, sourceTagFilter, retrievedRecords, injected }) {
4326
4687
  const records = sourcesFromHits(knowledge.hits, RAG_SOURCE_BOUNDS);
4327
4688
  const notice = chooseRagNotice({
4328
4689
  errorMessage: knowledge.search.errorMessage,
@@ -4349,16 +4710,35 @@ async function injectRetrievedKnowledge(knowledge, { transcript, sourceTagFilter
4349
4710
  });
4350
4711
  injected.sourcesWritten = true;
4351
4712
  if (records.length > 0) {
4352
- const framing = formatRagContext(knowledge.hits, knowledge.search.availableFiles ?? []);
4353
- const pages = pictured ? `\n${PDF_PAGES_SOURCE_LINE}` : '';
4354
- knowledge.lastUser.content = `${framing}${pages}\n\nThe user's message:\n${knowledge.lastUser.content}\n\n${RAG_CITATION_LINE}`;
4713
+ const framing = formatRagContext(shortLabels(knowledge.hits), knowledge.search.availableFiles ?? []);
4714
+ const outline = outlineLines(knowledge.search.outline ?? []);
4715
+ const gaps = uncoveredLine(knowledge.search.uncoveredItems ?? []);
4716
+ const scope = 'These are selected excerpts, not the complete document. Missing from the excerpts does not mean absent from the document. ' +
4717
+ 'Preserve the scope and conditions of each rule: when a list is only partly quoted, limit your claim to the quoted cases. ' +
4718
+ 'Do not present an amount for particular cases as a universal cap, or extend a condition from one step to other steps. ' +
4719
+ 'Repeat numeric conditions faithfully, including "over", "at least", and whether days are total or additional.';
4720
+ knowledge.lastUser.content = `${outline}${framing}${gaps}\n\n${scope}\n\nThe user's message:\n${knowledge.lastUser.content}\n\n${RAG_CITATION_LINE}`;
4355
4721
  injected.ragInjected = true;
4356
4722
  injected.documentsInjected = true;
4357
4723
  }
4358
- else if (knowledge.search.availableFiles?.length) {
4359
- 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.]`;
4724
+ else {
4725
+ const outline = outlineLines(knowledge.search.outline ?? []);
4726
+ if (outline) {
4727
+ knowledge.lastUser.content = `${outline}The user's message:\n${knowledge.lastUser.content}`;
4728
+ }
4729
+ if (knowledge.search.availableFiles?.length) {
4730
+ 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.]`;
4731
+ }
4360
4732
  }
4361
4733
  }
4734
+ function outlineLines(lines) {
4735
+ return lines.length === 0 ? '' : `${lines.join('\n')}\n\n`;
4736
+ }
4737
+ function uncoveredLine(items) {
4738
+ return items.length === 0
4739
+ ? ''
4740
+ : `\n\nThe selected excerpts may not cover: ${items.join(', ')}. Check the supplied passages, including equivalent wording. Answer only where they provide evidence; otherwise say the retrieved excerpts do not establish the answer. Do not infer that the full document omits it.`;
4741
+ }
4362
4742
  function noop() { }
4363
4743
  function toolCallChunk(event) {
4364
4744
  return {
@@ -4377,6 +4757,15 @@ function toolResultChunk(event) {
4377
4757
  ...callOf(event)
4378
4758
  };
4379
4759
  }
4760
+ const ROUND_WRITES = new Set(['content', 'thinking', 'round-reset', 'tool-call', 'tool-result']);
4761
+ function afterRounds(event, base) {
4762
+ const shifts = base > 0 && ROUND_WRITES.has(event.type) && typeof event.round === 'number';
4763
+ return shifts ? { ...event, round: Number(event.round) + base } : event;
4764
+ }
4765
+ function sameToolCall(chunk, call) {
4766
+ return (chunk.toolCall?.name === call.name &&
4767
+ JSON.stringify(chunk.toolCall.arguments ?? null) === JSON.stringify(call.arguments ?? null));
4768
+ }
4380
4769
  function roundOf(event) {
4381
4770
  return typeof event.round === 'number' ? event.round : undefined;
4382
4771
  }