@qvac/core 1.0.0-rc.8 → 1.0.1

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 (226) hide show
  1. package/CHANGELOG.md +243 -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/devices.js +6 -18
  8. package/dist-lib/lib/api/execution.d.ts +6 -2
  9. package/dist-lib/lib/api/execution.js +29 -3
  10. package/dist-lib/lib/api/host-run-log.d.ts +7 -1
  11. package/dist-lib/lib/api/host-run-log.js +3 -1
  12. package/dist-lib/lib/api/index.d.ts +3 -1
  13. package/dist-lib/lib/api/index.js +17 -6
  14. package/dist-lib/lib/api/members.js +13 -29
  15. package/dist-lib/lib/api/mesh.d.ts +2 -1
  16. package/dist-lib/lib/api/projects.d.ts +1 -1
  17. package/dist-lib/lib/api/projects.js +9 -5
  18. package/dist-lib/lib/api/rag-label.d.ts +4 -0
  19. package/dist-lib/lib/api/rag-label.js +21 -0
  20. package/dist-lib/lib/api/rag-lexical.d.ts +22 -0
  21. package/dist-lib/lib/api/rag-lexical.js +1058 -0
  22. package/dist-lib/lib/api/rag-manifest.d.ts +13 -0
  23. package/dist-lib/lib/api/rag-manifest.js +46 -0
  24. package/dist-lib/lib/api/rag-outline.d.ts +36 -0
  25. package/dist-lib/lib/api/rag-outline.js +426 -0
  26. package/dist-lib/lib/api/rag-plugin.d.ts +20 -0
  27. package/dist-lib/lib/api/rag-plugin.js +70 -0
  28. package/dist-lib/lib/api/rag-synthesis.d.ts +7 -0
  29. package/dist-lib/lib/api/rag-synthesis.js +102 -0
  30. package/dist-lib/lib/api/rag-targets.d.ts +23 -0
  31. package/dist-lib/lib/api/rag-targets.js +215 -0
  32. package/dist-lib/lib/api/rag-window.d.ts +5 -0
  33. package/dist-lib/lib/api/rag-window.js +46 -0
  34. package/dist-lib/lib/api/rag.d.ts +3 -2
  35. package/dist-lib/lib/api/rag.js +421 -181
  36. package/dist-lib/lib/api/served.js +8 -14
  37. package/dist-lib/lib/api/workspace.d.ts +8 -1
  38. package/dist-lib/lib/api/workspace.js +27 -13
  39. package/dist-lib/lib/assistant/harness.d.ts +9 -1
  40. package/dist-lib/lib/assistant/history.d.ts +11 -2
  41. package/dist-lib/lib/assistant/history.js +74 -4
  42. package/dist-lib/lib/assistant/media-round.d.ts +61 -0
  43. package/dist-lib/lib/assistant/media-round.js +111 -0
  44. package/dist-lib/lib/assistant/message.d.ts +7 -0
  45. package/dist-lib/lib/assistant/model-round.d.ts +12 -0
  46. package/dist-lib/lib/assistant/model-round.js +130 -17
  47. package/dist-lib/lib/assistant/pdf-pages.d.ts +16 -1
  48. package/dist-lib/lib/assistant/pdf-pages.js +67 -3
  49. package/dist-lib/lib/assistant/round-host.d.ts +22 -0
  50. package/dist-lib/lib/assistant/round-host.js +142 -8
  51. package/dist-lib/lib/assistant/upload.d.ts +12 -0
  52. package/dist-lib/lib/assistant/upload.js +36 -0
  53. package/dist-lib/lib/assistant-client.d.ts +1 -0
  54. package/dist-lib/lib/assistant-client.js +40 -19
  55. package/dist-lib/lib/assistant-tree.d.ts +2 -0
  56. package/dist-lib/lib/assistant-tree.js +41 -1
  57. package/dist-lib/lib/assistant.d.ts +15 -0
  58. package/dist-lib/lib/assistant.js +474 -89
  59. package/dist-lib/lib/client.d.ts +1 -0
  60. package/dist-lib/lib/client.js +4 -1
  61. package/dist-lib/lib/compaction.js +2 -2
  62. package/dist-lib/lib/core/agent.js +12 -45
  63. package/dist-lib/lib/core/approval-rules.js +1 -8
  64. package/dist-lib/lib/core/archived-mesh.d.ts +2 -1
  65. package/dist-lib/lib/core/archived-mesh.js +12 -8
  66. package/dist-lib/lib/core/chat-copy.d.ts +18 -3
  67. package/dist-lib/lib/core/chat-copy.js +74 -25
  68. package/dist-lib/lib/core/chat-fork.js +15 -6
  69. package/dist-lib/lib/core/chat-group.js +16 -24
  70. package/dist-lib/lib/core/chat-move.d.ts +9 -0
  71. package/dist-lib/lib/core/chat-move.js +94 -3
  72. package/dist-lib/lib/core/chat-order.js +12 -18
  73. package/dist-lib/lib/core/chat.js +38 -31
  74. package/dist-lib/lib/core/chunk.d.ts +2 -2
  75. package/dist-lib/lib/core/chunk.js +18 -25
  76. package/dist-lib/lib/core/collections.d.ts +2 -0
  77. package/dist-lib/lib/core/collections.js +2 -0
  78. package/dist-lib/lib/core/contract/activity.d.ts +4 -0
  79. package/dist-lib/lib/core/contract/activity.js +34 -4
  80. package/dist-lib/lib/core/contract/chat-group.d.ts +1 -0
  81. package/dist-lib/lib/core/contract/chat-group.js +21 -1
  82. package/dist-lib/lib/core/contract/chat.d.ts +9 -0
  83. package/dist-lib/lib/core/contract/chat.js +127 -5
  84. package/dist-lib/lib/core/contract/chunks.d.ts +1 -0
  85. package/dist-lib/lib/core/contract/chunks.js +37 -6
  86. package/dist-lib/lib/core/contract/features.d.ts +21 -0
  87. package/dist-lib/lib/core/contract/features.js +22 -0
  88. package/dist-lib/lib/core/contract/index.js +2 -0
  89. package/dist-lib/lib/core/contract/indexing.js +4 -2
  90. package/dist-lib/lib/core/contract/knowledge.js +5 -4
  91. package/dist-lib/lib/core/contract/members.js +16 -10
  92. package/dist-lib/lib/core/contract/mesh-kind.d.ts +1 -0
  93. package/dist-lib/lib/core/contract/mesh-kind.js +3 -1
  94. package/dist-lib/lib/core/contract/projects.d.ts +5 -0
  95. package/dist-lib/lib/core/contract/projects.js +13 -5
  96. package/dist-lib/lib/core/contract/roles.js +32 -4
  97. package/dist-lib/lib/core/contract/run-requests.d.ts +1 -0
  98. package/dist-lib/lib/core/contract/run-requests.js +35 -2
  99. package/dist-lib/lib/core/contract/seed-defaults.js +2 -2
  100. package/dist-lib/lib/core/contract/tool-configs.js +3 -2
  101. package/dist-lib/lib/core/contract/workspace.d.ts +2 -0
  102. package/dist-lib/lib/core/contract/workspace.js +40 -7
  103. package/dist-lib/lib/core/contract/writers.js +2 -2
  104. package/dist-lib/lib/core/departures.d.ts +3 -1
  105. package/dist-lib/lib/core/departures.js +3 -0
  106. package/dist-lib/lib/core/devices.js +1 -8
  107. package/dist-lib/lib/core/files/attachments.d.ts +2 -0
  108. package/dist-lib/lib/core/files/attachments.js +19 -1
  109. package/dist-lib/lib/core/files/index.d.ts +20 -4
  110. package/dist-lib/lib/core/files/index.js +132 -21
  111. package/dist-lib/lib/core/files/processing/document.d.ts +4 -0
  112. package/dist-lib/lib/core/files/processing/document.js +7 -0
  113. package/dist-lib/lib/core/files/processing/index.d.ts +13 -0
  114. package/dist-lib/lib/core/files/processing/index.js +144 -12
  115. package/dist-lib/lib/core/files/processing/protocol.js +13 -34
  116. package/dist-lib/lib/core/files/processing/state.d.ts +2 -0
  117. package/dist-lib/lib/core/files/rag/build.d.ts +19 -2
  118. package/dist-lib/lib/core/files/rag/build.js +123 -19
  119. package/dist-lib/lib/core/files/rag/failure.js +2 -1
  120. package/dist-lib/lib/core/files/rag/index.d.ts +11 -3
  121. package/dist-lib/lib/core/files/rag/index.js +13 -1
  122. package/dist-lib/lib/core/files/rag/load.d.ts +6 -3
  123. package/dist-lib/lib/core/files/rag/load.js +27 -6
  124. package/dist-lib/lib/core/files/rag/plugin-index.d.ts +32 -0
  125. package/dist-lib/lib/core/files/rag/plugin-index.js +127 -0
  126. package/dist-lib/lib/core/files/rag/produce.d.ts +5 -3
  127. package/dist-lib/lib/core/files/rag/produce.js +104 -16
  128. package/dist-lib/lib/core/files/rag/retrieval/full-text.d.ts +3 -1
  129. package/dist-lib/lib/core/files/rag/retrieval/full-text.js +62 -13
  130. package/dist-lib/lib/core/files/rag/text-cache.d.ts +28 -0
  131. package/dist-lib/lib/core/files/rag/text-cache.js +141 -0
  132. package/dist-lib/lib/core/files/records/knowledge.d.ts +1 -3
  133. package/dist-lib/lib/core/files/records/knowledge.js +7 -40
  134. package/dist-lib/lib/core/files/records/reclaim.js +5 -10
  135. package/dist-lib/lib/core/files/upload.js +68 -41
  136. package/dist-lib/lib/core/fs-drives.js +14 -26
  137. package/dist-lib/lib/core/invite.d.ts +1 -0
  138. package/dist-lib/lib/core/invite.js +4 -2
  139. package/dist-lib/lib/core/local/files.d.ts +1 -1
  140. package/dist-lib/lib/core/local/files.js +5 -5
  141. package/dist-lib/lib/core/local/index.d.ts +13 -6
  142. package/dist-lib/lib/core/local/index.js +103 -69
  143. package/dist-lib/lib/core/local/run-logs.d.ts +1 -1
  144. package/dist-lib/lib/core/local/run-logs.js +5 -5
  145. package/dist-lib/lib/core/mesh-processor.d.ts +19 -0
  146. package/dist-lib/lib/core/mesh-processor.js +87 -0
  147. package/dist-lib/lib/core/mesh.d.ts +48 -5
  148. package/dist-lib/lib/core/mesh.js +318 -129
  149. package/dist-lib/lib/core/migrations/agent-catalog.js +2 -8
  150. package/dist-lib/lib/core/migrations/agent-tools-scope.js +5 -11
  151. package/dist-lib/lib/core/migrations/workspace-from-0-21/index.d.ts +88 -0
  152. package/dist-lib/lib/core/migrations/workspace-from-0-21/index.js +731 -0
  153. package/dist-lib/lib/core/migrations/writer-links.d.ts +2 -0
  154. package/dist-lib/lib/core/migrations/writer-links.js +32 -0
  155. package/dist-lib/lib/core/project-keys.d.ts +6 -0
  156. package/dist-lib/lib/core/project-keys.js +11 -0
  157. package/dist-lib/lib/core/public-mesh.d.ts +3 -0
  158. package/dist-lib/lib/core/public-mesh.js +10 -8
  159. package/dist-lib/lib/core/run-requests.d.ts +3 -0
  160. package/dist-lib/lib/core/run-requests.js +21 -1
  161. package/dist-lib/lib/core/skill-availability.d.ts +33 -0
  162. package/dist-lib/lib/core/skill-availability.js +67 -0
  163. package/dist-lib/lib/core/sync/background.d.ts +0 -1
  164. package/dist-lib/lib/core/sync/background.js +2 -1
  165. package/dist-lib/lib/core/sync/changed.d.ts +4 -2
  166. package/dist-lib/lib/core/sync/changed.js +1 -1
  167. package/dist-lib/lib/core/sync/index.js +52 -10
  168. package/dist-lib/lib/core/sync/view-updates.d.ts +18 -12
  169. package/dist-lib/lib/core/sync/view-updates.js +82 -91
  170. package/dist-lib/lib/core/sync-status.js +97 -41
  171. package/dist-lib/lib/core/tool-activity.js +6 -12
  172. package/dist-lib/lib/core/tool-configs.js +17 -29
  173. package/dist-lib/lib/core/upgrade-summary.d.ts +19 -0
  174. package/dist-lib/lib/core/upgrade-summary.js +67 -0
  175. package/dist-lib/lib/core/view-read.d.ts +4 -0
  176. package/dist-lib/lib/core/view-read.js +23 -0
  177. package/dist-lib/lib/core/workspace-upgrade.d.ts +7 -0
  178. package/dist-lib/lib/core/workspace-upgrade.js +27 -16
  179. package/dist-lib/lib/core.d.ts +39 -42
  180. package/dist-lib/lib/core.js +661 -639
  181. package/dist-lib/lib/errors.d.ts +20 -0
  182. package/dist-lib/lib/errors.js +20 -0
  183. package/dist-lib/lib/llm-model-names.d.ts +1 -1
  184. package/dist-lib/lib/llm-model-names.js +1 -1
  185. package/dist-lib/lib/mesh-scoped-engine.js +4 -0
  186. package/dist-lib/lib/model-context.js +1 -2
  187. package/dist-lib/lib/probe-rounds.d.ts +7 -2
  188. package/dist-lib/lib/probe-rounds.js +6 -1
  189. package/dist-lib/lib/run-transcript.d.ts +4 -1
  190. package/dist-lib/lib/run-transcript.js +15 -6
  191. package/dist-lib/schema/engine.js +83 -6
  192. package/dist-lib/schema/indexes.d.ts +3 -0
  193. package/dist-lib/schema/indexes.js +5 -0
  194. package/dist-lib/schema/local.js +11 -2
  195. package/dist-lib/schema/qvac.js +49 -5
  196. package/dist-lib/spec/bind.js +34 -0
  197. package/dist-lib/spec/calls.js +5 -0
  198. package/dist-lib/spec/capabilities.d.ts +17 -0
  199. package/dist-lib/spec/hrpc/hrpc.json +65 -0
  200. package/dist-lib/spec/hrpc/index.js +68 -5
  201. package/dist-lib/spec/hrpc/messages.js +260 -23
  202. package/dist-lib/spec/hyperschema/index.js +260 -23
  203. package/dist-lib/spec/hyperschema/schema.json +181 -0
  204. package/dist-lib/spec/hyperschema/types.d.ts +48 -0
  205. package/dist-lib/spec/local/hyperdb/messages.js +270 -224
  206. package/dist-lib/spec/local/hyperschema/index.js +73 -38
  207. package/dist-lib/spec/local/hyperschema/schema.json +31 -0
  208. package/dist-lib/spec/local/hyperschema/types.d.ts +8 -0
  209. package/dist-lib/spec/mesh/hyperdb/db.json +39 -2
  210. package/dist-lib/spec/mesh/hyperdb/index.js +120 -3
  211. package/dist-lib/spec/mesh/hyperdb/messages.js +1328 -838
  212. package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +30 -0
  213. package/dist-lib/spec/mesh/hyperdispatch/index.js +86 -1
  214. package/dist-lib/spec/mesh/hyperdispatch/messages.js +682 -287
  215. package/dist-lib/spec/mesh/hyperschema/index.js +682 -287
  216. package/dist-lib/spec/mesh/hyperschema/schema.json +201 -0
  217. package/dist-lib/spec/mesh/hyperschema/types.d.ts +52 -0
  218. package/package.json +75 -70
  219. package/tui/README.md +2 -1
  220. package/tui/app.mjs +28 -7
  221. package/tui/boot.mjs +5 -1
  222. package/tui/lib/attachment-files.mjs +82 -0
  223. package/tui/lib/reopen-meshes.mjs +15 -0
  224. package/tui/main.mjs +4 -43
  225. package/worker/assistant-sidecar.mjs +10 -3
  226. package/worker/stow-entry.mjs +3 -52
@@ -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;
@@ -1158,6 +1240,36 @@ export class QvacAssistant extends ReadyResource {
1158
1240
  this._recordSkillReadiness().catch(noop);
1159
1241
  return resolved;
1160
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
+ }
1161
1273
  async _recordSkillReadiness() {
1162
1274
  this._readinessDirty = true;
1163
1275
  if (this._recordingReadiness) {
@@ -1181,6 +1293,7 @@ export class QvacAssistant extends ReadyResource {
1181
1293
  }
1182
1294
  async _readySkillNames() {
1183
1295
  const unscoped = (await this._harness.skills?.(null, null, { skipCredentialProbe: true })) ?? [];
1296
+ this._skillsHere = loadableSkills(unscoped);
1184
1297
  const ready = unscoped.filter((skill) => isSetUpHere(skill.status)).map((skill) => skill.name);
1185
1298
  const deviceId = this._deviceId;
1186
1299
  const meshId = this._accountMeshId;
@@ -1333,11 +1446,19 @@ export class QvacAssistant extends ReadyResource {
1333
1446
  }
1334
1447
  async _reconcileCompaction(engine, chatId) {
1335
1448
  const deviceId = this._requireDeviceId();
1336
- const { chunks } = await engine
1337
- .listChunks({ chatId, type: 'compaction' })
1338
- .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
+ }
1339
1460
  let newest = null;
1340
- for (const chunk of chunks ?? []) {
1461
+ for (const chunk of chunks) {
1341
1462
  if (!newest || chunk.seq > newest.seq) {
1342
1463
  newest = chunk;
1343
1464
  }
@@ -1471,6 +1592,7 @@ export class QvacAssistant extends ReadyResource {
1471
1592
  sentRuns,
1472
1593
  askedRuns,
1473
1594
  readRuns: served.readRuns,
1595
+ shownRuns: served.shownRuns,
1474
1596
  rebuild: await this._runReplay.rebuild({ holder: LOCAL_HOLDER, chatId })
1475
1597
  };
1476
1598
  }
@@ -1481,6 +1603,7 @@ export class QvacAssistant extends ReadyResource {
1481
1603
  sentRuns: new Map(),
1482
1604
  askedRuns: new Map(),
1483
1605
  readRuns: new Map(),
1606
+ shownRuns: new Map(),
1484
1607
  rebuild: await this._runReplay.rebuild({ holder: LOCAL_HOLDER, chatId })
1485
1608
  };
1486
1609
  }
@@ -1559,6 +1682,7 @@ export class QvacAssistant extends ReadyResource {
1559
1682
  if (consumed.has(meshId))
1560
1683
  continue;
1561
1684
  consumer.stop();
1685
+ this._roundHost?.drop(meshId);
1562
1686
  this._meshConsumers.delete(meshId);
1563
1687
  this._pruneSeen(meshId, new Set());
1564
1688
  for (const chatId of this._servedChats.get(meshId) ?? []) {
@@ -1663,6 +1787,9 @@ export class QvacAssistant extends ReadyResource {
1663
1787
  for (const consumer of this._meshConsumers.values()) {
1664
1788
  consumer.stop();
1665
1789
  }
1790
+ for (const edge of this._retakes.values()) {
1791
+ edge.stop();
1792
+ }
1666
1793
  this._meshConsumers.clear();
1667
1794
  this._advertisedOn = null;
1668
1795
  }
@@ -1705,7 +1832,11 @@ export class QvacAssistant extends ReadyResource {
1705
1832
  this._chats.delete(chat);
1706
1833
  }
1707
1834
  void this._reportCapabilities();
1708
- 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
+ });
1709
1840
  }
1710
1841
  })();
1711
1842
  settle.catch((err) => this._log?.error('assistant', `run ${runId} failed`, err));
@@ -1734,7 +1865,17 @@ export class QvacAssistant extends ReadyResource {
1734
1865
  }
1735
1866
  async _claim(engine, request, { runId, meshId }) {
1736
1867
  const agentId = request.runRequest?.agentId;
1737
- 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
+ }
1738
1879
  if (!agent) {
1739
1880
  this._seen.delete(runId);
1740
1881
  return undefined;
@@ -1780,7 +1921,7 @@ export class QvacAssistant extends ReadyResource {
1780
1921
  running.steering.push({ mode: 'steer', text: turn.content, id: request.runId });
1781
1922
  }
1782
1923
  }
1783
- _requeue(runId, reason, replay) {
1924
+ _requeue(runId, { meshId, reason, replay }) {
1784
1925
  const background = this._backgroundReplays.delete(runId);
1785
1926
  if (this._seen.has(runId) || this.closing) {
1786
1927
  return;
@@ -1791,6 +1932,31 @@ export class QvacAssistant extends ReadyResource {
1791
1932
  else if ((background && !this._backgrounded) || reason === MODEL_UNLOADED) {
1792
1933
  replay();
1793
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
+ })();
1794
1960
  }
1795
1961
  async _citeWebSources(transcript, runId, result) {
1796
1962
  const records = webSourcesFromToolResult(result, WEB_RAG_SOURCE_BOUNDS);
@@ -1804,14 +1970,9 @@ export class QvacAssistant extends ReadyResource {
1804
1970
  this._log?.warn('assistant', 'web sources chunk skipped', { runId, err });
1805
1971
  }
1806
1972
  }
1807
- async _searchKnowledge(engine, messages, { chatId, attachmentIds, signal, preparing }) {
1808
- let lastUser;
1809
- for (let index = messages.length - 1; index >= 0; index--) {
1810
- if (messages[index].role === 'user') {
1811
- lastUser = messages[index];
1812
- break;
1813
- }
1814
- }
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);
1815
1976
  const query = lastUser?.content.trim();
1816
1977
  if (!lastUser || !query) {
1817
1978
  return null;
@@ -1820,6 +1981,8 @@ export class QvacAssistant extends ReadyResource {
1820
1981
  const search = await searchKnowledge(engine, {
1821
1982
  chatId,
1822
1983
  query,
1984
+ previousQuery: attachmentIds.length === 0 ? previousQuery : undefined,
1985
+ excerptsOnly,
1823
1986
  attachmentIds: attachmentIds.length > 0 ? [...attachmentIds] : undefined
1824
1987
  }, { signal, preparing });
1825
1988
  if (!search.enabled) {
@@ -1899,6 +2062,10 @@ export class QvacAssistant extends ReadyResource {
1899
2062
  return null;
1900
2063
  }
1901
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
+ }
1902
2069
  async _isHandled(engine, request) {
1903
2070
  const { chunks } = await engine.listChunks({
1904
2071
  chatId: request.chatId,
@@ -1919,8 +2086,29 @@ export class QvacAssistant extends ReadyResource {
1919
2086
  if (await this._isHandled(engine, request)) {
1920
2087
  return;
1921
2088
  }
1922
- const fresh = await engine.getAgent({ agentId: agent.id }).catch(noop);
1923
- 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)) {
1924
2112
  return;
1925
2113
  }
1926
2114
  agent = fresh;
@@ -1952,7 +2140,11 @@ export class QvacAssistant extends ReadyResource {
1952
2140
  }
1953
2141
  const hold = await this._openRunLog(engine, runId);
1954
2142
  const runLog = hold?.runLog;
1955
- 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;
1956
2148
  const releaseRunLog = async () => {
1957
2149
  await hold?.runLog
1958
2150
  .release(runId, hold.lease.generation)
@@ -1991,6 +2183,7 @@ export class QvacAssistant extends ReadyResource {
1991
2183
  runId,
1992
2184
  deviceId,
1993
2185
  model,
2186
+ unavailableSkills: await this._skillsNotHere(agent),
1994
2187
  log: this._log
1995
2188
  });
1996
2189
  const rounds = new OwnerRounds({
@@ -2001,7 +2194,10 @@ export class QvacAssistant extends ReadyResource {
2001
2194
  deviceId,
2002
2195
  from: request.seq,
2003
2196
  media: (file) => this._mediaRefs.get(path.basename(file)),
2004
- 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
2005
2201
  });
2006
2202
  await rounds.retire();
2007
2203
  const folded = !resumed &&
@@ -2038,6 +2234,7 @@ export class QvacAssistant extends ReadyResource {
2038
2234
  sentRuns: new Map(),
2039
2235
  askedRuns: new Map(),
2040
2236
  readRuns: new Map(),
2237
+ shownRuns: new Map(),
2041
2238
  rebuild: await this._runReplay.rebuild({ holder: LOCAL_HOLDER, chatId })
2042
2239
  };
2043
2240
  }
@@ -2068,7 +2265,7 @@ export class QvacAssistant extends ReadyResource {
2068
2265
  await this._storeVideoDigests(sampled.splice(0), { meshId, chatId, runId, deviceId });
2069
2266
  }
2070
2267
  };
2071
- let pdfPages = { rendered: new Set(), notices: [] };
2268
+ let pdfPages = { rendered: new Set(), shown: false, notices: [] };
2072
2269
  const selectedPdfs = [];
2073
2270
  if (!seesImages) {
2074
2271
  const dropped = messages.reduce((count, turn) => count + (turn.images?.length ?? 0), 0);
@@ -2082,6 +2279,7 @@ export class QvacAssistant extends ReadyResource {
2082
2279
  }
2083
2280
  delete turn.images;
2084
2281
  delete turn.pdfs;
2282
+ delete turn.pdfShots;
2085
2283
  }
2086
2284
  }
2087
2285
  else {
@@ -2162,12 +2360,15 @@ export class QvacAssistant extends ReadyResource {
2162
2360
  if (held.status) {
2163
2361
  await transcript.progress(runStatusId, runStatus);
2164
2362
  }
2363
+ const earlierCalls = carried ? await this._callsOf(engine, chatId, runId) : [];
2364
+ const roundBase = carried?.firstRound ?? 0;
2165
2365
  const approvals = this._createApprove(engine, {
2166
2366
  chatId,
2167
2367
  runId,
2168
2368
  deviceId,
2169
2369
  agentId: agent.id,
2170
- hasRun: () => ran
2370
+ hasRun: () => ran,
2371
+ repeats: (call) => earlierCalls.some((chunk) => sameToolCall(chunk, call))
2171
2372
  });
2172
2373
  const idle = idleWatch(controller, this._runIdleTimeoutMs);
2173
2374
  const modelWatch = this._watchModelProgress((runStatus) => transcript.progress(runStatusId, runStatus), busyChatId !== null);
@@ -2198,6 +2399,7 @@ export class QvacAssistant extends ReadyResource {
2198
2399
  let primeRound;
2199
2400
  let primeTokens;
2200
2401
  let injectedHits = 0;
2402
+ let injectedWhole = false;
2201
2403
  const writeText = async (type, raw) => {
2202
2404
  const text = type === 'content' ? sourceTagFilter.push(raw) : raw;
2203
2405
  if (!text) {
@@ -2234,9 +2436,14 @@ export class QvacAssistant extends ReadyResource {
2234
2436
  this._knowledgeCaps.set(chatId, Math.floor(injectedHits / 2));
2235
2437
  this._log?.info('runs', 'knowledge shrunk for retry', { runId, hits: injectedHits });
2236
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
+ }
2237
2444
  runFailure = runFailureNotice(message);
2238
2445
  await transcript.status({ state: 'failed', reason: runFailure, metrics });
2239
- if (folded || shrunk) {
2446
+ if (folded || shrunk || excerpted) {
2240
2447
  await engine
2241
2448
  .runAgent({ chatId, agentId: agent.id, parentRunId: runId })
2242
2449
  .catch((err) => this._log?.warn('runs', 'compaction retry not queued', { runId, err }));
@@ -2305,6 +2512,8 @@ export class QvacAssistant extends ReadyResource {
2305
2512
  ? await this._searchKnowledge(engine, messages, {
2306
2513
  chatId,
2307
2514
  attachmentIds: attachTurnIds,
2515
+ previousQuery: previousQuestion(history),
2516
+ excerptsOnly: this._excerptsOnly.delete(chatId),
2308
2517
  signal: controller.signal,
2309
2518
  preparing: (active) => {
2310
2519
  this._markPreparing(runId, active);
@@ -2330,15 +2539,22 @@ export class QvacAssistant extends ReadyResource {
2330
2539
  const cap = this._knowledgeCaps.get(chatId);
2331
2540
  this._knowledgeCaps.delete(chatId);
2332
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);
2333
2544
  const injected = fitted
2334
2545
  ? await this._injectKnowledge(fitted, {
2335
2546
  transcript,
2336
2547
  sourceTagFilter,
2337
- retrievedRecords,
2338
- pictured: seesImages && messages.some((turn) => turn.pdfs?.length)
2548
+ retrievedRecords
2339
2549
  })
2340
2550
  : { documentsInjected: false, sourcesWritten: false, ragInjected: false };
2341
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);
2342
2558
  const hasAudio = messages.some((turn) => turn.audioAttachments?.length || turn.videos?.length);
2343
2559
  if (hasAudio) {
2344
2560
  modelWatch.transcribe(true);
@@ -2369,9 +2585,18 @@ export class QvacAssistant extends ReadyResource {
2369
2585
  .filter((hit) => !isTruncatedFullTextHit(hit))
2370
2586
  .map(fullTextAttachmentId)
2371
2587
  .filter((id) => id !== null)),
2588
+ citedPages: injected.ragInjected ? citedPdfPages(hits) : null,
2589
+ readablePages: readablePdfTextPages(hits),
2590
+ visualRequest,
2372
2591
  selected: selectedPdfs
2373
2592
  });
2374
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
+ }
2375
2600
  annotateDocumentAttachments(messages);
2376
2601
  for (const { turn, notice } of pdfPages.notices) {
2377
2602
  turn.content = `${turn.content}\n${notice}`;
@@ -2426,9 +2651,11 @@ export class QvacAssistant extends ReadyResource {
2426
2651
  restoreSentContent(messages);
2427
2652
  asked = messages.findLast((turn) => turn.role === 'user')?.content;
2428
2653
  }
2429
- const input = resent ?? nameSenders(messages);
2430
- inputLength = input.length;
2654
+ const named = nameSenders(messages);
2655
+ const input = resent ?? (carried ? carriedInput(named, carried) : named);
2656
+ inputLength = resent ? input.length : named.length;
2431
2657
  rounds.inputLength = inputLength;
2658
+ rounds.roundBase = roundBase;
2432
2659
  const sentImages = earlierImages(messages);
2433
2660
  const inspecting = await this._capturingInferenceCalls();
2434
2661
  steering = new RunInputQueue();
@@ -2448,6 +2675,9 @@ export class QvacAssistant extends ReadyResource {
2448
2675
  replaysContext: true,
2449
2676
  deferTools: runAgent.shared === true || !!request.requesterMemberId,
2450
2677
  modelRound: idle.parks(rounds.port),
2678
+ mediaRound: b4a.equals(agent.providerDeviceId, deviceId)
2679
+ ? undefined
2680
+ : idle.parks(rounds.media),
2451
2681
  session: chatKvCacheKey(chatId),
2452
2682
  thinkingDefault: thinkingDefault(config),
2453
2683
  toolConfigs,
@@ -2456,7 +2686,13 @@ export class QvacAssistant extends ReadyResource {
2456
2686
  driveOp,
2457
2687
  grantedWriteDirs: write,
2458
2688
  approve: idle.parks(approvals.approve),
2459
- 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
+ }),
2460
2696
  fetchAttachment: (query) => this._fetchAttachment(engine, chatId, query),
2461
2697
  showImage: ({ attachmentId }) => this._showImage(engine, { chatId, runId, model, signal: controller.signal }, attachmentId),
2462
2698
  progress: (update) => {
@@ -2469,7 +2705,8 @@ export class QvacAssistant extends ReadyResource {
2469
2705
  signal: controller.signal
2470
2706
  });
2471
2707
  let harnessEvents = 0;
2472
- for await (const event of events) {
2708
+ for await (const raw of events) {
2709
+ const event = afterRounds(raw, roundBase);
2473
2710
  harnessEvents++;
2474
2711
  idle.touch();
2475
2712
  modelWatch.clear();
@@ -2634,7 +2871,10 @@ export class QvacAssistant extends ReadyResource {
2634
2871
  idle.stop();
2635
2872
  modelWatch.stop();
2636
2873
  ownershipWatch.stop();
2637
- 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
+ });
2638
2878
  settledWatch.stop();
2639
2879
  await releaseRunLog();
2640
2880
  if (titleOutcome && !diagramRepair) {
@@ -2874,29 +3114,35 @@ export class QvacAssistant extends ReadyResource {
2874
3114
  return null;
2875
3115
  }
2876
3116
  }
2877
- async _uploadGenerated({ fileName, mimeType, bytes, raw }, { meshId, chatId, runId, deviceId, sourceId }) {
3117
+ async _uploadGenerated({ fileName, mimeType, bytes, raw }, { meshId, chatId, runId, deviceId, sourceId, blobId }) {
2878
3118
  const duplex = this._engine.uploadAttachment();
2879
- duplex.write({
2880
- chatId,
2881
- runId,
2882
- originDeviceId: deviceId,
2883
- fileName,
2884
- mimeType,
2885
- raw,
2886
- meshId,
2887
- sourceId
2888
- });
2889
- const buf = b4a.toBuffer(bytes);
2890
- for (let i = 0; i < buf.length; i += UPLOAD_FRAME_BYTES) {
2891
- duplex.write({ data: buf.subarray(i, i + UPLOAD_FRAME_BYTES) });
2892
- }
2893
- duplex.end();
2894
- let attachment = null;
2895
- for await (const frame of duplex) {
2896
- if (frame.attachment) {
2897
- 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) });
2898
3137
  }
3138
+ duplex.end();
3139
+ }
3140
+ catch (error) {
3141
+ duplex.destroy();
3142
+ await response;
3143
+ throw error;
2899
3144
  }
3145
+ const attachment = await response;
2900
3146
  if (!attachment) {
2901
3147
  throw new Error('attachment upload ended without a record');
2902
3148
  }
@@ -3041,6 +3287,9 @@ export class QvacAssistant extends ReadyResource {
3041
3287
  if (!probe) {
3042
3288
  return 'unsynced';
3043
3289
  }
3290
+ if (!probe.hasVideo && !probe.hasAudio) {
3291
+ throw new Error('no video or audio stream');
3292
+ }
3044
3293
  if (!probe.hasVideo) {
3045
3294
  return 'audio';
3046
3295
  }
@@ -3342,35 +3591,74 @@ export class QvacAssistant extends ReadyResource {
3342
3591
  });
3343
3592
  return file;
3344
3593
  }
3345
- async _hydratePdfPages(messages, { signal, runId, window, reading, textChars, wholeIds, selected }) {
3594
+ async _hydratePdfPages(messages, { signal, runId, window, reading, textChars, wholeIds, citedPages, readablePages, visualRequest, selected }) {
3346
3595
  const rendered = new Set();
3596
+ let shown = false;
3347
3597
  const notices = [];
3348
3598
  const turns = messages.filter((turn) => turn.pdfs?.length);
3349
3599
  if (!turns.length) {
3350
- return { rendered, notices };
3600
+ return { rendered, shown, notices };
3351
3601
  }
3352
3602
  const dir = this._imageCacheDir;
3353
3603
  await fs.promises.mkdir(dir, { recursive: true, mode: 0o700 });
3354
3604
  const pinned = this._runImageCacheNames.get(runId) ?? new Set();
3355
3605
  this._runImageCacheNames.set(runId, pinned);
3356
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
+ }
3357
3619
  const measured = await this._pdfReading(window, reading);
3358
3620
  for (const turn of turns) {
3359
3621
  const room = pdfImageRoom(measured, turn === latestUser ? textChars : 0);
3360
3622
  for (const pdf of turn.pdfs ?? []) {
3361
- const pages = await this._tryHydratePdf({ dir, pdf, pinned, room, signal, selected });
3362
- 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;
3363
3651
  if (pages.names.length && turn === latestUser) {
3364
3652
  rendered.add(pdf.id);
3365
3653
  }
3366
3654
  const whole = turn === latestUser && wholeIds.has(pdf.id);
3367
3655
  for (const notice of pdfNotices(pdf.fileName, pages, whole)) {
3368
- notices.push({ turn, notice });
3656
+ notices.push({ turn: shownOn, notice });
3369
3657
  }
3370
3658
  }
3371
3659
  delete turn.pdfs;
3372
3660
  }
3373
- return { rendered, notices };
3661
+ return { rendered, shown, notices };
3374
3662
  }
3375
3663
  async _pdfReading({ ctxSize, modelName }, options) {
3376
3664
  const custom = await this._customModel(modelName);
@@ -3386,6 +3674,12 @@ export class QvacAssistant extends ReadyResource {
3386
3674
  }
3387
3675
  async _tryHydratePdf(opts) {
3388
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
+ }
3389
3683
  return await this._hydratePdf(opts);
3390
3684
  }
3391
3685
  catch (err) {
@@ -3397,18 +3691,23 @@ export class QvacAssistant extends ReadyResource {
3397
3691
  }
3398
3692
  }
3399
3693
  async _hydratePdf(hydration) {
3400
- const { dir, pdf, pinned, room, signal, selected } = hydration;
3694
+ const { dir, pdf, pinned, room, signal, selected, cited, textFirst, readablePages, visualRequest, reselect, earlier = false } = hydration;
3401
3695
  const key = pdf.pages?.blobId ?? pdf.id;
3402
- const remember = (selection) => {
3403
- this._pdfSelections.set(key, selection);
3696
+ const remember = (selection, persist = true) => {
3697
+ if (persist) {
3698
+ this._pdfSelections.set(key, selection);
3699
+ }
3404
3700
  for (const { page, clip } of selection.shots) {
3405
3701
  this._mediaRefs.set(pdfShotCacheName(pdf, { page, clip }), { id: pdf.id, page, clip });
3406
3702
  }
3407
- if (!pdf.pages) {
3703
+ if (persist && !pdf.pages) {
3408
3704
  selected.push({ ref: pdf, selection });
3409
3705
  }
3410
3706
  };
3411
- 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;
3412
3711
  if (known) {
3413
3712
  remember(known);
3414
3713
  const cached = await cachedPdfPages(known, { dir, pdf, pinned });
@@ -3416,6 +3715,9 @@ export class QvacAssistant extends ReadyResource {
3416
3715
  return cached;
3417
3716
  }
3418
3717
  }
3718
+ if (earlier && !known && !visualRequest && !cited?.size) {
3719
+ return NO_PDF_PAGES;
3720
+ }
3419
3721
  const bytes = await this._readBlob(pdf.url, signal);
3420
3722
  if (signal.aborted) {
3421
3723
  throw new Error('hydration aborted');
@@ -3425,10 +3727,49 @@ export class QvacAssistant extends ReadyResource {
3425
3727
  return NO_PDF_PAGES;
3426
3728
  }
3427
3729
  try {
3428
- const selection = known ?? (await selectPdfPages(doc, room));
3730
+ const selection = known ?? (await selectPdfPages(doc, room, { cited, textFirst, readablePages }));
3429
3731
  if (!known) {
3430
- 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;
3431
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 {
3432
3773
  return await this._renderPdfShots(doc, selection, hydration);
3433
3774
  }
3434
3775
  finally {
@@ -3819,7 +4160,7 @@ export class QvacAssistant extends ReadyResource {
3819
4160
  }
3820
4161
  }
3821
4162
  const title = generated ?? deriveChatTitle(prompt);
3822
- if (!title) {
4163
+ if (!title || !isDefaultChatTitle((await engine.getChat({ chatId }))?.title)) {
3823
4164
  return null;
3824
4165
  }
3825
4166
  await engine.renameChat({ chatId, config: { title } });
@@ -3884,7 +4225,7 @@ export class QvacAssistant extends ReadyResource {
3884
4225
  }
3885
4226
  return text;
3886
4227
  }
3887
- _createApprove(engine, { chatId, runId, deviceId, agentId, hasRun }) {
4228
+ _createApprove(engine, { chatId, runId, deviceId, agentId, hasRun, repeats }) {
3888
4229
  const waiters = new Map();
3889
4230
  let latestChunks = [];
3890
4231
  let watcher = null;
@@ -3973,13 +4314,16 @@ export class QvacAssistant extends ReadyResource {
3973
4314
  this._log?.warn('approvals', 'rule lookup failed, asking instead', { runId, err });
3974
4315
  return { rules: [], ourMemberId: undefined };
3975
4316
  });
3976
- const rule = matchApprovalRule(rules ?? [], {
3977
- toolName: call.name,
3978
- resource: call.resource,
3979
- agentId,
3980
- chatId,
3981
- memberId: ourMemberId
3982
- });
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
+ });
3983
4327
  if (rule) {
3984
4328
  this._log?.info('approvals', `rule ${rule.decision}`, {
3985
4329
  runId,
@@ -4002,7 +4346,7 @@ export class QvacAssistant extends ReadyResource {
4002
4346
  });
4003
4347
  return park(prior.id, call.name);
4004
4348
  }
4005
- if (!hasRun()) {
4349
+ if (!hasRun() && !repeated) {
4006
4350
  this._log?.info('approvals', `answer ${status} (from before the app closed)`, {
4007
4351
  runId,
4008
4352
  chatId,
@@ -4293,6 +4637,10 @@ export class QvacAssistant extends ReadyResource {
4293
4637
  };
4294
4638
  }
4295
4639
  }
4640
+ function previousQuestion(history) {
4641
+ const asked = history.flatMap((chunk) => chunk.userMessage?.text?.trim() || []);
4642
+ return asked.at(-2);
4643
+ }
4296
4644
  function notReady(search) {
4297
4645
  return {
4298
4646
  chatFilesNotReady: search.chatFilesNotReady ?? 0,
@@ -4303,19 +4651,28 @@ function notReadyCount(search) {
4303
4651
  const { chatFilesNotReady, projectFilesNotReady } = notReady(search);
4304
4652
  return chatFilesNotReady + projectFilesNotReady;
4305
4653
  }
4306
- function notReadyLine(search) {
4654
+ export function notReadyLine(search) {
4307
4655
  const { projectFilesNotReady } = notReady(search);
4308
- const chat = search.pendingFiles ?? [];
4309
- 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 = [];
4310
4660
  if (chat.length) {
4311
- parts.push(`${filesCount(chat.length, 'chat')} (${chat.join(', ')})`);
4661
+ indexing.push(`${filesCount(chat.length, 'chat')} (${chat.join(', ')})`);
4312
4662
  }
4313
4663
  if (projectFilesNotReady) {
4314
- parts.push(filesCount(projectFilesNotReady, 'project'));
4664
+ indexing.push(filesCount(projectFilesNotReady, 'project'));
4315
4665
  }
4316
- return parts.length
4317
- ? `[Still indexing and not used for this reply: ${parts.join(' and ')}.]`
4318
- : 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))];
4319
4676
  }
4320
4677
  function filesCount(count, source) {
4321
4678
  return `${count} ${source} file${count === 1 ? '' : 's'}`;
@@ -4326,7 +4683,7 @@ function chunkSources(records) {
4326
4683
  function downloadingProgress(model, percentage) {
4327
4684
  return { phase: 'downloading', model, percentage: Math.floor(percentage / 10) * 10 };
4328
4685
  }
4329
- async function injectRetrievedKnowledge(knowledge, { transcript, sourceTagFilter, retrievedRecords, pictured, injected }) {
4686
+ async function injectRetrievedKnowledge(knowledge, { transcript, sourceTagFilter, retrievedRecords, injected }) {
4330
4687
  const records = sourcesFromHits(knowledge.hits, RAG_SOURCE_BOUNDS);
4331
4688
  const notice = chooseRagNotice({
4332
4689
  errorMessage: knowledge.search.errorMessage,
@@ -4353,16 +4710,35 @@ async function injectRetrievedKnowledge(knowledge, { transcript, sourceTagFilter
4353
4710
  });
4354
4711
  injected.sourcesWritten = true;
4355
4712
  if (records.length > 0) {
4356
- const framing = formatRagContext(knowledge.hits, knowledge.search.availableFiles ?? []);
4357
- const pages = pictured ? `\n${PDF_PAGES_SOURCE_LINE}` : '';
4358
- 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}`;
4359
4721
  injected.ragInjected = true;
4360
4722
  injected.documentsInjected = true;
4361
4723
  }
4362
- else if (knowledge.search.availableFiles?.length) {
4363
- 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
+ }
4364
4732
  }
4365
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
+ }
4366
4742
  function noop() { }
4367
4743
  function toolCallChunk(event) {
4368
4744
  return {
@@ -4381,6 +4757,15 @@ function toolResultChunk(event) {
4381
4757
  ...callOf(event)
4382
4758
  };
4383
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
+ }
4384
4769
  function roundOf(event) {
4385
4770
  return typeof event.round === 'number' ? event.round : undefined;
4386
4771
  }