@qvac/core 0.6.7 → 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 (116) hide show
  1. package/CHANGELOG.md +145 -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 +15 -2
  14. package/dist-lib/lib/assistant.js +175 -41
  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/identity.d.ts +1 -1
  33. package/dist-lib/lib/core/identity.js +6 -4
  34. package/dist-lib/lib/core/invite.d.ts +2 -0
  35. package/dist-lib/lib/core/invite.js +6 -2
  36. package/dist-lib/lib/core/membership.d.ts +1 -12
  37. package/dist-lib/lib/core/membership.js +1 -94
  38. package/dist-lib/lib/core/mesh.d.ts +8 -13
  39. package/dist-lib/lib/core/mesh.js +41 -87
  40. package/dist-lib/lib/core/sync/index.js +2 -16
  41. package/dist-lib/lib/core.d.ts +30 -49
  42. package/dist-lib/lib/core.js +341 -868
  43. package/dist-lib/lib/diagnostics.d.ts +7 -0
  44. package/dist-lib/lib/diagnostics.js +22 -0
  45. package/dist-lib/lib/doctor/checks.d.ts +15 -0
  46. package/dist-lib/lib/doctor/checks.js +269 -0
  47. package/dist-lib/lib/doctor/redact.d.ts +4 -0
  48. package/dist-lib/lib/doctor/redact.js +31 -0
  49. package/dist-lib/lib/doctor/render.d.ts +8 -0
  50. package/dist-lib/lib/doctor/render.js +83 -0
  51. package/dist-lib/lib/doctor/tree.d.ts +5 -0
  52. package/dist-lib/lib/doctor/tree.js +167 -0
  53. package/dist-lib/lib/downloads-allowed-pusher.d.ts +10 -0
  54. package/dist-lib/lib/downloads-allowed-pusher.js +37 -0
  55. package/dist-lib/lib/guards.d.ts +1 -0
  56. package/dist-lib/lib/guards.js +4 -0
  57. package/dist-lib/lib/inference-calls.js +1 -5
  58. package/dist-lib/lib/kv-cache-reclaim.d.ts +2 -1
  59. package/dist-lib/lib/kv-cache-reclaim.js +24 -0
  60. package/dist-lib/lib/mesh-scoped-engine.js +1 -0
  61. package/dist-lib/lib/model-download-status.d.ts +1 -1
  62. package/dist-lib/lib/model-download.d.ts +9 -1
  63. package/dist-lib/lib/model-download.js +48 -3
  64. package/dist-lib/lib/model-memory-cap.d.ts +29 -0
  65. package/dist-lib/lib/model-memory-cap.js +69 -0
  66. package/dist-lib/lib/models-watch.d.ts +1 -1
  67. package/dist-lib/lib/models-watch.js +4 -4
  68. package/dist-lib/lib/oauth/flow.js +1 -1
  69. package/dist-lib/lib/oauth/loopback.js +16 -2
  70. package/dist-lib/lib/oauth/redirect-types.d.ts +1 -0
  71. package/dist-lib/lib/run-replay.d.ts +26 -0
  72. package/dist-lib/lib/run-replay.js +107 -0
  73. package/dist-lib/schema/config.d.ts +1 -1
  74. package/dist-lib/schema/config.js +1 -1
  75. package/dist-lib/schema/content.js +9 -1
  76. package/dist-lib/schema/engine.js +30 -5
  77. package/dist-lib/schema/local.js +3 -2
  78. package/dist-lib/schema/qvac.js +18 -1
  79. package/dist-lib/spec/bind.js +3 -0
  80. package/dist-lib/spec/calls.js +1 -0
  81. package/dist-lib/spec/capabilities.d.ts +2 -0
  82. package/dist-lib/spec/config/hyperconf/index.js +1 -1
  83. package/dist-lib/spec/hrpc/hrpc.json +13 -0
  84. package/dist-lib/spec/hrpc/index.js +15 -3
  85. package/dist-lib/spec/hrpc/messages.js +646 -557
  86. package/dist-lib/spec/hyperschema/index.js +646 -557
  87. package/dist-lib/spec/hyperschema/schema.json +80 -0
  88. package/dist-lib/spec/hyperschema/types.d.ts +20 -3
  89. package/dist-lib/spec/local/hyperdb/messages.js +22 -10
  90. package/dist-lib/spec/local/hyperschema/index.js +16 -7
  91. package/dist-lib/spec/local/hyperschema/schema.json +10 -1
  92. package/dist-lib/spec/local/hyperschema/types.d.ts +3 -2
  93. package/dist-lib/spec/mesh/hyperdb/db.json +16 -1
  94. package/dist-lib/spec/mesh/hyperdb/index.js +71 -2
  95. package/dist-lib/spec/mesh/hyperdb/messages.js +477 -240
  96. package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +13 -1
  97. package/dist-lib/spec/mesh/hyperdispatch/index.js +35 -1
  98. package/dist-lib/spec/mesh/hyperdispatch/messages.js +180 -7
  99. package/dist-lib/spec/mesh/hyperschema/index.js +180 -7
  100. package/dist-lib/spec/mesh/hyperschema/schema.json +98 -1
  101. package/dist-lib/spec/mesh/hyperschema/types.d.ts +26 -2
  102. package/package.json +8 -16
  103. package/tui/README.md +7 -1
  104. package/tui/app.mjs +245 -11
  105. package/tui/boot.mjs +1 -0
  106. package/tui/index.mjs +1 -0
  107. package/tui/lib/render.mjs +25 -1
  108. package/worker/assistant-sidecar.mjs +5 -2
  109. package/dist-lib/lib/core/device-invites.d.ts +0 -11
  110. package/dist-lib/lib/core/device-invites.js +0 -63
  111. package/dist-lib/lib/core/grant-expiry.d.ts +0 -11
  112. package/dist-lib/lib/core/grant-expiry.js +0 -66
  113. package/dist-lib/lib/core/mesh-seed-handoff.d.ts +0 -27
  114. package/dist-lib/lib/core/mesh-seed-handoff.js +0 -53
  115. package/dist-lib/lib/oauth/loopback.stub.d.ts +0 -3
  116. package/dist-lib/lib/oauth/loopback.stub.js +0 -6
@@ -22,6 +22,7 @@ export interface AssistantOptions {
22
22
  logger?: Logger | null;
23
23
  imageCacheDir?: string;
24
24
  inferenceCallsDir?: string;
25
+ runReplayDir?: string;
25
26
  imageCacheMaxBytes?: number;
26
27
  imageCacheMaxAgeMs?: number;
27
28
  downloadRetryMs?: number;
@@ -74,6 +75,7 @@ export declare class QvacAssistant extends ReadyResource {
74
75
  private _harnessDiagnostics;
75
76
  private _harnessDeath;
76
77
  private _refreshingDiagnostics;
78
+ private readonly _backendUse;
77
79
  private _meshList;
78
80
  private _accountFrames;
79
81
  private _accountMeshId;
@@ -84,6 +86,7 @@ export declare class QvacAssistant extends ReadyResource {
84
86
  private readonly _runs;
85
87
  private readonly _seen;
86
88
  private readonly _inferenceCalls;
89
+ private readonly _runReplay;
87
90
  private readonly _lifecycle;
88
91
  private readonly _audioTranscripts;
89
92
  private readonly _pdfPages;
@@ -94,7 +97,7 @@ export declare class QvacAssistant extends ReadyResource {
94
97
  private readonly _modelCatalog;
95
98
  private readonly _onHarnessStatus;
96
99
  private readonly _onModelCatalogEvent;
97
- constructor(engine: Capabilities, harness: Harness, { logger, imageCacheDir, inferenceCallsDir, imageCacheMaxBytes, imageCacheMaxAgeMs, downloadRetryMs, platform, preloadCurrentAgentModel, drives, borrowHarness, blobReadTimeoutMs, pdfRasterizer: pdfRasterizerOption }?: AssistantOptions);
100
+ constructor(engine: Capabilities, harness: Harness, { logger, imageCacheDir, inferenceCallsDir, runReplayDir, imageCacheMaxBytes, imageCacheMaxAgeMs, downloadRetryMs, platform, preloadCurrentAgentModel, drives, borrowHarness, blobReadTimeoutMs, pdfRasterizer: pdfRasterizerOption }?: AssistantOptions);
98
101
  private _requireDeviceId;
99
102
  _open(): Promise<void>;
100
103
  private _triggerModelWarm;
@@ -122,6 +125,7 @@ export declare class QvacAssistant extends ReadyResource {
122
125
  private _ensureCapability;
123
126
  private _reconcileDeviceProfile;
124
127
  _close(): Promise<void>;
128
+ get mobileClass(): boolean;
125
129
  get suspended(): boolean;
126
130
  presuspend(): Promise<void>;
127
131
  suspend(): Promise<void>;
@@ -139,6 +143,7 @@ export declare class QvacAssistant extends ReadyResource {
139
143
  private _meshModelUsers;
140
144
  private _runnableHost;
141
145
  notifyModelCatalog(): void;
146
+ cachedModelsChanged(): void;
142
147
  retryModelDownload(name: string): Promise<void>;
143
148
  skills(agentId?: string | null, modelName?: string | null, options?: SkillListOptions): Promise<ReadonlyArray<Skill>>;
144
149
  private _recordSkillReadiness;
@@ -152,7 +157,13 @@ export declare class QvacAssistant extends ReadyResource {
152
157
  private _reconcileCompaction;
153
158
  private _senderNames;
154
159
  private _reconcileAttribution;
155
- deleteChatKvCache(chatId: string): Promise<AssistantKvCacheDeletionResponse>;
160
+ deleteChatKvCache(chatId: string, holder?: string): Promise<AssistantKvCacheDeletionResponse>;
161
+ clearKvCache(): Promise<AssistantKvCacheDeletionResponse>;
162
+ private _clearPreReplayCaches;
163
+ private _inspectCall;
164
+ private _inspectSent;
165
+ private _sentRuns;
166
+ private _recordSentRun;
156
167
  private _deleteChatKvCache;
157
168
  private _watchMeshList;
158
169
  private _watchAccount;
@@ -165,6 +176,8 @@ export declare class QvacAssistant extends ReadyResource {
165
176
  private _pruneSeen;
166
177
  private _lenderFor;
167
178
  private _harnessFor;
179
+ private _holderOf;
180
+ private _holderHarness;
168
181
  private _dispatch;
169
182
  private _citeWebSources;
170
183
  private _searchKnowledge;
@@ -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;
1117
+ }
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) {
1150
+ try {
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
+ }
1089
1158
  }
1090
- async _deleteChatKvCache(chatId, reason) {
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) {
1091
1172
  try {
1092
- const result = await this.deleteChatKvCache(chatId);
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,11 @@ 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);
1596
+ await this._reconcileAttribution(chatId, senders, holder);
1503
1597
  }
1504
- 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 });
1505
1601
  if (currentRepair) {
1506
1602
  messages.push({ role: 'user', content: currentRepair.promptText });
1507
1603
  }
@@ -1540,9 +1636,17 @@ export class QvacAssistant extends ReadyResource {
1540
1636
  const settledWatch = this._watchSettled(engine, { chatId, runId, controller });
1541
1637
  const ownershipWatch = this._watchOwnership(engine, agent.id, controller);
1542
1638
  let readinessError = null;
1639
+ const held = { status: null };
1543
1640
  try {
1544
1641
  if (!lender) {
1545
- 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
+ });
1546
1650
  }
1547
1651
  }
1548
1652
  catch (err) {
@@ -1582,10 +1686,13 @@ export class QvacAssistant extends ReadyResource {
1582
1686
  }
1583
1687
  this._yieldDownloads();
1584
1688
  const busyChatId = this._modelHolder(runId, chatId, model);
1585
- const executing = await transcript.status(busyChatId
1689
+ const runStatus = busyChatId
1586
1690
  ? { state: 'executing', progress: { phase: 'queued', busyChatId } }
1587
- : { state: 'executing' });
1588
- 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
+ }
1589
1696
  const approvals = this._createApprove(engine, {
1590
1697
  chatId,
1591
1698
  runId,
@@ -1609,6 +1716,8 @@ export class QvacAssistant extends ReadyResource {
1609
1716
  let titleOutcome = null;
1610
1717
  let runFailure = null;
1611
1718
  let runVerdict = 'completed';
1719
+ let sentRun = null;
1720
+ let inputLength = 0;
1612
1721
  const writeText = async (type, raw) => {
1613
1722
  const text = type === 'content' ? sourceTagFilter.push(raw) : raw;
1614
1723
  if (!text) {
@@ -1734,7 +1843,10 @@ export class QvacAssistant extends ReadyResource {
1734
1843
  if (await this._isHandled(engine, request)) {
1735
1844
  return;
1736
1845
  }
1737
- 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);
1738
1850
  const unloading = lender ? undefined : this._unloading.get(model);
1739
1851
  if (unloading) {
1740
1852
  await raceAbort(unloading.catch(noop), controller.signal);
@@ -1745,10 +1857,13 @@ export class QvacAssistant extends ReadyResource {
1745
1857
  cacheKey,
1746
1858
  turns: messages.length
1747
1859
  });
1860
+ const input = nameSenders(messages);
1861
+ inputLength = input.length;
1862
+ const inspecting = await this._capturingInferenceCalls();
1748
1863
  const events = this._harnessFor(runAgent).completion({
1749
1864
  model,
1750
1865
  agent: runAgent,
1751
- messages: nameSenders(messages),
1866
+ messages: input,
1752
1867
  cacheKey,
1753
1868
  thinkingDefault: thinkingDefault(config),
1754
1869
  toolConfigs,
@@ -1795,7 +1910,7 @@ export class QvacAssistant extends ReadyResource {
1795
1910
  },
1796
1911
  progress: (update) => modelWatch.image(update),
1797
1912
  generationParams: diagramRepair ? { reasoning_budget: 0 } : undefined,
1798
- captureInferenceCalls: await this._capturingInferenceCalls(),
1913
+ captureInferenceCalls: true,
1799
1914
  signal: controller.signal
1800
1915
  });
1801
1916
  let harnessEvents = 0;
@@ -1809,26 +1924,25 @@ export class QvacAssistant extends ReadyResource {
1809
1924
  if (event.type === 'metrics') {
1810
1925
  const { type, ...rest } = event;
1811
1926
  metrics = rest;
1927
+ recordBackends(this._backendUse, rest.backends);
1812
1928
  this._log?.info('runs', 'run metrics', { runId, ...runMetricsSummary(rest) });
1813
1929
  continue;
1814
1930
  }
1815
1931
  if (event.type === 'inference-sent') {
1816
- void this._inferenceCalls
1817
- .recordSent(chatId, {
1932
+ this._inspectSent(inspecting, chatId, {
1818
1933
  runId,
1819
1934
  round: Number(event.round ?? 0),
1820
1935
  messages: event.messages,
1821
1936
  tokens: event.tokens,
1822
1937
  outcome: event.outcome,
1823
1938
  reason: event.reason
1824
- })
1825
- .catch((err) => this._log?.warn('assistant', 'sent slice not recorded', { err }));
1939
+ });
1826
1940
  continue;
1827
1941
  }
1828
1942
  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 }));
1943
+ const call = asRecord(event.call);
1944
+ sentRun?.observe(call);
1945
+ this._inspectCall(inspecting, chatId, { runId, at: Date.now(), call });
1832
1946
  continue;
1833
1947
  }
1834
1948
  await flushTranscript();
@@ -1879,6 +1993,14 @@ export class QvacAssistant extends ReadyResource {
1879
1993
  const citedTitles = injected.ragInjected
1880
1994
  ? citedByPhraseOverlap(answerText, retrievedRecords)
1881
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
+ }
1882
2004
  await transcript.status({
1883
2005
  state: 'completed',
1884
2006
  metrics: citedTitles ? { ...metrics, citedTitles } : metrics
@@ -1905,6 +2027,13 @@ export class QvacAssistant extends ReadyResource {
1905
2027
  titleOutcome = 'ended';
1906
2028
  }
1907
2029
  finally {
2030
+ if (sentRun?.used) {
2031
+ await this._recordSentRun(chatId, {
2032
+ runId,
2033
+ turns: null,
2034
+ holder
2035
+ });
2036
+ }
1908
2037
  void this._inferenceCalls
1909
2038
  .recordOutcome(chatId, {
1910
2039
  runId,
@@ -2494,7 +2623,9 @@ export class QvacAssistant extends ReadyResource {
2494
2623
  return { compacted: false, reason: 'already-compacted' };
2495
2624
  }
2496
2625
  const inRange = chunks.filter((chunk) => chunk.seq <= through.seq);
2497
- const folded = historyFromChunks(inRange, staleToolRunIds(inRange, priorAsks ?? [], ''), senders);
2626
+ const folded = historyFromChunks(inRange, staleToolRunIds(inRange, priorAsks ?? [], ''), {
2627
+ senderNames: senders
2628
+ });
2498
2629
  if (!announced) {
2499
2630
  await options.onFold?.();
2500
2631
  announced = true;
@@ -2524,7 +2655,7 @@ export class QvacAssistant extends ReadyResource {
2524
2655
  compaction: { summary, throughId: boundary.throughId },
2525
2656
  originDeviceId: deviceId
2526
2657
  });
2527
- await this._deleteChatKvCache(chatId, 'compaction');
2658
+ await this._deleteChatKvCache(chatId, 'compaction', this._holderOf(agent));
2528
2659
  this._log?.info('runs', 'chat compacted', {
2529
2660
  chatId,
2530
2661
  foldedTurns: boundary.foldedTurns,
@@ -2977,6 +3108,9 @@ export class QvacAssistant extends ReadyResource {
2977
3108
  if (state.status === 'loading') {
2978
3109
  apply({ phase: 'loading', model: state.model });
2979
3110
  }
3111
+ else if (state.status === 'held') {
3112
+ apply({ phase: 'awaiting-download', model: state.model });
3113
+ }
2980
3114
  else if (state.status === 'downloading') {
2981
3115
  const percentage = Math.floor((state.percentage ?? 0) / 10) * 10;
2982
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
  }