@qvac/core 0.6.6 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (123) hide show
  1. package/CHANGELOG.md +185 -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/assistant/fake-models.d.ts +22 -0
  6. package/dist-lib/lib/assistant/fake-models.js +112 -0
  7. package/dist-lib/lib/assistant/harness.d.ts +10 -0
  8. package/dist-lib/lib/assistant/harness.js +26 -0
  9. package/dist-lib/lib/assistant/history.d.ts +5 -1
  10. package/dist-lib/lib/assistant/history.js +14 -2
  11. package/dist-lib/lib/assistant-tree.d.ts +4 -0
  12. package/dist-lib/lib/assistant-tree.js +45 -7
  13. package/dist-lib/lib/assistant.d.ts +16 -2
  14. package/dist-lib/lib/assistant.js +184 -44
  15. package/dist-lib/lib/constants.d.ts +6 -0
  16. package/dist-lib/lib/constants.js +6 -0
  17. package/dist-lib/lib/core/chat-activity.d.ts +1 -1
  18. package/dist-lib/lib/core/chat-activity.js +3 -0
  19. package/dist-lib/lib/core/collections.d.ts +1 -0
  20. package/dist-lib/lib/core/collections.js +1 -0
  21. package/dist-lib/lib/core/contract/compute.d.ts +1 -1
  22. package/dist-lib/lib/core/contract/compute.js +2 -1
  23. package/dist-lib/lib/core/contract/index.js +6 -1
  24. package/dist-lib/lib/core/contract/invites.d.ts +1 -11
  25. package/dist-lib/lib/core/contract/invites.js +3 -3
  26. package/dist-lib/lib/core/contract/mesh-seeds.d.ts +2 -0
  27. package/dist-lib/lib/core/contract/mesh-seeds.js +26 -0
  28. package/dist-lib/lib/core/contract/open-join.d.ts +2 -0
  29. package/dist-lib/lib/core/contract/open-join.js +57 -0
  30. package/dist-lib/lib/core/departures.d.ts +3 -0
  31. package/dist-lib/lib/core/departures.js +44 -1
  32. package/dist-lib/lib/core/fs-drives.js +9 -6
  33. package/dist-lib/lib/core/handoff-channel.d.ts +1 -0
  34. package/dist-lib/lib/core/handoff-channel.js +7 -2
  35. package/dist-lib/lib/core/harness-loan.d.ts +1 -0
  36. package/dist-lib/lib/core/harness-loan.js +7 -2
  37. package/dist-lib/lib/core/identity.d.ts +1 -1
  38. package/dist-lib/lib/core/identity.js +6 -4
  39. package/dist-lib/lib/core/invite.d.ts +2 -0
  40. package/dist-lib/lib/core/invite.js +6 -2
  41. package/dist-lib/lib/core/membership.d.ts +1 -12
  42. package/dist-lib/lib/core/membership.js +1 -94
  43. package/dist-lib/lib/core/mesh.d.ts +8 -13
  44. package/dist-lib/lib/core/mesh.js +41 -87
  45. package/dist-lib/lib/core/protocol-pairing.d.ts +11 -0
  46. package/dist-lib/lib/core/protocol-pairing.js +22 -0
  47. package/dist-lib/lib/core/sync/index.js +2 -16
  48. package/dist-lib/lib/core.d.ts +43 -49
  49. package/dist-lib/lib/core.js +451 -909
  50. package/dist-lib/lib/diagnostics.d.ts +7 -0
  51. package/dist-lib/lib/diagnostics.js +22 -0
  52. package/dist-lib/lib/doctor/checks.d.ts +15 -0
  53. package/dist-lib/lib/doctor/checks.js +269 -0
  54. package/dist-lib/lib/doctor/redact.d.ts +4 -0
  55. package/dist-lib/lib/doctor/redact.js +31 -0
  56. package/dist-lib/lib/doctor/render.d.ts +8 -0
  57. package/dist-lib/lib/doctor/render.js +83 -0
  58. package/dist-lib/lib/doctor/tree.d.ts +5 -0
  59. package/dist-lib/lib/doctor/tree.js +167 -0
  60. package/dist-lib/lib/downloads-allowed-pusher.d.ts +10 -0
  61. package/dist-lib/lib/downloads-allowed-pusher.js +37 -0
  62. package/dist-lib/lib/guards.d.ts +1 -0
  63. package/dist-lib/lib/guards.js +4 -0
  64. package/dist-lib/lib/inference-calls.js +1 -5
  65. package/dist-lib/lib/kv-cache-reclaim.d.ts +2 -1
  66. package/dist-lib/lib/kv-cache-reclaim.js +24 -0
  67. package/dist-lib/lib/mesh-scoped-engine.js +1 -0
  68. package/dist-lib/lib/model-download-status.d.ts +1 -1
  69. package/dist-lib/lib/model-download.d.ts +9 -1
  70. package/dist-lib/lib/model-download.js +48 -3
  71. package/dist-lib/lib/model-memory-cap.d.ts +29 -0
  72. package/dist-lib/lib/model-memory-cap.js +69 -0
  73. package/dist-lib/lib/models-watch.d.ts +1 -1
  74. package/dist-lib/lib/models-watch.js +4 -4
  75. package/dist-lib/lib/oauth/flow.js +1 -1
  76. package/dist-lib/lib/oauth/loopback.js +16 -2
  77. package/dist-lib/lib/oauth/redirect-types.d.ts +1 -0
  78. package/dist-lib/lib/run-replay.d.ts +26 -0
  79. package/dist-lib/lib/run-replay.js +107 -0
  80. package/dist-lib/schema/config.d.ts +1 -1
  81. package/dist-lib/schema/config.js +1 -1
  82. package/dist-lib/schema/content.js +9 -1
  83. package/dist-lib/schema/engine.js +30 -5
  84. package/dist-lib/schema/local.js +3 -2
  85. package/dist-lib/schema/qvac.js +18 -1
  86. package/dist-lib/spec/bind.js +3 -0
  87. package/dist-lib/spec/calls.js +1 -0
  88. package/dist-lib/spec/capabilities.d.ts +2 -0
  89. package/dist-lib/spec/config/hyperconf/index.js +1 -1
  90. package/dist-lib/spec/hrpc/hrpc.json +13 -0
  91. package/dist-lib/spec/hrpc/index.js +15 -3
  92. package/dist-lib/spec/hrpc/messages.js +646 -557
  93. package/dist-lib/spec/hyperschema/index.js +646 -557
  94. package/dist-lib/spec/hyperschema/schema.json +80 -0
  95. package/dist-lib/spec/hyperschema/types.d.ts +20 -3
  96. package/dist-lib/spec/local/hyperdb/messages.js +22 -10
  97. package/dist-lib/spec/local/hyperschema/index.js +16 -7
  98. package/dist-lib/spec/local/hyperschema/schema.json +10 -1
  99. package/dist-lib/spec/local/hyperschema/types.d.ts +3 -2
  100. package/dist-lib/spec/mesh/hyperdb/db.json +16 -1
  101. package/dist-lib/spec/mesh/hyperdb/index.js +71 -2
  102. package/dist-lib/spec/mesh/hyperdb/messages.js +477 -240
  103. package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +13 -1
  104. package/dist-lib/spec/mesh/hyperdispatch/index.js +35 -1
  105. package/dist-lib/spec/mesh/hyperdispatch/messages.js +180 -7
  106. package/dist-lib/spec/mesh/hyperschema/index.js +180 -7
  107. package/dist-lib/spec/mesh/hyperschema/schema.json +98 -1
  108. package/dist-lib/spec/mesh/hyperschema/types.d.ts +26 -2
  109. package/package.json +9 -17
  110. package/tui/README.md +7 -1
  111. package/tui/app.mjs +245 -11
  112. package/tui/boot.mjs +1 -0
  113. package/tui/index.mjs +1 -0
  114. package/tui/lib/render.mjs +25 -1
  115. package/worker/assistant-sidecar.mjs +5 -2
  116. package/dist-lib/lib/core/device-invites.d.ts +0 -11
  117. package/dist-lib/lib/core/device-invites.js +0 -63
  118. package/dist-lib/lib/core/grant-expiry.d.ts +0 -11
  119. package/dist-lib/lib/core/grant-expiry.js +0 -66
  120. package/dist-lib/lib/core/mesh-seed-handoff.d.ts +0 -27
  121. package/dist-lib/lib/core/mesh-seed-handoff.js +0 -53
  122. package/dist-lib/lib/oauth/loopback.stub.d.ts +0 -3
  123. 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';
@@ -158,6 +159,7 @@ export class QvacAssistant extends ReadyResource {
158
159
  _lastCapabilities = new Map();
159
160
  _sharedMeshes = new Set();
160
161
  _hostedIn = new Map();
162
+ _accountHosted = [];
161
163
  _readySkills;
162
164
  _recordingReadiness = false;
163
165
  _readinessDirty = false;
@@ -170,6 +172,7 @@ export class QvacAssistant extends ReadyResource {
170
172
  _harnessDiagnostics;
171
173
  _harnessDeath;
172
174
  _refreshingDiagnostics;
175
+ _backendUse = new Map();
173
176
  _meshList;
174
177
  _accountFrames;
175
178
  _accountMeshId;
@@ -180,6 +183,7 @@ export class QvacAssistant extends ReadyResource {
180
183
  _runs;
181
184
  _seen;
182
185
  _inferenceCalls;
186
+ _runReplay;
183
187
  _lifecycle;
184
188
  _audioTranscripts;
185
189
  _pdfPages;
@@ -190,7 +194,7 @@ export class QvacAssistant extends ReadyResource {
190
194
  _modelCatalog = new ModelCatalogRevisionHub();
191
195
  _onHarnessStatus;
192
196
  _onModelCatalogEvent;
193
- 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 } = {}) {
194
198
  super();
195
199
  this._engine = engine;
196
200
  this._harness = harness;
@@ -226,6 +230,7 @@ export class QvacAssistant extends ReadyResource {
226
230
  this._runs = new Map();
227
231
  this._seen = new Map();
228
232
  this._inferenceCalls = new InferenceCallStore(inferenceCallsDir);
233
+ this._runReplay = new RunReplayStore(runReplayDir);
229
234
  this._lifecycle = new AbortController();
230
235
  this._audioTranscripts = new Map();
231
236
  this._pdfPages = new Map();
@@ -255,6 +260,14 @@ export class QvacAssistant extends ReadyResource {
255
260
  if (progress) {
256
261
  this._downloads?.observeImageProgress(progress);
257
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
+ }
258
271
  };
259
272
  }
260
273
  _requireDeviceId() {
@@ -272,6 +285,7 @@ export class QvacAssistant extends ReadyResource {
272
285
  this._harness.on?.('status', this._onHarnessStatus);
273
286
  this._harness.on?.('model', this._onModelCatalogEvent);
274
287
  this._info = await this._describeHarness();
288
+ await this._clearPreReplayCaches();
275
289
  await this._clearPersistedPauses();
276
290
  this._downloads = new ModelDownloadReconciler({
277
291
  engine: this._engine,
@@ -499,7 +513,8 @@ export class QvacAssistant extends ReadyResource {
499
513
  assistant: summarizeRuns(runs, this.suspended),
500
514
  platform: this._platform,
501
515
  mobileClass: this._mobileClass,
502
- memoryBudget: this._capability?.memoryBudget ?? null
516
+ memoryBudget: this._capability?.memoryBudget ?? null,
517
+ backendUse: Object.fromEntries([...this._backendUse].map(([tool, uses]) => [tool, [...uses]]))
503
518
  };
504
519
  }
505
520
  async _refreshHarnessDiagnostics() {
@@ -582,17 +597,22 @@ export class QvacAssistant extends ReadyResource {
582
597
  }
583
598
  _followHosted(meshId, agents) {
584
599
  const deviceId = this._deviceId;
585
- if (!deviceId || !this._sharedMeshes.has(meshId)) {
600
+ const account = meshId === this._accountMeshId;
601
+ if (!deviceId || (!account && !this._sharedMeshes.has(meshId))) {
586
602
  return;
587
603
  }
588
604
  const hosted = agents
589
605
  .filter((agent) => !!agent.providerDeviceId && b4a.equals(agent.providerDeviceId, deviceId))
590
606
  .map((agent) => agent.id)
591
607
  .sort();
592
- if (sameNameList(this._hostedIn.get(meshId) ?? [], hosted)) {
608
+ const known = account ? this._accountHosted : (this._hostedIn.get(meshId) ?? []);
609
+ if (sameNameList(known, hosted)) {
593
610
  return;
594
611
  }
595
- if (hosted.length > 0) {
612
+ if (account) {
613
+ this._accountHosted = hosted;
614
+ }
615
+ else if (hosted.length > 0) {
596
616
  this._hostedIn.set(meshId, hosted);
597
617
  }
598
618
  else {
@@ -687,6 +707,9 @@ export class QvacAssistant extends ReadyResource {
687
707
  this._lifecycle.abort(RUNNER_CLOSED);
688
708
  await Promise.allSettled([...this._runs.values()].map((run) => run.done));
689
709
  }
710
+ get mobileClass() {
711
+ return this._mobileClass;
712
+ }
690
713
  get suspended() {
691
714
  return this._state === QvacState.SUSPEND;
692
715
  }
@@ -870,6 +893,10 @@ export class QvacAssistant extends ReadyResource {
870
893
  notifyModelCatalog() {
871
894
  this._modelCatalog.notify();
872
895
  }
896
+ cachedModelsChanged() {
897
+ this._modelCatalog.notify();
898
+ this._reportCapabilities().catch(noop);
899
+ }
873
900
  async retryModelDownload(name) {
874
901
  if (!this._downloads) {
875
902
  throw new Error('model download reconciler is unavailable');
@@ -992,7 +1019,9 @@ export class QvacAssistant extends ReadyResource {
992
1019
  }
993
1020
  this._reconciledCompactions.delete(chatId);
994
1021
  this._chatAttribution.delete(chatId);
995
- 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 })));
996
1025
  void this._inferenceCalls
997
1026
  .forget(chatId)
998
1027
  .catch((err) => this._log?.warn('assistant', 'inspector records not dropped', { err }));
@@ -1008,26 +1037,26 @@ export class QvacAssistant extends ReadyResource {
1008
1037
  for (const agent of agents) {
1009
1038
  const previous = known?.get(agent.id);
1010
1039
  if (previous !== undefined && previous !== next.get(agent.id)) {
1011
- void this._reclaimAgentChatCaches(meshId, agent.id);
1040
+ void this._reclaimAgentChatCaches(meshId, agent);
1012
1041
  }
1013
1042
  }
1014
1043
  known = next;
1015
1044
  }
1016
1045
  }
1017
- async _reclaimAgentChatCaches(meshId, agentId) {
1046
+ async _reclaimAgentChatCaches(meshId, agent) {
1018
1047
  try {
1019
1048
  const { chats } = await this._engine.listChats({ meshId });
1020
1049
  for (const chat of chats ?? []) {
1021
- if (chat.agentId === agentId) {
1022
- 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));
1023
1052
  }
1024
1053
  }
1025
1054
  }
1026
1055
  catch (err) {
1027
- 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 });
1028
1057
  }
1029
1058
  }
1030
- async _reconcileCompaction(engine, chatId) {
1059
+ async _reconcileCompaction(engine, chatId, holder) {
1031
1060
  const deviceId = this._requireDeviceId();
1032
1061
  const { chunks } = await engine
1033
1062
  .listChunks({ chatId, type: 'compaction' })
@@ -1044,7 +1073,7 @@ export class QvacAssistant extends ReadyResource {
1044
1073
  if (this._reconciledCompactions.get(chatId) === newest.id) {
1045
1074
  return;
1046
1075
  }
1047
- const gone = await this._deleteChatKvCache(chatId, 'compaction from another device');
1076
+ const gone = await this._deleteChatKvCache(chatId, 'compaction from another device', holder);
1048
1077
  if (gone) {
1049
1078
  this._reconciledCompactions.set(chatId, newest.id);
1050
1079
  }
@@ -1060,7 +1089,7 @@ export class QvacAssistant extends ReadyResource {
1060
1089
  ]);
1061
1090
  return senderNamesFrom(chunks, members ?? [], devices ?? []);
1062
1091
  }
1063
- async _reconcileAttribution(chatId, senders) {
1092
+ async _reconcileAttribution(chatId, senders, holder) {
1064
1093
  const attribution = senderAttribution(senders);
1065
1094
  const seen = this._chatAttribution.get(chatId);
1066
1095
  if (seen === attribution) {
@@ -1070,20 +1099,78 @@ export class QvacAssistant extends ReadyResource {
1070
1099
  this._chatAttribution.set(chatId, attribution);
1071
1100
  return;
1072
1101
  }
1073
- const gone = await this._deleteChatKvCache(chatId, 'sender attribution changed');
1102
+ const gone = await this._deleteChatKvCache(chatId, 'sender attribution changed', holder);
1074
1103
  if (gone) {
1075
1104
  this._chatAttribution.set(chatId, attribution);
1076
1105
  }
1077
1106
  }
1078
- async deleteChatKvCache(chatId) {
1079
- 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))) ?? {
1080
1110
  status: 'unsupported',
1081
1111
  error: 'Harness does not support KV cache deletion'
1082
- });
1112
+ };
1113
+ if (result.status === 'success') {
1114
+ await this._runReplay.forget(chatId, holder);
1115
+ }
1116
+ return result;
1083
1117
  }
1084
- 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) {
1085
1150
  try {
1086
- 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);
1087
1174
  if (result.status === 'failure') {
1088
1175
  this._log?.warn('assistant', 'chat kv-cache deletion failed', {
1089
1176
  chatId,
@@ -1224,7 +1311,13 @@ export class QvacAssistant extends ReadyResource {
1224
1311
  return own && id === b4a.toHex(own) ? null : id;
1225
1312
  }
1226
1313
  _harnessFor(agent) {
1227
- 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;
1228
1321
  const borrow = this._borrowHarness;
1229
1322
  if (!id || !borrow) {
1230
1323
  return this._harness;
@@ -1426,9 +1519,10 @@ export class QvacAssistant extends ReadyResource {
1426
1519
  runId
1427
1520
  });
1428
1521
  }
1429
- await this._reconcileCompaction(engine, chatId);
1522
+ const holder = this._holderOf(agent);
1523
+ await this._reconcileCompaction(engine, chatId, holder);
1430
1524
  if (request.parentId) {
1431
- await this._deleteChatKvCache(chatId, 'superseding run');
1525
+ await this._deleteChatKvCache(chatId, 'superseding run', holder);
1432
1526
  }
1433
1527
  const lender = this._lenderFor(agent);
1434
1528
  const model = lender
@@ -1471,6 +1565,12 @@ export class QvacAssistant extends ReadyResource {
1471
1565
  const toolConfigsPromise = this._loadToolConfigs(engine, agent.id);
1472
1566
  const fsGrantsPromise = this._loadFsGrants(agent.id);
1473
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
+ }
1474
1574
  const branches = historyBranches(all, runId);
1475
1575
  const currentRepair = branches.repairByRunId.get(runId);
1476
1576
  const repairParent = currentRepair
@@ -1493,9 +1593,11 @@ export class QvacAssistant extends ReadyResource {
1493
1593
  const named = couldNameSenders(history);
1494
1594
  const senders = named ? await this._senderNames(engine, history) : NO_SENDERS;
1495
1595
  if (named || this._chatAttribution.get(chatId)) {
1496
- await this._reconcileAttribution(chatId, senders);
1596
+ await this._reconcileAttribution(chatId, senders, holder);
1497
1597
  }
1498
- const messages = historyFromChunks(history, staleToolRunIds(history, priorAsks ?? [], runId), senders);
1598
+ const stale = staleToolRunIds(history, priorAsks ?? [], runId);
1599
+ const sentRuns = await this._sentRuns(chatId, holder);
1600
+ const messages = historyFromChunks(history, stale, { senderNames: senders, sentRuns });
1499
1601
  if (currentRepair) {
1500
1602
  messages.push({ role: 'user', content: currentRepair.promptText });
1501
1603
  }
@@ -1534,9 +1636,17 @@ export class QvacAssistant extends ReadyResource {
1534
1636
  const settledWatch = this._watchSettled(engine, { chatId, runId, controller });
1535
1637
  const ownershipWatch = this._watchOwnership(engine, agent.id, controller);
1536
1638
  let readinessError = null;
1639
+ const held = { status: null };
1537
1640
  try {
1538
1641
  if (!lender) {
1539
- await this._downloads?.waitUntilCached(model, controller.signal);
1642
+ await this._downloads?.waitUntilCached(model, controller.signal, {
1643
+ onHold: () => {
1644
+ held.status = transcript.status({
1645
+ state: 'executing',
1646
+ progress: { phase: 'awaiting-download', model }
1647
+ });
1648
+ }
1649
+ });
1540
1650
  }
1541
1651
  }
1542
1652
  catch (err) {
@@ -1576,10 +1686,13 @@ export class QvacAssistant extends ReadyResource {
1576
1686
  }
1577
1687
  this._yieldDownloads();
1578
1688
  const busyChatId = this._modelHolder(runId, chatId, model);
1579
- const executing = await transcript.status(busyChatId
1689
+ const runStatus = busyChatId
1580
1690
  ? { state: 'executing', progress: { phase: 'queued', busyChatId } }
1581
- : { state: 'executing' });
1582
- const runStatusId = executing.id;
1691
+ : { state: 'executing' };
1692
+ const runStatusId = (await (held.status ?? transcript.status(runStatus))).id;
1693
+ if (held.status) {
1694
+ await transcript.progress(runStatusId, runStatus);
1695
+ }
1583
1696
  const approvals = this._createApprove(engine, {
1584
1697
  chatId,
1585
1698
  runId,
@@ -1603,6 +1716,8 @@ export class QvacAssistant extends ReadyResource {
1603
1716
  let titleOutcome = null;
1604
1717
  let runFailure = null;
1605
1718
  let runVerdict = 'completed';
1719
+ let sentRun = null;
1720
+ let inputLength = 0;
1606
1721
  const writeText = async (type, raw) => {
1607
1722
  const text = type === 'content' ? sourceTagFilter.push(raw) : raw;
1608
1723
  if (!text) {
@@ -1728,7 +1843,10 @@ export class QvacAssistant extends ReadyResource {
1728
1843
  if (await this._isHandled(engine, request)) {
1729
1844
  return;
1730
1845
  }
1731
- const cacheKey = diagramRepair ? undefined : chatRepairKvCacheKey(chatId, acceptedRepairs);
1846
+ const cacheKey = diagramRepair || stale.runIds.size > 0
1847
+ ? undefined
1848
+ : chatRepairKvCacheKey(chatId, acceptedRepairs);
1849
+ sentRun = trackSentRun(cacheKey);
1732
1850
  const unloading = lender ? undefined : this._unloading.get(model);
1733
1851
  if (unloading) {
1734
1852
  await raceAbort(unloading.catch(noop), controller.signal);
@@ -1739,10 +1857,13 @@ export class QvacAssistant extends ReadyResource {
1739
1857
  cacheKey,
1740
1858
  turns: messages.length
1741
1859
  });
1860
+ const input = nameSenders(messages);
1861
+ inputLength = input.length;
1862
+ const inspecting = await this._capturingInferenceCalls();
1742
1863
  const events = this._harnessFor(runAgent).completion({
1743
1864
  model,
1744
1865
  agent: runAgent,
1745
- messages: nameSenders(messages),
1866
+ messages: input,
1746
1867
  cacheKey,
1747
1868
  thinkingDefault: thinkingDefault(config),
1748
1869
  toolConfigs,
@@ -1789,7 +1910,7 @@ export class QvacAssistant extends ReadyResource {
1789
1910
  },
1790
1911
  progress: (update) => modelWatch.image(update),
1791
1912
  generationParams: diagramRepair ? { reasoning_budget: 0 } : undefined,
1792
- captureInferenceCalls: await this._capturingInferenceCalls(),
1913
+ captureInferenceCalls: true,
1793
1914
  signal: controller.signal
1794
1915
  });
1795
1916
  let harnessEvents = 0;
@@ -1803,26 +1924,25 @@ export class QvacAssistant extends ReadyResource {
1803
1924
  if (event.type === 'metrics') {
1804
1925
  const { type, ...rest } = event;
1805
1926
  metrics = rest;
1927
+ recordBackends(this._backendUse, rest.backends);
1806
1928
  this._log?.info('runs', 'run metrics', { runId, ...runMetricsSummary(rest) });
1807
1929
  continue;
1808
1930
  }
1809
1931
  if (event.type === 'inference-sent') {
1810
- void this._inferenceCalls
1811
- .recordSent(chatId, {
1932
+ this._inspectSent(inspecting, chatId, {
1812
1933
  runId,
1813
1934
  round: Number(event.round ?? 0),
1814
1935
  messages: event.messages,
1815
1936
  tokens: event.tokens,
1816
1937
  outcome: event.outcome,
1817
1938
  reason: event.reason
1818
- })
1819
- .catch((err) => this._log?.warn('assistant', 'sent slice not recorded', { err }));
1939
+ });
1820
1940
  continue;
1821
1941
  }
1822
1942
  if (event.type === 'inference-call') {
1823
- void this._inferenceCalls
1824
- .record(chatId, { runId, at: Date.now(), call: asRecord(event.call) })
1825
- .catch((err) => this._log?.warn('assistant', 'inference call not recorded', { err }));
1943
+ const call = asRecord(event.call);
1944
+ sentRun?.observe(call);
1945
+ this._inspectCall(inspecting, chatId, { runId, at: Date.now(), call });
1826
1946
  continue;
1827
1947
  }
1828
1948
  await flushTranscript();
@@ -1873,6 +1993,14 @@ export class QvacAssistant extends ReadyResource {
1873
1993
  const citedTitles = injected.ragInjected
1874
1994
  ? citedByPhraseOverlap(answerText, retrievedRecords)
1875
1995
  : null;
1996
+ if (sentRun?.used) {
1997
+ await this._recordSentRun(chatId, {
1998
+ runId,
1999
+ turns: sentRun.turns(inputLength),
2000
+ holder
2001
+ });
2002
+ sentRun = null;
2003
+ }
1876
2004
  await transcript.status({
1877
2005
  state: 'completed',
1878
2006
  metrics: citedTitles ? { ...metrics, citedTitles } : metrics
@@ -1899,6 +2027,13 @@ export class QvacAssistant extends ReadyResource {
1899
2027
  titleOutcome = 'ended';
1900
2028
  }
1901
2029
  finally {
2030
+ if (sentRun?.used) {
2031
+ await this._recordSentRun(chatId, {
2032
+ runId,
2033
+ turns: null,
2034
+ holder
2035
+ });
2036
+ }
1902
2037
  void this._inferenceCalls
1903
2038
  .recordOutcome(chatId, {
1904
2039
  runId,
@@ -2488,7 +2623,9 @@ export class QvacAssistant extends ReadyResource {
2488
2623
  return { compacted: false, reason: 'already-compacted' };
2489
2624
  }
2490
2625
  const inRange = chunks.filter((chunk) => chunk.seq <= through.seq);
2491
- const folded = historyFromChunks(inRange, staleToolRunIds(inRange, priorAsks ?? [], ''), senders);
2626
+ const folded = historyFromChunks(inRange, staleToolRunIds(inRange, priorAsks ?? [], ''), {
2627
+ senderNames: senders
2628
+ });
2492
2629
  if (!announced) {
2493
2630
  await options.onFold?.();
2494
2631
  announced = true;
@@ -2518,7 +2655,7 @@ export class QvacAssistant extends ReadyResource {
2518
2655
  compaction: { summary, throughId: boundary.throughId },
2519
2656
  originDeviceId: deviceId
2520
2657
  });
2521
- await this._deleteChatKvCache(chatId, 'compaction');
2658
+ await this._deleteChatKvCache(chatId, 'compaction', this._holderOf(agent));
2522
2659
  this._log?.info('runs', 'chat compacted', {
2523
2660
  chatId,
2524
2661
  foldedTurns: boundary.foldedTurns,
@@ -2971,6 +3108,9 @@ export class QvacAssistant extends ReadyResource {
2971
3108
  if (state.status === 'loading') {
2972
3109
  apply({ phase: 'loading', model: state.model });
2973
3110
  }
3111
+ else if (state.status === 'held') {
3112
+ apply({ phase: 'awaiting-download', model: state.model });
3113
+ }
2974
3114
  else if (state.status === 'downloading') {
2975
3115
  const percentage = Math.floor((state.percentage ?? 0) / 10) * 10;
2976
3116
  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,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) {
@@ -70,3 +70,4 @@ export declare const MESH_INVITE_GRANTS = "@qvac/mesh-invite-grants";
70
70
  export declare const MESH_FOUNDERS = "@qvac/mesh-founders";
71
71
  export declare const MESH_KINDS = "@qvac/mesh-kinds";
72
72
  export declare const MESH_NAMES = "@qvac/mesh-names";
73
+ export declare const MESH_SEEDS = "@qvac/mesh-seeds";
@@ -70,3 +70,4 @@ export const MESH_INVITE_GRANTS = '@qvac/mesh-invite-grants';
70
70
  export const MESH_FOUNDERS = '@qvac/mesh-founders';
71
71
  export const MESH_KINDS = '@qvac/mesh-kinds';
72
72
  export const MESH_NAMES = '@qvac/mesh-names';
73
+ export const MESH_SEEDS = '@qvac/mesh-seeds';
@@ -1,5 +1,5 @@
1
1
  import type { Router } from '../../../spec/mesh/hyperdispatch/index.js';
2
- declare const DROPPED: readonly ['@qvac/append-chunk', '@qvac/update-chunk', '@qvac/resolve-approval', '@qvac/put-agent', '@qvac/put-approval-rule', '@qvac/seed-defaults', '@qvac/add-knowledge', '@qvac/remove-knowledge', '@qvac/add-knowledge-artifact', '@qvac/claim-indexing', '@qvac/resolve-indexing', '@qvac/put-chat', '@qvac/put-tool-configs', '@qvac/append-tool-activity', '@qvac/migrate-agent-catalog', '@qvac/reindex-agents', '@qvac/put-chat-group', '@qvac/put-joined-mesh', '@qvac/purge-chat', '@qvac/name-mesh'];
2
+ declare const DROPPED: readonly ['@qvac/append-chunk', '@qvac/update-chunk', '@qvac/resolve-approval', '@qvac/put-agent', '@qvac/put-approval-rule', '@qvac/seed-defaults', '@qvac/add-knowledge', '@qvac/remove-knowledge', '@qvac/add-knowledge-artifact', '@qvac/claim-indexing', '@qvac/resolve-indexing', '@qvac/put-chat', '@qvac/put-tool-configs', '@qvac/append-tool-activity', '@qvac/migrate-agent-catalog', '@qvac/reindex-agents', '@qvac/put-chat-group', '@qvac/put-joined-mesh', '@qvac/purge-chat', '@qvac/name-mesh', '@qvac/put-mesh-seed'];
3
3
  export type DroppedComputeRoute = (typeof DROPPED)[number];
4
4
  export declare function droppedComputeRoutes(): ReadonlyArray<DroppedComputeRoute>;
5
5
  export declare class ComputeRouteDropped extends Error {
@@ -18,7 +18,8 @@ const DROPPED = [
18
18
  '@qvac/put-chat-group',
19
19
  '@qvac/put-joined-mesh',
20
20
  '@qvac/purge-chat',
21
- '@qvac/name-mesh'
21
+ '@qvac/name-mesh',
22
+ '@qvac/put-mesh-seed'
22
23
  ];
23
24
  const DROPPED_ROUTES = new Set(DROPPED);
24
25
  export function droppedComputeRoutes() {
@@ -13,6 +13,8 @@ import { registerJoinedMeshRoutes } from './joined-meshes.js';
13
13
  import { registerMemberRoutes } from './members.js';
14
14
  import { registerMeshKindRoutes } from './mesh-kind.js';
15
15
  import { registerMeshNameRoutes } from './mesh-name.js';
16
+ import { registerMeshSeedRoutes } from './mesh-seeds.js';
17
+ import { registerOpenJoinRoutes } from './open-join.js';
16
18
  import { registerSeedDefaultsRoutes } from './seed-defaults.js';
17
19
  import { registerToolActivityRoutes } from './tool-activity.js';
18
20
  import { registerToolConfigsRoutes } from './tool-configs.js';
@@ -22,7 +24,8 @@ const OPTIMISTIC_ROUTES = new Set([
22
24
  '@qvac/claim-invite',
23
25
  '@qvac/recover-device',
24
26
  '@qvac/attest-device',
25
- '@qvac/join-member'
27
+ '@qvac/join-member',
28
+ '@qvac/open-join'
26
29
  ]);
27
30
  function writersOnly(router) {
28
31
  return {
@@ -56,6 +59,8 @@ export function registerRoutes(target, { compute = false } = {}) {
56
59
  registerInviteRoutes(router, { compute });
57
60
  registerMeshKindRoutes(router, { compute });
58
61
  registerMeshNameRoutes(router);
62
+ registerOpenJoinRoutes(router);
63
+ registerMeshSeedRoutes(router);
59
64
  if (compute) {
60
65
  registerComputeDrops(router);
61
66
  }
@@ -1,17 +1,7 @@
1
1
  import type { Router } from '../../../spec/mesh/hyperdispatch/index.js';
2
2
  import type HyperDB from 'hyperdb';
3
- import type { QvacDevice, QvacMeshInviteGrant } from '../../../spec/mesh/hyperschema/types.d.ts';
4
- export declare function memberStanding(tx: HyperDB, memberId: Buffer, grant: QvacMeshInviteGrant): Promise<'present' | 'removed' | null>;
5
- interface InviteStandingOptions {
6
- readonly device: QvacDevice | null;
7
- readonly memberId: Buffer;
8
- readonly grant: QvacMeshInviteGrant;
9
- readonly compute?: boolean;
10
- }
11
- export declare function inviteStanding(tx: HyperDB, { device, memberId, grant, compute }: InviteStandingOptions): Promise<'present' | 'removed' | null>;
12
- export declare function belongsToAnotherMember(tx: HyperDB, device: QvacDevice | null, memberId: Buffer): Promise<boolean>;
3
+ import type { QvacDevice } from '../../../spec/mesh/hyperschema/types.d.ts';
13
4
  export declare function removedBy(tx: HyperDB, device: QvacDevice): Promise<'itself' | 'own-member' | 'another-member'>;
14
5
  export declare function registerInviteRoutes(router: Router, { compute }?: {
15
6
  compute?: boolean | undefined;
16
7
  }): void;
17
- export {};