@qvac/core 0.6.7 → 0.7.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 (146) hide show
  1. package/CHANGELOG.md +211 -1
  2. package/README.md +2 -0
  3. package/dist-lib/lib/api/app-config.d.ts +3 -1
  4. package/dist-lib/lib/api/app-config.js +15 -4
  5. package/dist-lib/lib/api/chat.d.ts +1 -1
  6. package/dist-lib/lib/api/chat.js +9 -0
  7. package/dist-lib/lib/api/index.d.ts +2 -1
  8. package/dist-lib/lib/api/index.js +2 -1
  9. package/dist-lib/lib/api/network.d.ts +8 -1
  10. package/dist-lib/lib/api/network.js +6 -0
  11. package/dist-lib/lib/assistant/fake-models.d.ts +22 -0
  12. package/dist-lib/lib/assistant/fake-models.js +112 -0
  13. package/dist-lib/lib/assistant/harness.d.ts +12 -0
  14. package/dist-lib/lib/assistant/harness.js +26 -0
  15. package/dist-lib/lib/assistant/history.d.ts +5 -1
  16. package/dist-lib/lib/assistant/history.js +14 -2
  17. package/dist-lib/lib/assistant-tree.d.ts +4 -0
  18. package/dist-lib/lib/assistant-tree.js +45 -7
  19. package/dist-lib/lib/assistant.d.ts +15 -2
  20. package/dist-lib/lib/assistant.js +181 -42
  21. package/dist-lib/lib/constants.d.ts +6 -0
  22. package/dist-lib/lib/constants.js +6 -0
  23. package/dist-lib/lib/core/agent.d.ts +2 -0
  24. package/dist-lib/lib/core/agent.js +16 -5
  25. package/dist-lib/lib/core/blind-peers.d.ts +4 -0
  26. package/dist-lib/lib/core/blind-peers.js +1 -0
  27. package/dist-lib/lib/core/chat-activity.d.ts +1 -1
  28. package/dist-lib/lib/core/chat-activity.js +3 -0
  29. package/dist-lib/lib/core/chat-copy.d.ts +4 -0
  30. package/dist-lib/lib/core/chat-copy.js +1 -1
  31. package/dist-lib/lib/core/chat-fork.d.ts +21 -0
  32. package/dist-lib/lib/core/chat-fork.js +245 -0
  33. package/dist-lib/lib/core/chat-move.js +1 -4
  34. package/dist-lib/lib/core/chat.d.ts +1 -0
  35. package/dist-lib/lib/core/chat.js +1 -1
  36. package/dist-lib/lib/core/collections.d.ts +1 -0
  37. package/dist-lib/lib/core/collections.js +1 -0
  38. package/dist-lib/lib/core/contract/compute.d.ts +1 -1
  39. package/dist-lib/lib/core/contract/compute.js +2 -1
  40. package/dist-lib/lib/core/contract/index.js +6 -1
  41. package/dist-lib/lib/core/contract/invites.d.ts +1 -11
  42. package/dist-lib/lib/core/contract/invites.js +3 -3
  43. package/dist-lib/lib/core/contract/mesh-seeds.d.ts +2 -0
  44. package/dist-lib/lib/core/contract/mesh-seeds.js +26 -0
  45. package/dist-lib/lib/core/contract/open-join.d.ts +2 -0
  46. package/dist-lib/lib/core/contract/open-join.js +57 -0
  47. package/dist-lib/lib/core/departures.d.ts +8 -1
  48. package/dist-lib/lib/core/departures.js +59 -2
  49. package/dist-lib/lib/core/fs-drives.d.ts +8 -1
  50. package/dist-lib/lib/core/fs-drives.js +47 -15
  51. package/dist-lib/lib/core/harness-loan.d.ts +4 -20
  52. package/dist-lib/lib/core/harness-loan.js +7 -184
  53. package/dist-lib/lib/core/id.d.ts +1 -0
  54. package/dist-lib/lib/core/id.js +3 -0
  55. package/dist-lib/lib/core/identity.d.ts +1 -1
  56. package/dist-lib/lib/core/identity.js +6 -4
  57. package/dist-lib/lib/core/invite.d.ts +2 -0
  58. package/dist-lib/lib/core/invite.js +6 -2
  59. package/dist-lib/lib/core/membership.d.ts +1 -12
  60. package/dist-lib/lib/core/membership.js +1 -94
  61. package/dist-lib/lib/core/mesh.d.ts +11 -15
  62. package/dist-lib/lib/core/mesh.js +52 -88
  63. package/dist-lib/lib/core/migrations/agent-tools-scope.d.ts +2 -0
  64. package/dist-lib/lib/core/migrations/agent-tools-scope.js +24 -0
  65. package/dist-lib/lib/core/migrations/index.js +3 -1
  66. package/dist-lib/lib/core/peer-duplex.d.ts +32 -0
  67. package/dist-lib/lib/core/peer-duplex.js +207 -0
  68. package/dist-lib/lib/core/sync/index.js +2 -16
  69. package/dist-lib/lib/core.d.ts +47 -50
  70. package/dist-lib/lib/core.js +524 -878
  71. package/dist-lib/lib/diagnostics.d.ts +7 -0
  72. package/dist-lib/lib/diagnostics.js +22 -0
  73. package/dist-lib/lib/doctor/checks.d.ts +15 -0
  74. package/dist-lib/lib/doctor/checks.js +272 -0
  75. package/dist-lib/lib/doctor/redact.d.ts +4 -0
  76. package/dist-lib/lib/doctor/redact.js +31 -0
  77. package/dist-lib/lib/doctor/render.d.ts +8 -0
  78. package/dist-lib/lib/doctor/render.js +83 -0
  79. package/dist-lib/lib/doctor/tree.d.ts +5 -0
  80. package/dist-lib/lib/doctor/tree.js +167 -0
  81. package/dist-lib/lib/downloads-allowed-pusher.d.ts +10 -0
  82. package/dist-lib/lib/downloads-allowed-pusher.js +37 -0
  83. package/dist-lib/lib/guards.d.ts +1 -0
  84. package/dist-lib/lib/guards.js +4 -0
  85. package/dist-lib/lib/inference-calls.js +1 -5
  86. package/dist-lib/lib/kv-cache-reclaim.d.ts +2 -1
  87. package/dist-lib/lib/kv-cache-reclaim.js +24 -0
  88. package/dist-lib/lib/mesh-scoped-engine.js +4 -0
  89. package/dist-lib/lib/model-download-status.d.ts +1 -1
  90. package/dist-lib/lib/model-download.d.ts +9 -10
  91. package/dist-lib/lib/model-download.js +49 -16
  92. package/dist-lib/lib/model-memory-cap.d.ts +29 -0
  93. package/dist-lib/lib/model-memory-cap.js +69 -0
  94. package/dist-lib/lib/models-watch.d.ts +1 -1
  95. package/dist-lib/lib/models-watch.js +4 -4
  96. package/dist-lib/lib/oauth/flow.js +1 -1
  97. package/dist-lib/lib/oauth/loopback.js +16 -2
  98. package/dist-lib/lib/oauth/redirect-types.d.ts +1 -0
  99. package/dist-lib/lib/run-replay.d.ts +26 -0
  100. package/dist-lib/lib/run-replay.js +107 -0
  101. package/dist-lib/schema/config.d.ts +1 -1
  102. package/dist-lib/schema/config.js +1 -1
  103. package/dist-lib/schema/content.js +9 -1
  104. package/dist-lib/schema/engine.js +75 -6
  105. package/dist-lib/schema/field-order.js +1 -1
  106. package/dist-lib/schema/local.js +3 -2
  107. package/dist-lib/schema/qvac.js +28 -2
  108. package/dist-lib/spec/bind.js +17 -0
  109. package/dist-lib/spec/calls.js +4 -0
  110. package/dist-lib/spec/capabilities.d.ts +8 -0
  111. package/dist-lib/spec/config/hyperconf/index.js +1 -1
  112. package/dist-lib/spec/hrpc/hrpc.json +52 -0
  113. package/dist-lib/spec/hrpc/index.js +53 -4
  114. package/dist-lib/spec/hrpc/messages.js +1498 -1278
  115. package/dist-lib/spec/hyperschema/index.js +1498 -1278
  116. package/dist-lib/spec/hyperschema/schema.json +182 -0
  117. package/dist-lib/spec/hyperschema/types.d.ts +49 -3
  118. package/dist-lib/spec/local/hyperdb/messages.js +22 -10
  119. package/dist-lib/spec/local/hyperschema/index.js +16 -7
  120. package/dist-lib/spec/local/hyperschema/schema.json +10 -1
  121. package/dist-lib/spec/local/hyperschema/types.d.ts +3 -2
  122. package/dist-lib/spec/mesh/hyperdb/db.json +16 -1
  123. package/dist-lib/spec/mesh/hyperdb/index.js +71 -2
  124. package/dist-lib/spec/mesh/hyperdb/messages.js +530 -246
  125. package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +13 -1
  126. package/dist-lib/spec/mesh/hyperdispatch/index.js +35 -1
  127. package/dist-lib/spec/mesh/hyperdispatch/messages.js +223 -10
  128. package/dist-lib/spec/mesh/hyperschema/index.js +223 -10
  129. package/dist-lib/spec/mesh/hyperschema/schema.json +128 -1
  130. package/dist-lib/spec/mesh/hyperschema/types.d.ts +34 -2
  131. package/package.json +14 -19
  132. package/tui/README.md +7 -1
  133. package/tui/app.mjs +366 -78
  134. package/tui/boot.mjs +1 -0
  135. package/tui/index.mjs +1 -0
  136. package/tui/lib/fork-command.mjs +43 -0
  137. package/tui/lib/render.mjs +25 -1
  138. package/worker/assistant-sidecar.mjs +5 -2
  139. package/dist-lib/lib/core/device-invites.d.ts +0 -11
  140. package/dist-lib/lib/core/device-invites.js +0 -63
  141. package/dist-lib/lib/core/grant-expiry.d.ts +0 -11
  142. package/dist-lib/lib/core/grant-expiry.js +0 -66
  143. package/dist-lib/lib/core/mesh-seed-handoff.d.ts +0 -27
  144. package/dist-lib/lib/core/mesh-seed-handoff.js +0 -53
  145. package/dist-lib/lib/oauth/loopback.stub.d.ts +0 -3
  146. package/dist-lib/lib/oauth/loopback.stub.js +0 -6
@@ -27,14 +27,15 @@ import { annotateDocumentAttachments, documentUnreadNotice, documentUnreadReason
27
27
  import { imageCacheKey, imageCacheName, sweepImageCache } from './core/image/cache-sweep.js';
28
28
  import { IMAGE_MIMES, normalizeImageForModel } from './core/image/normalize.js';
29
29
  import { MAX_PDF_PAGE_IMAGES, pdfRasterizer } from './core/pdf/rasterize.js';
30
- import { summarizeRuns } from './diagnostics.js';
30
+ import { recordBackends, summarizeRuns } from './diagnostics.js';
31
31
  import { resolveFsGrants } from './fs-drive-bridge.js';
32
32
  import { InferenceCallStore } from './inference-calls.js';
33
+ import { LOCAL_HOLDER, RunReplayStore, trackSentRun } from './run-replay.js';
33
34
  import { fsGrantKey, fsWriteGrantKey } from './fs-grant-keys.js';
34
35
  import { fsGrantsFrom, grantedDirsFrom } from './fs-grants.js';
35
36
  import { defaultImageCacheDir } from './image-cache-dir.js';
36
37
  import { ModelCatalogRevisionHub } from './model-catalog-revision.js';
37
- import { agentRekeyFingerprint, chatKvCacheKey, chatRepairKvCacheKey, disappearedChatIds } from './kv-cache-reclaim.js';
38
+ import { agentRekeyFingerprint, agentSwitched, chatKvCacheKey, chatRepairKvCacheKey, disappearedChatIds } from './kv-cache-reclaim.js';
38
39
  import { buildCompactionPrompt, canCompact, foldReducesPrompt, COMPACTION_KEEP_TURNS, COMPACTION_TURN_PREFIX, isContextOverflowFailure, nearContextLimit, compactionBoundary, lastCompactedThroughId, parseCompactionSummary } from './compaction.js';
39
40
  import { ModelDownloadReconciler, musicModelChoiceOf } from './model-download.js';
40
41
  import { AbortedError, raceAbort } from '@qvac/harness/race-abort';
@@ -42,7 +43,7 @@ import { RunTranscript } from './run-transcript.js';
42
43
  import { searchKnowledge } from './knowledge-search.js';
43
44
  import { isLifecycleTeardown, knownAbortOutcome, RUN_ABORT_REASONS, RUN_FAILURE_REASONS, runFailureNotice } from './run-notices.js';
44
45
  import { encodeAgentSkillReadiness } from './skill-readiness.js';
45
- import { harnessClearKvCache, harnessDeathReason, harnessDeleteModel, harnessDied, harnessListModels, harnessPreviewModelSource, harnessSkillCatalog, harnessSkillManagement, harnessDetectLanguage, harnessSpeak, harnessSpeakStream, harnessTools, harnessTranscribe, imageDownloadProgress, isHarnessEvent, modelCatalogChanged, toolReportEntry } from './assistant/harness.js';
46
+ import { harnessClearKvCache, harnessDeathReason, harnessDeleteModel, harnessDied, harnessListModels, harnessPreviewModelSource, harnessSkillCatalog, harnessSkillManagement, harnessDetectLanguage, harnessSpeak, harnessSpeakStream, harnessTools, harnessTranscribe, heldLoadModel, droppedHeldLoadModel, imageDownloadProgress, isHarnessEvent, modelCatalogChanged, toolReportEntry } from './assistant/harness.js';
46
47
  import { annotateUnreadableImages, audioTranscriptBlock, backfillDocumentOnlyTurn, backfillImageOnlyTurns, silentAudioNotice, unheardAudioNotice, nameSenders } from './assistant/message.js';
47
48
  import { drainBody, raceInactivity } from './assistant/fetch-body.js';
48
49
  import { GENERIC_ACKNOWLEDGEMENTS, HISTORY_TYPES, SKILL_TOOL, TERMINAL_STATES, couldNameSenders, historyFromChunks, inputAvailableToRun, listHistoryChunks, senderAttribution, senderNamesFrom, staleToolRunIds } from './assistant/history.js';
@@ -171,6 +172,7 @@ export class QvacAssistant extends ReadyResource {
171
172
  _harnessDiagnostics;
172
173
  _harnessDeath;
173
174
  _refreshingDiagnostics;
175
+ _backendUse = new Map();
174
176
  _meshList;
175
177
  _accountFrames;
176
178
  _accountMeshId;
@@ -181,6 +183,7 @@ export class QvacAssistant extends ReadyResource {
181
183
  _runs;
182
184
  _seen;
183
185
  _inferenceCalls;
186
+ _runReplay;
184
187
  _lifecycle;
185
188
  _audioTranscripts;
186
189
  _pdfPages;
@@ -191,7 +194,7 @@ export class QvacAssistant extends ReadyResource {
191
194
  _modelCatalog = new ModelCatalogRevisionHub();
192
195
  _onHarnessStatus;
193
196
  _onModelCatalogEvent;
194
- constructor(engine, harness, { logger = null, imageCacheDir, inferenceCallsDir, imageCacheMaxBytes, imageCacheMaxAgeMs, downloadRetryMs, platform = os.platform(), preloadCurrentAgentModel = true, drives, borrowHarness, blobReadTimeoutMs = BLOB_READ_TIMEOUT_MS, pdfRasterizer: pdfRasterizerOption = pdfRasterizer } = {}) {
197
+ constructor(engine, harness, { logger = null, imageCacheDir, inferenceCallsDir, runReplayDir, imageCacheMaxBytes, imageCacheMaxAgeMs, downloadRetryMs, platform = os.platform(), preloadCurrentAgentModel = true, drives, borrowHarness, blobReadTimeoutMs = BLOB_READ_TIMEOUT_MS, pdfRasterizer: pdfRasterizerOption = pdfRasterizer } = {}) {
195
198
  super();
196
199
  this._engine = engine;
197
200
  this._harness = harness;
@@ -227,6 +230,7 @@ export class QvacAssistant extends ReadyResource {
227
230
  this._runs = new Map();
228
231
  this._seen = new Map();
229
232
  this._inferenceCalls = new InferenceCallStore(inferenceCallsDir);
233
+ this._runReplay = new RunReplayStore(runReplayDir);
230
234
  this._lifecycle = new AbortController();
231
235
  this._audioTranscripts = new Map();
232
236
  this._pdfPages = new Map();
@@ -256,6 +260,14 @@ export class QvacAssistant extends ReadyResource {
256
260
  if (progress) {
257
261
  this._downloads?.observeImageProgress(progress);
258
262
  }
263
+ const held = heldLoadModel(state);
264
+ if (held) {
265
+ this._downloads?.observeHeldLoad(held);
266
+ }
267
+ const dropped = droppedHeldLoadModel(state);
268
+ if (dropped) {
269
+ this._downloads?.observeDroppedHeldLoad(dropped);
270
+ }
259
271
  };
260
272
  }
261
273
  _requireDeviceId() {
@@ -273,6 +285,7 @@ export class QvacAssistant extends ReadyResource {
273
285
  this._harness.on?.('status', this._onHarnessStatus);
274
286
  this._harness.on?.('model', this._onModelCatalogEvent);
275
287
  this._info = await this._describeHarness();
288
+ await this._clearPreReplayCaches();
276
289
  await this._clearPersistedPauses();
277
290
  this._downloads = new ModelDownloadReconciler({
278
291
  engine: this._engine,
@@ -500,7 +513,8 @@ export class QvacAssistant extends ReadyResource {
500
513
  assistant: summarizeRuns(runs, this.suspended),
501
514
  platform: this._platform,
502
515
  mobileClass: this._mobileClass,
503
- memoryBudget: this._capability?.memoryBudget ?? null
516
+ memoryBudget: this._capability?.memoryBudget ?? null,
517
+ backendUse: Object.fromEntries([...this._backendUse].map(([tool, uses]) => [tool, [...uses]]))
504
518
  };
505
519
  }
506
520
  async _refreshHarnessDiagnostics() {
@@ -693,6 +707,9 @@ export class QvacAssistant extends ReadyResource {
693
707
  this._lifecycle.abort(RUNNER_CLOSED);
694
708
  await Promise.allSettled([...this._runs.values()].map((run) => run.done));
695
709
  }
710
+ get mobileClass() {
711
+ return this._mobileClass;
712
+ }
696
713
  get suspended() {
697
714
  return this._state === QvacState.SUSPEND;
698
715
  }
@@ -876,6 +893,10 @@ export class QvacAssistant extends ReadyResource {
876
893
  notifyModelCatalog() {
877
894
  this._modelCatalog.notify();
878
895
  }
896
+ cachedModelsChanged() {
897
+ this._modelCatalog.notify();
898
+ this._reportCapabilities().catch(noop);
899
+ }
879
900
  async retryModelDownload(name) {
880
901
  if (!this._downloads) {
881
902
  throw new Error('model download reconciler is unavailable');
@@ -998,7 +1019,9 @@ export class QvacAssistant extends ReadyResource {
998
1019
  }
999
1020
  this._reconciledCompactions.delete(chatId);
1000
1021
  this._chatAttribution.delete(chatId);
1001
- void this._deleteChatKvCache(chatId, 'chat deleted');
1022
+ void this._deleteChatKvCache(chatId, 'chat deleted').finally(() => this._runReplay
1023
+ .forget(chatId)
1024
+ .catch((err) => this._log?.warn('assistant', 'run replay not dropped', { err })));
1002
1025
  void this._inferenceCalls
1003
1026
  .forget(chatId)
1004
1027
  .catch((err) => this._log?.warn('assistant', 'inspector records not dropped', { err }));
@@ -1014,26 +1037,26 @@ export class QvacAssistant extends ReadyResource {
1014
1037
  for (const agent of agents) {
1015
1038
  const previous = known?.get(agent.id);
1016
1039
  if (previous !== undefined && previous !== next.get(agent.id)) {
1017
- void this._reclaimAgentChatCaches(meshId, agent.id);
1040
+ void this._reclaimAgentChatCaches(meshId, agent);
1018
1041
  }
1019
1042
  }
1020
1043
  known = next;
1021
1044
  }
1022
1045
  }
1023
- async _reclaimAgentChatCaches(meshId, agentId) {
1046
+ async _reclaimAgentChatCaches(meshId, agent) {
1024
1047
  try {
1025
1048
  const { chats } = await this._engine.listChats({ meshId });
1026
1049
  for (const chat of chats ?? []) {
1027
- if (chat.agentId === agentId) {
1028
- await this._deleteChatKvCache(chat.id, 'agent re-key');
1050
+ if (chat.agentId === agent.id) {
1051
+ await this._deleteChatKvCache(chat.id, 'agent re-key', this._holderOf(agent));
1029
1052
  }
1030
1053
  }
1031
1054
  }
1032
1055
  catch (err) {
1033
- this._log?.warn('assistant', 'agent re-key cache reclaim failed', { agentId, err });
1056
+ this._log?.warn('assistant', 'agent re-key cache reclaim failed', { agentId: agent.id, err });
1034
1057
  }
1035
1058
  }
1036
- async _reconcileCompaction(engine, chatId) {
1059
+ async _reconcileCompaction(engine, chatId, holder) {
1037
1060
  const deviceId = this._requireDeviceId();
1038
1061
  const { chunks } = await engine
1039
1062
  .listChunks({ chatId, type: 'compaction' })
@@ -1050,7 +1073,7 @@ export class QvacAssistant extends ReadyResource {
1050
1073
  if (this._reconciledCompactions.get(chatId) === newest.id) {
1051
1074
  return;
1052
1075
  }
1053
- const gone = await this._deleteChatKvCache(chatId, 'compaction from another device');
1076
+ const gone = await this._deleteChatKvCache(chatId, 'compaction from another device', holder);
1054
1077
  if (gone) {
1055
1078
  this._reconciledCompactions.set(chatId, newest.id);
1056
1079
  }
@@ -1066,7 +1089,7 @@ export class QvacAssistant extends ReadyResource {
1066
1089
  ]);
1067
1090
  return senderNamesFrom(chunks, members ?? [], devices ?? []);
1068
1091
  }
1069
- async _reconcileAttribution(chatId, senders) {
1092
+ async _reconcileAttribution(chatId, senders, holder) {
1070
1093
  const attribution = senderAttribution(senders);
1071
1094
  const seen = this._chatAttribution.get(chatId);
1072
1095
  if (seen === attribution) {
@@ -1076,20 +1099,78 @@ export class QvacAssistant extends ReadyResource {
1076
1099
  this._chatAttribution.set(chatId, attribution);
1077
1100
  return;
1078
1101
  }
1079
- const gone = await this._deleteChatKvCache(chatId, 'sender attribution changed');
1102
+ const gone = await this._deleteChatKvCache(chatId, 'sender attribution changed', holder);
1080
1103
  if (gone) {
1081
1104
  this._chatAttribution.set(chatId, attribution);
1082
1105
  }
1083
1106
  }
1084
- async deleteChatKvCache(chatId) {
1085
- return ((await this._harness.deleteKvCache?.(chatKvCacheKey(chatId))) ?? {
1107
+ async deleteChatKvCache(chatId, holder = LOCAL_HOLDER) {
1108
+ const harness = this._holderHarness(holder);
1109
+ const result = (await harness.deleteKvCache?.(chatKvCacheKey(chatId))) ?? {
1086
1110
  status: 'unsupported',
1087
1111
  error: 'Harness does not support KV cache deletion'
1088
- });
1112
+ };
1113
+ if (result.status === 'success') {
1114
+ await this._runReplay.forget(chatId, holder);
1115
+ }
1116
+ return result;
1089
1117
  }
1090
- async _deleteChatKvCache(chatId, reason) {
1118
+ async clearKvCache() {
1119
+ const result = await harnessClearKvCache(this._harness)();
1120
+ if (result.status === 'success') {
1121
+ await this._runReplay.clear(LOCAL_HOLDER);
1122
+ }
1123
+ return result;
1124
+ }
1125
+ async _clearPreReplayCaches() {
1126
+ if (await this._runReplay.started())
1127
+ return;
1128
+ const result = await this.clearKvCache();
1129
+ if (result.status === 'failure') {
1130
+ this._log?.warn('assistant', 'pre-replay kv caches not cleared', { error: result.error });
1131
+ return;
1132
+ }
1133
+ await this._runReplay.start();
1134
+ }
1135
+ _inspectCall(inspecting, chatId, entry) {
1136
+ if (!inspecting)
1137
+ return;
1138
+ void this._inferenceCalls
1139
+ .record(chatId, entry)
1140
+ .catch((err) => this._log?.warn('assistant', 'inference call not recorded', { err }));
1141
+ }
1142
+ _inspectSent(inspecting, chatId, sent) {
1143
+ if (!inspecting)
1144
+ return;
1145
+ void this._inferenceCalls
1146
+ .recordSent(chatId, sent)
1147
+ .catch((err) => this._log?.warn('assistant', 'sent slice not recorded', { err }));
1148
+ }
1149
+ async _sentRuns(chatId, holder) {
1091
1150
  try {
1092
- const result = await this.deleteChatKvCache(chatId);
1151
+ return await this._runReplay.load({ holder, chatId });
1152
+ }
1153
+ catch (err) {
1154
+ this._log?.warn('assistant', 'run replay unreadable', { chatId, err });
1155
+ await this._deleteChatKvCache(chatId, 'run replay unreadable', holder);
1156
+ return new Map();
1157
+ }
1158
+ }
1159
+ async _recordSentRun(chatId, { runId, turns, holder }) {
1160
+ if (turns) {
1161
+ try {
1162
+ await this._runReplay.save({ holder, chatId }, runId, turns);
1163
+ return;
1164
+ }
1165
+ catch (err) {
1166
+ this._log?.warn('assistant', 'run replay not saved', { chatId, runId, err });
1167
+ }
1168
+ }
1169
+ await this._deleteChatKvCache(chatId, 'run not replayable as sent', holder);
1170
+ }
1171
+ async _deleteChatKvCache(chatId, reason, holder = LOCAL_HOLDER) {
1172
+ try {
1173
+ const result = await this.deleteChatKvCache(chatId, holder);
1093
1174
  if (result.status === 'failure') {
1094
1175
  this._log?.warn('assistant', 'chat kv-cache deletion failed', {
1095
1176
  chatId,
@@ -1230,7 +1311,13 @@ export class QvacAssistant extends ReadyResource {
1230
1311
  return own && id === b4a.toHex(own) ? null : id;
1231
1312
  }
1232
1313
  _harnessFor(agent) {
1233
- const id = this._lenderFor(agent);
1314
+ return this._holderHarness(this._holderOf(agent));
1315
+ }
1316
+ _holderOf(agent) {
1317
+ return this._lenderFor(agent) ?? LOCAL_HOLDER;
1318
+ }
1319
+ _holderHarness(holder) {
1320
+ const id = holder === LOCAL_HOLDER ? null : holder;
1234
1321
  const borrow = this._borrowHarness;
1235
1322
  if (!id || !borrow) {
1236
1323
  return this._harness;
@@ -1432,9 +1519,10 @@ export class QvacAssistant extends ReadyResource {
1432
1519
  runId
1433
1520
  });
1434
1521
  }
1435
- await this._reconcileCompaction(engine, chatId);
1522
+ const holder = this._holderOf(agent);
1523
+ await this._reconcileCompaction(engine, chatId, holder);
1436
1524
  if (request.parentId) {
1437
- await this._deleteChatKvCache(chatId, 'superseding run');
1525
+ await this._deleteChatKvCache(chatId, 'superseding run', holder);
1438
1526
  }
1439
1527
  const lender = this._lenderFor(agent);
1440
1528
  const model = lender
@@ -1477,6 +1565,12 @@ export class QvacAssistant extends ReadyResource {
1477
1565
  const toolConfigsPromise = this._loadToolConfigs(engine, agent.id);
1478
1566
  const fsGrantsPromise = this._loadFsGrants(agent.id);
1479
1567
  const all = await listHistoryChunks(engine, chatId);
1568
+ if (agentSwitched(all, request, deviceId)) {
1569
+ await this._deleteChatKvCache(chatId, 'agent switched');
1570
+ if (holder !== LOCAL_HOLDER) {
1571
+ await this._deleteChatKvCache(chatId, 'agent switched', holder);
1572
+ }
1573
+ }
1480
1574
  const branches = historyBranches(all, runId);
1481
1575
  const currentRepair = branches.repairByRunId.get(runId);
1482
1576
  const repairParent = currentRepair
@@ -1499,9 +1593,14 @@ export class QvacAssistant extends ReadyResource {
1499
1593
  const named = couldNameSenders(history);
1500
1594
  const senders = named ? await this._senderNames(engine, history) : NO_SENDERS;
1501
1595
  if (named || this._chatAttribution.get(chatId)) {
1502
- await this._reconcileAttribution(chatId, senders);
1503
- }
1504
- const messages = historyFromChunks(history, staleToolRunIds(history, priorAsks ?? [], runId), senders);
1596
+ await this._reconcileAttribution(chatId, senders, holder);
1597
+ }
1598
+ const stale = staleToolRunIds(history, priorAsks ?? [], runId);
1599
+ const sentRuns = await this._sentRuns(chatId, holder);
1600
+ const messages = historyFromChunks(history, stale, { senderNames: senders, sentRuns });
1601
+ const typedText = currentRepair
1602
+ ? undefined
1603
+ : history.findLast((chunk) => chunk.userMessage)?.userMessage?.text;
1505
1604
  if (currentRepair) {
1506
1605
  messages.push({ role: 'user', content: currentRepair.promptText });
1507
1606
  }
@@ -1540,9 +1639,17 @@ export class QvacAssistant extends ReadyResource {
1540
1639
  const settledWatch = this._watchSettled(engine, { chatId, runId, controller });
1541
1640
  const ownershipWatch = this._watchOwnership(engine, agent.id, controller);
1542
1641
  let readinessError = null;
1642
+ const held = { status: null };
1543
1643
  try {
1544
1644
  if (!lender) {
1545
- await this._downloads?.waitUntilCached(model, controller.signal);
1645
+ await this._downloads?.waitUntilCached(model, controller.signal, {
1646
+ onHold: () => {
1647
+ held.status = transcript.status({
1648
+ state: 'executing',
1649
+ progress: { phase: 'awaiting-download', model }
1650
+ });
1651
+ }
1652
+ });
1546
1653
  }
1547
1654
  }
1548
1655
  catch (err) {
@@ -1582,10 +1689,13 @@ export class QvacAssistant extends ReadyResource {
1582
1689
  }
1583
1690
  this._yieldDownloads();
1584
1691
  const busyChatId = this._modelHolder(runId, chatId, model);
1585
- const executing = await transcript.status(busyChatId
1692
+ const runStatus = busyChatId
1586
1693
  ? { state: 'executing', progress: { phase: 'queued', busyChatId } }
1587
- : { state: 'executing' });
1588
- const runStatusId = executing.id;
1694
+ : { state: 'executing' };
1695
+ const runStatusId = (await (held.status ?? transcript.status(runStatus))).id;
1696
+ if (held.status) {
1697
+ await transcript.progress(runStatusId, runStatus);
1698
+ }
1589
1699
  const approvals = this._createApprove(engine, {
1590
1700
  chatId,
1591
1701
  runId,
@@ -1609,6 +1719,8 @@ export class QvacAssistant extends ReadyResource {
1609
1719
  let titleOutcome = null;
1610
1720
  let runFailure = null;
1611
1721
  let runVerdict = 'completed';
1722
+ let sentRun = null;
1723
+ let inputLength = 0;
1612
1724
  const writeText = async (type, raw) => {
1613
1725
  const text = type === 'content' ? sourceTagFilter.push(raw) : raw;
1614
1726
  if (!text) {
@@ -1734,7 +1846,10 @@ export class QvacAssistant extends ReadyResource {
1734
1846
  if (await this._isHandled(engine, request)) {
1735
1847
  return;
1736
1848
  }
1737
- const cacheKey = diagramRepair ? undefined : chatRepairKvCacheKey(chatId, acceptedRepairs);
1849
+ const cacheKey = diagramRepair || stale.runIds.size > 0
1850
+ ? undefined
1851
+ : chatRepairKvCacheKey(chatId, acceptedRepairs);
1852
+ sentRun = trackSentRun(cacheKey);
1738
1853
  const unloading = lender ? undefined : this._unloading.get(model);
1739
1854
  if (unloading) {
1740
1855
  await raceAbort(unloading.catch(noop), controller.signal);
@@ -1745,11 +1860,16 @@ export class QvacAssistant extends ReadyResource {
1745
1860
  cacheKey,
1746
1861
  turns: messages.length
1747
1862
  });
1863
+ const input = nameSenders(messages);
1864
+ inputLength = input.length;
1865
+ const inspecting = await this._capturingInferenceCalls();
1748
1866
  const events = this._harnessFor(runAgent).completion({
1749
1867
  model,
1750
1868
  agent: runAgent,
1751
- messages: nameSenders(messages),
1869
+ messages: input,
1870
+ typedText,
1752
1871
  cacheKey,
1872
+ session: chatKvCacheKey(chatId),
1753
1873
  thinkingDefault: thinkingDefault(config),
1754
1874
  toolConfigs,
1755
1875
  grantedDirs,
@@ -1795,7 +1915,7 @@ export class QvacAssistant extends ReadyResource {
1795
1915
  },
1796
1916
  progress: (update) => modelWatch.image(update),
1797
1917
  generationParams: diagramRepair ? { reasoning_budget: 0 } : undefined,
1798
- captureInferenceCalls: await this._capturingInferenceCalls(),
1918
+ captureInferenceCalls: true,
1799
1919
  signal: controller.signal
1800
1920
  });
1801
1921
  let harnessEvents = 0;
@@ -1809,26 +1929,25 @@ export class QvacAssistant extends ReadyResource {
1809
1929
  if (event.type === 'metrics') {
1810
1930
  const { type, ...rest } = event;
1811
1931
  metrics = rest;
1932
+ recordBackends(this._backendUse, rest.backends);
1812
1933
  this._log?.info('runs', 'run metrics', { runId, ...runMetricsSummary(rest) });
1813
1934
  continue;
1814
1935
  }
1815
1936
  if (event.type === 'inference-sent') {
1816
- void this._inferenceCalls
1817
- .recordSent(chatId, {
1937
+ this._inspectSent(inspecting, chatId, {
1818
1938
  runId,
1819
1939
  round: Number(event.round ?? 0),
1820
1940
  messages: event.messages,
1821
1941
  tokens: event.tokens,
1822
1942
  outcome: event.outcome,
1823
1943
  reason: event.reason
1824
- })
1825
- .catch((err) => this._log?.warn('assistant', 'sent slice not recorded', { err }));
1944
+ });
1826
1945
  continue;
1827
1946
  }
1828
1947
  if (event.type === 'inference-call') {
1829
- void this._inferenceCalls
1830
- .record(chatId, { runId, at: Date.now(), call: asRecord(event.call) })
1831
- .catch((err) => this._log?.warn('assistant', 'inference call not recorded', { err }));
1948
+ const call = asRecord(event.call);
1949
+ sentRun?.observe(call);
1950
+ this._inspectCall(inspecting, chatId, { runId, at: Date.now(), call });
1832
1951
  continue;
1833
1952
  }
1834
1953
  await flushTranscript();
@@ -1879,6 +1998,14 @@ export class QvacAssistant extends ReadyResource {
1879
1998
  const citedTitles = injected.ragInjected
1880
1999
  ? citedByPhraseOverlap(answerText, retrievedRecords)
1881
2000
  : null;
2001
+ if (sentRun?.used) {
2002
+ await this._recordSentRun(chatId, {
2003
+ runId,
2004
+ turns: sentRun.turns(inputLength),
2005
+ holder
2006
+ });
2007
+ sentRun = null;
2008
+ }
1882
2009
  await transcript.status({
1883
2010
  state: 'completed',
1884
2011
  metrics: citedTitles ? { ...metrics, citedTitles } : metrics
@@ -1905,6 +2032,13 @@ export class QvacAssistant extends ReadyResource {
1905
2032
  titleOutcome = 'ended';
1906
2033
  }
1907
2034
  finally {
2035
+ if (sentRun?.used) {
2036
+ await this._recordSentRun(chatId, {
2037
+ runId,
2038
+ turns: null,
2039
+ holder
2040
+ });
2041
+ }
1908
2042
  void this._inferenceCalls
1909
2043
  .recordOutcome(chatId, {
1910
2044
  runId,
@@ -2494,7 +2628,9 @@ export class QvacAssistant extends ReadyResource {
2494
2628
  return { compacted: false, reason: 'already-compacted' };
2495
2629
  }
2496
2630
  const inRange = chunks.filter((chunk) => chunk.seq <= through.seq);
2497
- const folded = historyFromChunks(inRange, staleToolRunIds(inRange, priorAsks ?? [], ''), senders);
2631
+ const folded = historyFromChunks(inRange, staleToolRunIds(inRange, priorAsks ?? [], ''), {
2632
+ senderNames: senders
2633
+ });
2498
2634
  if (!announced) {
2499
2635
  await options.onFold?.();
2500
2636
  announced = true;
@@ -2524,7 +2660,7 @@ export class QvacAssistant extends ReadyResource {
2524
2660
  compaction: { summary, throughId: boundary.throughId },
2525
2661
  originDeviceId: deviceId
2526
2662
  });
2527
- await this._deleteChatKvCache(chatId, 'compaction');
2663
+ await this._deleteChatKvCache(chatId, 'compaction', this._holderOf(agent));
2528
2664
  this._log?.info('runs', 'chat compacted', {
2529
2665
  chatId,
2530
2666
  foldedTurns: boundary.foldedTurns,
@@ -2977,6 +3113,9 @@ export class QvacAssistant extends ReadyResource {
2977
3113
  if (state.status === 'loading') {
2978
3114
  apply({ phase: 'loading', model: state.model });
2979
3115
  }
3116
+ else if (state.status === 'held') {
3117
+ apply({ phase: 'awaiting-download', model: state.model });
3118
+ }
2980
3119
  else if (state.status === 'downloading') {
2981
3120
  const percentage = Math.floor((state.percentage ?? 0) / 10) * 10;
2982
3121
  apply({ phase: 'downloading', model: state.model, percentage });
@@ -5,3 +5,9 @@ export declare const HOMEBREW_BIN_DIRS: ReadonlyArray<string>;
5
5
  export declare const SAFE_BIN_NAME: RegExp;
6
6
  export declare const MODEL_IDLE_UNLOAD_KEY = "model.idleUnloadMinutes";
7
7
  export declare const MODEL_IDLE_UNLOAD_MINUTES: ReadonlyArray<number>;
8
+ export declare const MODEL_MEMORY_CAP_KEY = "model.memoryCapPercent";
9
+ export declare const MODEL_MEMORY_CAP_DEFAULT = 85;
10
+ export declare const MODEL_MEMORY_CAP_MIN = 10;
11
+ export declare const MODEL_MEMORY_CAP_MAX = 100;
12
+ export declare const FAKE_MODELS_KEY = "debug.fakeModels";
13
+ export declare const QUACK_MODEL = "quack";
@@ -7,3 +7,9 @@ export const MODEL_IDLE_UNLOAD_KEY = 'model.idleUnloadMinutes';
7
7
  export const MODEL_IDLE_UNLOAD_MINUTES = [
8
8
  15, 30, 60, 120, 240, 480, 720, 1440
9
9
  ];
10
+ export const MODEL_MEMORY_CAP_KEY = 'model.memoryCapPercent';
11
+ export const MODEL_MEMORY_CAP_DEFAULT = 85;
12
+ export const MODEL_MEMORY_CAP_MIN = 10;
13
+ export const MODEL_MEMORY_CAP_MAX = 100;
14
+ export const FAKE_MODELS_KEY = 'debug.fakeModels';
15
+ export const QUACK_MODEL = 'quack';
@@ -1,6 +1,7 @@
1
1
  import type { AssistantAgentConfig, AssistantAgentUpdateConfig } from '../../spec/hyperschema/types.d.ts';
2
2
  import type { QvacAgent } from '../../spec/mesh/hyperschema/types.d.ts';
3
3
  import type { Mesh } from './mesh.ts';
4
+ export declare function allTools(): Pick<QvacAgent, 'toolsScope' | 'enabledTools'>;
4
5
  export declare function listAgents(store: Mesh, providerDeviceId?: Buffer): Promise<QvacAgent[]>;
5
6
  export declare function getDefaultAgent(store: Mesh, providerDeviceId?: Buffer): Promise<QvacAgent | null>;
6
7
  export declare function getAgent(store: Mesh, id: string): Promise<QvacAgent | null>;
@@ -10,4 +11,5 @@ export declare function mustGetAgent(store: Mesh, id: string): Promise<QvacAgent
10
11
  export declare function mustGetActiveAgent(store: Mesh, id: string): Promise<QvacAgent>;
11
12
  export declare function createAgent(store: Mesh, input: AssistantAgentConfig): Promise<QvacAgent>;
12
13
  export declare function updateAgent(store: Mesh, agentId: string, input: AssistantAgentUpdateConfig): Promise<QvacAgent>;
14
+ export declare function rehomeAgents(store: Mesh, from: Buffer, to: Buffer): Promise<void>;
13
15
  export declare function deleteAgent(store: Mesh, deviceId: Buffer | null, id: string): Promise<QvacAgent>;
@@ -4,6 +4,9 @@ import { softDeleteToolConfigsForAgent } from './tool-configs.js';
4
4
  import b4a from 'b4a';
5
5
  import { ACTIVE_AGENTS, AGENTS, AGENTS_BY_PROVIDER, DEFAULT_AGENTS, DEVICES } from './collections.js';
6
6
  const DEFAULT_ICON_NAME = 'artificial-intelligence';
7
+ export function allTools() {
8
+ return { toolsScope: 'all', enabledTools: [] };
9
+ }
7
10
  function mergeSkills(base, enabled) {
8
11
  return Array.from(new Set([...(base ?? []), ...(enabled ?? [])]));
9
12
  }
@@ -89,7 +92,6 @@ export async function mustGetActiveAgent(store, id) {
89
92
  export async function createAgent(store, input) {
90
93
  const now = Date.now();
91
94
  const enabledSkills = input.enabledSkills ?? [];
92
- const enabledTools = input.enabledTools ?? [];
93
95
  const installedBase = input.installedSkills ?? enabledSkills;
94
96
  const agent = {
95
97
  id: randomId(),
@@ -99,7 +101,7 @@ export async function createAgent(store, input) {
99
101
  modelName: input.modelName,
100
102
  reasoning: input.reasoning ?? 'inherit',
101
103
  enabledSkills,
102
- enabledTools,
104
+ ...allTools(),
103
105
  installedSkills: mergeSkills(installedBase, enabledSkills),
104
106
  isDefault: input.isDefault ?? false,
105
107
  createdAt: now,
@@ -110,7 +112,6 @@ export async function createAgent(store, input) {
110
112
  kvCacheV: input.kvCacheV ?? undefined,
111
113
  iconName: input.iconName ?? DEFAULT_ICON_NAME,
112
114
  ctxSize: input.ctxSize || undefined,
113
- toolsScope: input.toolsScope ?? 'selected',
114
115
  skillsScope: input.skillsScope ?? 'selected',
115
116
  imageSteps: input.imageSteps || undefined,
116
117
  imageWidth: input.imageWidth || undefined,
@@ -166,7 +167,6 @@ export async function updateAgent(store, agentId, input) {
166
167
  modelName: patch(input.modelName, current.modelName),
167
168
  reasoning: patch(input.reasoning, current.reasoning),
168
169
  enabledSkills,
169
- enabledTools: patch(input.enabledTools, current.enabledTools),
170
170
  installedSkills: mergeSkills(installedBase, enabledSkills),
171
171
  isDefault: input.clearIsDefault ? false : input.isDefault || current.isDefault,
172
172
  imageModelChoice: patch(input.imageModelChoice, imageBase.imageModelChoice),
@@ -174,7 +174,7 @@ export async function updateAgent(store, agentId, input) {
174
174
  kvCacheV: patch(input.kvCacheV, current.kvCacheV),
175
175
  iconName: patch(input.iconName, current.iconName),
176
176
  ctxSize: input.clearCtxSize ? undefined : input.ctxSize || current.ctxSize || undefined,
177
- toolsScope: patch(input.toolsScope, current.toolsScope),
177
+ ...allTools(),
178
178
  skillsScope: patch(input.skillsScope, current.skillsScope),
179
179
  imageSteps: input.imageSteps || imageBase.imageSteps || undefined,
180
180
  imageWidth: input.imageWidth || imageBase.imageWidth || undefined,
@@ -189,6 +189,17 @@ export async function updateAgent(store, agentId, input) {
189
189
  await store.dispatch('put-agent', { agent: updated });
190
190
  return updated;
191
191
  }
192
+ export async function rehomeAgents(store, from, to) {
193
+ const hasDefault = (await getDefaultAgent(store, to)) !== null;
194
+ for (const agent of await listAgents(store, from)) {
195
+ if (!agent.deletedAt) {
196
+ await updateAgent(store, agent.id, {
197
+ providerDeviceId: to,
198
+ clearIsDefault: hasDefault && agent.isDefault
199
+ });
200
+ }
201
+ }
202
+ }
192
203
  export async function deleteAgent(store, deviceId, id) {
193
204
  const current = await mustGetAgent(store, id);
194
205
  if (current.isDefault) {
@@ -1,3 +1,7 @@
1
+ export declare const BLIND_PEERING_OFF_KEY = "debug.blindPeeringOff";
2
+ export interface StartBlindPeeringOpts {
3
+ readonly suspended?: boolean;
4
+ }
1
5
  export interface BlindPeeringOpts {
2
6
  readonly batchIdleWait?: number;
3
7
  readonly batchMaxWait?: number;
@@ -1,4 +1,5 @@
1
1
  import { decode } from 'z32';
2
+ export const BLIND_PEERING_OFF_KEY = 'debug.blindPeeringOff';
2
3
  export const BLIND_RELAY_KEYS = [
3
4
  'tgbo7m8fh3hwqqxj95zrezorjgodtjr19b9dqruyauwnbafsmx8y',
4
5
  'tfo1aqsc4hc4ok6a4akpxifkp65efoo9n13g1386zoa7umjipqpo',
@@ -1,5 +1,5 @@
1
1
  import type { Mesh } from './mesh.ts';
2
- export type ChatActivityState = 'queued' | 'running' | 'generating-image' | 'generating-audio' | 'transcribing-audio' | 'awaiting-approval';
2
+ export type ChatActivityState = 'queued' | 'running' | 'generating-image' | 'generating-audio' | 'transcribing-audio' | 'awaiting-approval' | 'awaiting-download';
3
3
  export interface ChatActivityRow {
4
4
  readonly id: string;
5
5
  readonly activityState: ChatActivityState;
@@ -152,6 +152,9 @@ function activityState(runs) {
152
152
  if (started.length === 0) {
153
153
  return 'queued';
154
154
  }
155
+ if (latestRun(started).progress === 'awaiting-download') {
156
+ return 'awaiting-download';
157
+ }
155
158
  return mediaState(latestRun(started)) ?? 'running';
156
159
  }
157
160
  function mediaState(run) {
@@ -14,6 +14,10 @@ export declare function copyChunks(snap: HyperDB, chatId: string, { from, into }
14
14
  export declare function copyMovedChunks(snap: HyperDB, chatId: string, copy: ChatMoveCopy): Promise<number>;
15
15
  export declare function copyMovedKnowledge(snap: HyperDB, chatId: string, copy: ChatMoveCopy): Promise<void>;
16
16
  export declare function movedBlob({ from, into, references }: ChatMoveCopy, { blobId }: BlobHolder): Promise<string>;
17
+ export declare function removedReference({ mimeType }: TypedFile): string;
18
+ interface TypedFile {
19
+ readonly mimeType: string;
20
+ }
17
21
  interface BlobHolder {
18
22
  readonly blobId: string;
19
23
  }
@@ -83,7 +83,7 @@ export async function movedBlob({ from, into, references }, { blobId }) {
83
83
  }
84
84
  return moved;
85
85
  }
86
- function removedReference({ mimeType }) {
86
+ export function removedReference({ mimeType }) {
87
87
  return inlineReference(b4a.alloc(0), mimeType);
88
88
  }
89
89
  async function heldInTarget(into, { collection, id, chatId }) {