@qvac/core 0.11.0 → 0.11.2

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 (104) hide show
  1. package/CHANGELOG.md +148 -1
  2. package/README.md +19 -16
  3. package/dist-lib/index.d.ts +1 -0
  4. package/dist-lib/index.js +1 -0
  5. package/dist-lib/lib/api/chunk-delta.d.ts +6 -2
  6. package/dist-lib/lib/api/chunk-delta.js +69 -7
  7. package/dist-lib/lib/api/chunks.js +10 -4
  8. package/dist-lib/lib/api/config.d.ts +4 -1
  9. package/dist-lib/lib/api/config.js +23 -4
  10. package/dist-lib/lib/api/devices.js +3 -1
  11. package/dist-lib/lib/api/rag.js +11 -7
  12. package/dist-lib/lib/assistant/harness.d.ts +3 -1
  13. package/dist-lib/lib/assistant/harness.js +10 -0
  14. package/dist-lib/lib/assistant/history.d.ts +1 -0
  15. package/dist-lib/lib/assistant/history.js +9 -3
  16. package/dist-lib/lib/assistant/message.js +2 -2
  17. package/dist-lib/lib/assistant-tree.d.ts +2 -0
  18. package/dist-lib/lib/assistant-tree.js +5 -3
  19. package/dist-lib/lib/assistant.d.ts +6 -1
  20. package/dist-lib/lib/assistant.js +77 -24
  21. package/dist-lib/lib/client.js +10 -1
  22. package/dist-lib/lib/compaction.d.ts +2 -1
  23. package/dist-lib/lib/compaction.js +14 -4
  24. package/dist-lib/lib/constants.d.ts +3 -0
  25. package/dist-lib/lib/constants.js +3 -0
  26. package/dist-lib/lib/core/archived-mesh.js +1 -0
  27. package/dist-lib/lib/core/chat-activity.js +12 -15
  28. package/dist-lib/lib/core/chunk.d.ts +14 -3
  29. package/dist-lib/lib/core/chunk.js +112 -22
  30. package/dist-lib/lib/core/collections.d.ts +1 -0
  31. package/dist-lib/lib/core/collections.js +1 -0
  32. package/dist-lib/lib/core/config-value.d.ts +6 -0
  33. package/dist-lib/lib/core/config-value.js +14 -0
  34. package/dist-lib/lib/core/contract/chat.d.ts +1 -0
  35. package/dist-lib/lib/core/contract/chat.js +8 -1
  36. package/dist-lib/lib/core/contract/chunks.js +20 -6
  37. package/dist-lib/lib/core/contract/compute.d.ts +1 -1
  38. package/dist-lib/lib/core/contract/compute.js +2 -1
  39. package/dist-lib/lib/core/contract/devices.js +3 -2
  40. package/dist-lib/lib/core/departures.js +1 -0
  41. package/dist-lib/lib/core/devices.d.ts +2 -1
  42. package/dist-lib/lib/core/devices.js +4 -2
  43. package/dist-lib/lib/core/files/attachments.js +3 -3
  44. package/dist-lib/lib/core/files/index.d.ts +1 -1
  45. package/dist-lib/lib/core/files/index.js +3 -2
  46. package/dist-lib/lib/core/files/processing/index.d.ts +1 -0
  47. package/dist-lib/lib/core/files/processing/index.js +8 -0
  48. package/dist-lib/lib/core/files/processing/settle.d.ts +1 -0
  49. package/dist-lib/lib/core/files/processing/settle.js +20 -13
  50. package/dist-lib/lib/core/files/rag/index.d.ts +1 -1
  51. package/dist-lib/lib/core/files/rag/index.js +1 -1
  52. package/dist-lib/lib/core/files/rag/retrieval/full-text.d.ts +1 -0
  53. package/dist-lib/lib/core/files/rag/retrieval/full-text.js +4 -1
  54. package/dist-lib/lib/core/hosted-agents.d.ts +3 -0
  55. package/dist-lib/lib/core/hosted-agents.js +19 -0
  56. package/dist-lib/lib/core/inference-calls-receiver.js +2 -2
  57. package/dist-lib/lib/core/known-sender.d.ts +3 -0
  58. package/dist-lib/lib/core/known-sender.js +26 -0
  59. package/dist-lib/lib/core/local/index.d.ts +2 -0
  60. package/dist-lib/lib/core/local/index.js +17 -10
  61. package/dist-lib/lib/core/mesh.d.ts +5 -1
  62. package/dist-lib/lib/core/mesh.js +6 -6
  63. package/dist-lib/lib/core/migrations/chat-chunks.d.ts +4 -0
  64. package/dist-lib/lib/core/migrations/chat-chunks.js +43 -0
  65. package/dist-lib/lib/core/notification.js +16 -13
  66. package/dist-lib/lib/core/skill-overlay-receiver.js +2 -2
  67. package/dist-lib/lib/core/skill-secrets.js +2 -2
  68. package/dist-lib/lib/core.d.ts +17 -1
  69. package/dist-lib/lib/core.js +55 -5
  70. package/dist-lib/lib/credential-pusher.d.ts +11 -7
  71. package/dist-lib/lib/credential-pusher.js +40 -8
  72. package/dist-lib/lib/device-tree.d.ts +18 -0
  73. package/dist-lib/lib/device-tree.js +32 -0
  74. package/dist-lib/lib/integrations/setup-routes.js +7 -5
  75. package/dist-lib/lib/kv-cache-retention.d.ts +34 -0
  76. package/dist-lib/lib/kv-cache-retention.js +98 -0
  77. package/dist-lib/schema/content.js +2 -1
  78. package/dist-lib/schema/engine.js +20 -3
  79. package/dist-lib/schema/field-order.js +8 -1
  80. package/dist-lib/schema/local.js +7 -3
  81. package/dist-lib/schema/qvac.js +23 -4
  82. package/dist-lib/spec/hrpc/messages.js +1240 -1194
  83. package/dist-lib/spec/hyperschema/index.js +1240 -1194
  84. package/dist-lib/spec/hyperschema/schema.json +49 -1
  85. package/dist-lib/spec/hyperschema/types.d.ts +12 -1
  86. package/dist-lib/spec/local/hyperdb/messages.js +70 -20
  87. package/dist-lib/spec/local/hyperschema/index.js +36 -11
  88. package/dist-lib/spec/local/hyperschema/schema.json +21 -1
  89. package/dist-lib/spec/local/hyperschema/types.d.ts +5 -1
  90. package/dist-lib/spec/mesh/hyperdb/db.json +17 -1
  91. package/dist-lib/spec/mesh/hyperdb/index.js +83 -2
  92. package/dist-lib/spec/mesh/hyperdb/messages.js +602 -264
  93. package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +7 -1
  94. package/dist-lib/spec/mesh/hyperdispatch/index.js +18 -1
  95. package/dist-lib/spec/mesh/hyperdispatch/messages.js +137 -14
  96. package/dist-lib/spec/mesh/hyperschema/index.js +137 -14
  97. package/dist-lib/spec/mesh/hyperschema/schema.json +79 -1
  98. package/dist-lib/spec/mesh/hyperschema/types.d.ts +21 -1
  99. package/package.json +13 -9
  100. package/tui/app.mjs +743 -266
  101. package/tui/boot.mjs +2 -2
  102. package/tui/lib/connect-sheet.mjs +350 -0
  103. package/tui/lib/setup.mjs +59 -25
  104. package/tui/lib/skill-connect.mjs +145 -0
@@ -10,7 +10,7 @@ import { serveHarness } from '@qvac/harness';
10
10
  import { duplexPair } from '@qvac/harness/transport';
11
11
  import { lentHarness } from './lent-harness.js';
12
12
  export function assistantTree(opts) {
13
- const { core, harness, logger = null, imageCacheDir, inferenceCallsDir, runReplayDir, imageCacheMaxBytes, imageCacheMaxAgeMs, embedder, extractors, onHarness, platform, preloadCurrentAgentModel, fakeModels = false } = opts;
13
+ const { core, harness, logger = null, imageCacheDir, inferenceCallsDir, runReplayDir, imageCacheMaxBytes, imageCacheMaxAgeMs, kvCacheRetentionMs, embedder, extractors, onHarness, platform, preloadCurrentAgentModel, fakeModels = false } = opts;
14
14
  const sup = new Supervisor();
15
15
  const logged = ['child-died', 'child-restarting', 'child-ready', 'gave-up', 'stall'];
16
16
  for (const event of logged) {
@@ -80,6 +80,7 @@ export function assistantTree(opts) {
80
80
  runReplayDir,
81
81
  imageCacheMaxBytes,
82
82
  imageCacheMaxAgeMs,
83
+ kvCacheRetentionMs,
83
84
  platform,
84
85
  preloadCurrentAgentModel,
85
86
  drives: core.fsDrives ?? undefined,
@@ -98,7 +99,7 @@ export function assistantTree(opts) {
98
99
  offModelCatalogWatch = core.registerModelCatalogWatch(() => assistant.modelCatalogWatch());
99
100
  offCustomModels = startCustomModelPusher(harness, core.local, {
100
101
  logger,
101
- onPushed: () => assistant.notifyModelCatalog()
102
+ onPushed: () => assistant.cachedModelsChanged()
102
103
  });
103
104
  offModelIdle = startModelIdlePusher(harness, core.local, logger);
104
105
  modelMemoryCap = assistant.mobileClass
@@ -243,7 +244,7 @@ export function assistantTree(opts) {
243
244
  return sup;
244
245
  }
245
246
  export function assistantChild(opts) {
246
- const { harness, core = 'core', logger = null, imageCacheDir, inferenceCallsDir, runReplayDir, imageCacheMaxBytes, imageCacheMaxAgeMs, embedder, extractors, onHarness, preloadCurrentAgentModel, fakeModels, ...spec } = opts;
247
+ const { harness, core = 'core', logger = null, imageCacheDir, inferenceCallsDir, runReplayDir, imageCacheMaxBytes, imageCacheMaxAgeMs, kvCacheRetentionMs, embedder, extractors, onHarness, preloadCurrentAgentModel, fakeModels, ...spec } = opts;
247
248
  return {
248
249
  ...spec,
249
250
  deps: [core],
@@ -257,6 +258,7 @@ export function assistantChild(opts) {
257
258
  runReplayDir,
258
259
  imageCacheMaxBytes,
259
260
  imageCacheMaxAgeMs,
261
+ kvCacheRetentionMs,
260
262
  embedder,
261
263
  extractors,
262
264
  onHarness,
@@ -25,6 +25,7 @@ export interface AssistantOptions {
25
25
  runReplayDir?: string;
26
26
  imageCacheMaxBytes?: number;
27
27
  imageCacheMaxAgeMs?: number;
28
+ kvCacheRetentionMs?: number;
28
29
  downloadRetryMs?: number;
29
30
  platform?: string;
30
31
  preloadCurrentAgentModel?: boolean;
@@ -66,6 +67,7 @@ export declare class QvacAssistant extends ReadyResource {
66
67
  private _info;
67
68
  private readonly _lastCapabilities;
68
69
  private _sharedMeshes;
70
+ private _writableMeshes;
69
71
  private readonly _hostedIn;
70
72
  private _accountHosted;
71
73
  private _readySkills;
@@ -94,6 +96,7 @@ export declare class QvacAssistant extends ReadyResource {
94
96
  private readonly _backgroundReplays;
95
97
  private readonly _inferenceCalls;
96
98
  private readonly _runReplay;
99
+ private readonly _kvRetention;
97
100
  private readonly _lifecycle;
98
101
  private readonly _audioTranscripts;
99
102
  private readonly _videoProbes;
@@ -102,10 +105,11 @@ export declare class QvacAssistant extends ReadyResource {
102
105
  private readonly _runImageCacheNames;
103
106
  private readonly _reconciledCompactions;
104
107
  private readonly _chatAttribution;
108
+ private readonly _knowledgeCaps;
105
109
  private readonly _modelCatalog;
106
110
  private readonly _onHarnessStatus;
107
111
  private readonly _onModelCatalogEvent;
108
- constructor(engine: Capabilities, harness: Harness, { logger, imageCacheDir, inferenceCallsDir, runReplayDir, imageCacheMaxBytes, imageCacheMaxAgeMs, downloadRetryMs, platform, preloadCurrentAgentModel, drives, borrowHarness, blobReadTimeoutMs, runIdleTimeoutMs, modelReloadDelayMs, pdfRasterizer: pdfRasterizerOption }?: AssistantOptions);
112
+ constructor(engine: Capabilities, harness: Harness, { logger, imageCacheDir, inferenceCallsDir, runReplayDir, imageCacheMaxBytes, imageCacheMaxAgeMs, kvCacheRetentionMs, downloadRetryMs, platform, preloadCurrentAgentModel, drives, borrowHarness, blobReadTimeoutMs, runIdleTimeoutMs, modelReloadDelayMs, pdfRasterizer: pdfRasterizerOption }?: AssistantOptions);
109
113
  private _requireDeviceId;
110
114
  _open(): Promise<void>;
111
115
  private _triggerModelWarm;
@@ -163,6 +167,7 @@ export declare class QvacAssistant extends ReadyResource {
163
167
  tools(): Promise<ReadonlyArray<ToolCatalogEntry>>;
164
168
  private _supervisedWatch;
165
169
  private _reclaimDeletedChats;
170
+ private _sweepIdleChatCaches;
166
171
  private _dropDeletedChat;
167
172
  private _followAgentEdits;
168
173
  private _reclaimAgentChatCaches;
@@ -35,7 +35,9 @@ import { fsGrantsFrom, grantedDirsFrom } from './fs-grants.js';
35
35
  import { persistentDir } from './persistent-dir.js';
36
36
  import { ModelCatalogRevisionHub } from './model-catalog-revision.js';
37
37
  import { agentRekeyFingerprint, agentSwitched, chatKvCacheKey, chatRepairKvCacheKey, disappearedChatIds } from './kv-cache-reclaim.js';
38
- import { buildCompactionPrompt, canCompact, foldReducesPrompt, COMPACTION_KEEP_TURNS, COMPACTION_TURN_PREFIX, isContextOverflowFailure, nearContextLimit, compactionBoundary, lastCompactedThroughId, parseCompactionSummary } from './compaction.js';
38
+ import { KvCacheRetention } from './kv-cache-retention.js';
39
+ import { KV_CACHE_RETENTION_KEY } from './constants.js';
40
+ import { buildCompactionPrompt, canCompact, foldReducesPrompt, COMPACTION_KEEP_TURNS, COMPACTION_TURN_PREFIX, isContextOverflowFailure, nearContextLimit, compactionBoundary, foldsThrough, lastCompactedThroughId, parseCompactionSummary } from './compaction.js';
39
41
  import { ModelDownloadReconciler, musicModelChoiceOf } from './model-download.js';
40
42
  import { AbortedError, raceAbort } from '@qvac/harness/race-abort';
41
43
  import { RunTranscript } from './run-transcript.js';
@@ -43,7 +45,7 @@ import { searchKnowledge } from './knowledge-search.js';
43
45
  import { isLifecycleTeardown, knownAbortOutcome, RUN_ABORT_REASONS, RUN_FAILURE_REASONS, runFailureNotice } from './run-notices.js';
44
46
  import { unref } from './unref.js';
45
47
  import { encodeAgentSkillReadiness } from './skill-readiness.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';
48
+ import { harnessClearKvCache, harnessDeathReason, harnessDeleteModel, harnessDied, harnessListModels, harnessPreviewModelSource, harnessSkillCatalog, harnessSkillManagement, harnessDetectLanguage, harnessSpeak, harnessSpeakStream, harnessTools, harnessTranscribe, heldLoadModel, droppedHeldLoadModel, imageDownloadProgress, isHarnessEvent, modelCatalogChanged, customModelReportEntries, toolReportEntry } from './assistant/harness.js';
47
49
  import { annotateUnreadableImages, audioTranscriptBlock, backfillDocumentOnlyTurn, backfillImageOnlyTurns, silentAudioNotice, unheardAudioNotice, nameSenders, restoreSentContent, hiddenVideoNotice, lentVideoNotice, unreadVideoNotice, unsyncedVideoNotice, VIDEO_NO_AUDIO, VIDEO_SILENT, videoAnnotation, videoTranscriptBlock } from './assistant/message.js';
48
50
  import { frameTimes, probeVideo, sampleFrames } from './core/video/frames.js';
49
51
  import { packVideoDigest, parseVideoDigestHeader, unpackVideoDigest, VIDEO_DIGEST_MIME, videoDigestFileName } from './core/video/digest.js';
@@ -233,6 +235,7 @@ export class QvacAssistant extends ReadyResource {
233
235
  _info;
234
236
  _lastCapabilities = new Map();
235
237
  _sharedMeshes = new Set();
238
+ _writableMeshes = new Set();
236
239
  _hostedIn = new Map();
237
240
  _accountHosted = [];
238
241
  _readySkills;
@@ -261,6 +264,7 @@ export class QvacAssistant extends ReadyResource {
261
264
  _backgroundReplays;
262
265
  _inferenceCalls;
263
266
  _runReplay;
267
+ _kvRetention;
264
268
  _lifecycle;
265
269
  _audioTranscripts;
266
270
  _videoProbes;
@@ -269,10 +273,11 @@ export class QvacAssistant extends ReadyResource {
269
273
  _runImageCacheNames;
270
274
  _reconciledCompactions;
271
275
  _chatAttribution;
276
+ _knowledgeCaps = new Map();
272
277
  _modelCatalog = new ModelCatalogRevisionHub();
273
278
  _onHarnessStatus;
274
279
  _onModelCatalogEvent;
275
- constructor(engine, harness, { logger = null, imageCacheDir, inferenceCallsDir, runReplayDir, imageCacheMaxBytes, imageCacheMaxAgeMs, downloadRetryMs, platform = os.platform(), preloadCurrentAgentModel = true, drives, borrowHarness, blobReadTimeoutMs = BLOB_READ_TIMEOUT_MS, runIdleTimeoutMs = RUN_IDLE_TIMEOUT_MS, modelReloadDelayMs = MODEL_RELOAD_DELAY_MS, pdfRasterizer: pdfRasterizerOption } = {}) {
280
+ constructor(engine, harness, { logger = null, imageCacheDir, inferenceCallsDir, runReplayDir, imageCacheMaxBytes, imageCacheMaxAgeMs, kvCacheRetentionMs, downloadRetryMs, platform = os.platform(), preloadCurrentAgentModel = true, drives, borrowHarness, blobReadTimeoutMs = BLOB_READ_TIMEOUT_MS, runIdleTimeoutMs = RUN_IDLE_TIMEOUT_MS, modelReloadDelayMs = MODEL_RELOAD_DELAY_MS, pdfRasterizer: pdfRasterizerOption } = {}) {
276
281
  super();
277
282
  this._engine = engine;
278
283
  this._harness = harness;
@@ -313,6 +318,13 @@ export class QvacAssistant extends ReadyResource {
313
318
  this._backgroundReplays = new Set();
314
319
  this._inferenceCalls = new InferenceCallStore(inferenceCallsDir ?? persistentDir('inspector'));
315
320
  this._runReplay = new RunReplayStore(runReplayDir ?? persistentDir('run-replay'));
321
+ this._kvRetention = new KvCacheRetention(engine, {
322
+ active: () => !this.closing && !this.suspended,
323
+ running: (chatId) => [...this._runs.values()].some((run) => run.chatId === chatId),
324
+ deleteChatKvCache: (chatId) => this._deleteChatKvCache(chatId, 'idle past retention'),
325
+ defaultMs: kvCacheRetentionMs,
326
+ logger
327
+ });
316
328
  this._lifecycle = new AbortController();
317
329
  this._audioTranscripts = new Map();
318
330
  this._videoProbes = new Map();
@@ -387,6 +399,7 @@ export class QvacAssistant extends ReadyResource {
387
399
  this._downloads.start();
388
400
  this._meshListWatch.start();
389
401
  this._accountWatch.start();
402
+ this._kvRetention.start(() => this._meshConsumers.keys());
390
403
  void this._bootOnline();
391
404
  this._triggerModelWarm();
392
405
  void this.onlineReady().then(() => this._triggerModelWarm(), noop);
@@ -624,6 +637,7 @@ export class QvacAssistant extends ReadyResource {
624
637
  }
625
638
  const installedModelNames = models.map((model) => model.name);
626
639
  const cachedModelNames = models.filter((model) => model.isCached).map((model) => model.name);
640
+ const customModels = customModelReportEntries(models);
627
641
  let toolCatalog = null;
628
642
  if (this._harness.tools) {
629
643
  try {
@@ -639,7 +653,8 @@ export class QvacAssistant extends ReadyResource {
639
653
  installedModelNames,
640
654
  cachedModelNames,
641
655
  this._readySkills,
642
- toolCatalog
656
+ toolCatalog,
657
+ customModels
643
658
  ]);
644
659
  const meshId = this._accountMeshId;
645
660
  if (!meshId) {
@@ -652,14 +667,15 @@ export class QvacAssistant extends ReadyResource {
652
667
  installedModelNames,
653
668
  cachedModelNames,
654
669
  readySkillNames: readySkillNames ?? undefined,
655
- toolCatalog: toolCatalog ?? undefined
670
+ toolCatalog: toolCatalog ?? undefined,
671
+ customModels
656
672
  };
657
673
  await this._reportInto(meshId, key, report);
658
674
  if (!readySkillNames) {
659
675
  return;
660
676
  }
661
677
  for (const [shared, hosted] of this._hostedIn) {
662
- if (shared === meshId)
678
+ if (shared === meshId || !this._writableMeshes.has(shared))
663
679
  continue;
664
680
  await this._reportInto(shared, JSON.stringify([readySkillNames, hosted]), report);
665
681
  }
@@ -776,6 +792,7 @@ export class QvacAssistant extends ReadyResource {
776
792
  }
777
793
  async _close() {
778
794
  this._stopMeshIntake();
795
+ this._kvRetention.stop();
779
796
  this._harness.off?.('status', this._onHarnessStatus);
780
797
  this._harness.off?.('model', this._onModelCatalogEvent);
781
798
  this._warmAbort.abort(RUNNER_CLOSED);
@@ -1146,6 +1163,13 @@ export class QvacAssistant extends ReadyResource {
1146
1163
  await this._sweepInferenceCalls();
1147
1164
  }
1148
1165
  }
1166
+ async _sweepIdleChatCaches(meshId, frames) {
1167
+ for await (const _frame of frames) {
1168
+ if (this.closing || this.suspended)
1169
+ return;
1170
+ void this._kvRetention.sweep(meshId);
1171
+ }
1172
+ }
1149
1173
  _dropDeletedChat(chatId) {
1150
1174
  for (const run of this._runs.values()) {
1151
1175
  if (run.chatId === chatId) {
@@ -1362,6 +1386,12 @@ export class QvacAssistant extends ReadyResource {
1362
1386
  this._hostedIn.delete(meshId);
1363
1387
  this._lastCapabilities.delete(meshId);
1364
1388
  }
1389
+ const writable = new Set(meshes.filter((mesh) => mesh.writable).map((mesh) => mesh.meshId));
1390
+ const joined = [...this._hostedIn.keys()].some((meshId) => writable.has(meshId) && !this._writableMeshes.has(meshId));
1391
+ this._writableMeshes = writable;
1392
+ if (joined) {
1393
+ void this._reportHosted().catch(noop);
1394
+ }
1365
1395
  for (const [meshId, consumer] of this._meshConsumers) {
1366
1396
  if (open.has(meshId))
1367
1397
  continue;
@@ -1396,7 +1426,8 @@ export class QvacAssistant extends ReadyResource {
1396
1426
  const watches = [
1397
1427
  this._streamWatch(`run-requests:${meshId}`, () => this._engine.runRequestsWatch({ meshId }), (frames) => this._dispatchRunRequests(meshId, frames)),
1398
1428
  this._streamWatch(`chat-list:${meshId}`, () => this._engine.chatsWatch({ meshId }), (frames) => this._reclaimDeletedChats(frames)),
1399
- this._streamWatch(`agent:${meshId}`, () => this._engine.agentsWatch({ meshId }), (frames) => this._followAgentEdits(meshId, frames))
1429
+ this._streamWatch(`agent:${meshId}`, () => this._engine.agentsWatch({ meshId }), (frames) => this._followAgentEdits(meshId, frames)),
1430
+ this._streamWatch(`kv-cache-retention:${meshId}`, () => this._engine.configWatch({ key: KV_CACHE_RETENTION_KEY }), (frames) => this._sweepIdleChatCaches(meshId, frames))
1400
1431
  ];
1401
1432
  return {
1402
1433
  start: () => watches.forEach((watch) => watch.start()),
@@ -1713,7 +1744,8 @@ export class QvacAssistant extends ReadyResource {
1713
1744
  transcript,
1714
1745
  trigger: 'before-run',
1715
1746
  signal: controller.signal,
1716
- meshId
1747
+ meshId,
1748
+ requestSeq: request.seq
1717
1749
  });
1718
1750
  const toolConfigsPromise = this._loadToolConfigs(engine, agent.id);
1719
1751
  const fsGrantsPromise = lender ? Promise.resolve(NO_FS_GRANTS) : this._loadFsGrants(agent.id);
@@ -1881,6 +1913,7 @@ export class QvacAssistant extends ReadyResource {
1881
1913
  let sentRun = null;
1882
1914
  let inputLength = 0;
1883
1915
  let asked;
1916
+ let injectedHits = 0;
1884
1917
  const writeText = async (type, raw) => {
1885
1918
  const text = type === 'content' ? sourceTagFilter.push(raw) : raw;
1886
1919
  if (!text) {
@@ -1892,17 +1925,24 @@ export class QvacAssistant extends ReadyResource {
1892
1925
  await transcript.text(type, text);
1893
1926
  };
1894
1927
  const failRun = async (message) => {
1895
- const folded = isContextOverflowFailure(message) &&
1928
+ const overflowed = isContextOverflowFailure(message);
1929
+ const folded = overflowed &&
1896
1930
  (await this._runCompaction({
1897
1931
  chatId,
1898
1932
  transcript,
1899
1933
  trigger: 'overflow',
1900
1934
  signal: controller.signal,
1901
- meshId
1935
+ meshId,
1936
+ requestSeq: request.seq
1902
1937
  }));
1938
+ const shrunk = overflowed && !folded && injectedHits > 1;
1939
+ if (shrunk) {
1940
+ this._knowledgeCaps.set(chatId, Math.floor(injectedHits / 2));
1941
+ this._log?.info('runs', 'knowledge shrunk for retry', { runId, hits: injectedHits });
1942
+ }
1903
1943
  runFailure = runFailureNotice(message);
1904
1944
  await transcript.status({ state: 'failed', reason: runFailure, metrics });
1905
- if (folded) {
1945
+ if (folded || shrunk) {
1906
1946
  await engine
1907
1947
  .runAgent({ chatId, agentId: agent.id, parentRunId: runId })
1908
1948
  .catch((err) => this._log?.warn('runs', 'compaction retry not queued', { runId, err }));
@@ -1965,9 +2005,13 @@ export class QvacAssistant extends ReadyResource {
1965
2005
  await transcript.status({ state: 'failed', reason: outcome.reason, metrics });
1966
2006
  return;
1967
2007
  }
1968
- const injected = knowledge
1969
- ? await this._injectKnowledge(knowledge, { transcript, sourceTagFilter, retrievedRecords })
2008
+ const cap = this._knowledgeCaps.get(chatId);
2009
+ this._knowledgeCaps.delete(chatId);
2010
+ const fitted = knowledge && cap ? { ...knowledge, hits: knowledge.hits.slice(0, cap) } : knowledge;
2011
+ const injected = fitted
2012
+ ? await this._injectKnowledge(fitted, { transcript, sourceTagFilter, retrievedRecords })
1970
2013
  : { documentsInjected: false, sourcesWritten: false, ragInjected: false };
2014
+ injectedHits = injected.ragInjected && fitted ? fitted.hits.length : 0;
1971
2015
  const hasAudio = messages.some((turn) => turn.audioAttachments?.length || turn.videos?.length);
1972
2016
  if (hasAudio) {
1973
2017
  modelWatch.transcribe(true);
@@ -2117,6 +2161,9 @@ export class QvacAssistant extends ReadyResource {
2117
2161
  this._log?.info('runs', 'run metrics', { runId, ...runMetricsSummary(rest) });
2118
2162
  continue;
2119
2163
  }
2164
+ if (event.type === 'inference-sent' && event.outcome === 'overflowed') {
2165
+ this._log?.info('runs', 'context overflow', { runId, reason: event.reason });
2166
+ }
2120
2167
  if (event.type === 'inference-sent') {
2121
2168
  this._inspectSent(inspecting, chatId, {
2122
2169
  runId,
@@ -2244,6 +2291,7 @@ export class QvacAssistant extends ReadyResource {
2244
2291
  if (titleOutcome && !diagramRepair) {
2245
2292
  void this._autoTitle(engine, {
2246
2293
  chatId,
2294
+ history,
2247
2295
  messages,
2248
2296
  model,
2249
2297
  agent: runAgent,
@@ -2987,7 +3035,7 @@ export class QvacAssistant extends ReadyResource {
2987
3035
  }
2988
3036
  }
2989
3037
  async _compactIfNearFull(params) {
2990
- const { chatId, transcript, trigger, signal, meshId } = params;
3038
+ const { chatId, transcript, trigger, signal, meshId, requestSeq } = params;
2991
3039
  const metrics = await this._lastRunMetrics(chatId, meshId);
2992
3040
  if (!nearContextLimit(metrics.cacheTokens, metrics.ctxSize)) {
2993
3041
  return false;
@@ -3005,11 +3053,12 @@ export class QvacAssistant extends ReadyResource {
3005
3053
  trigger,
3006
3054
  signal,
3007
3055
  occupancy: metrics,
3008
- meshId
3056
+ meshId,
3057
+ requestSeq
3009
3058
  });
3010
3059
  }
3011
3060
  async _runCompaction(params) {
3012
- const { chatId, transcript, trigger, signal, meshId } = params;
3061
+ const { chatId, transcript, trigger, signal, meshId, requestSeq } = params;
3013
3062
  let statusId = null;
3014
3063
  const started = Date.now();
3015
3064
  try {
@@ -3022,7 +3071,8 @@ export class QvacAssistant extends ReadyResource {
3022
3071
  statusId = status.id;
3023
3072
  },
3024
3073
  meshId,
3025
- signal
3074
+ signal,
3075
+ requestSeq
3026
3076
  });
3027
3077
  this._log?.info('runs', 'compaction', {
3028
3078
  chatId,
@@ -3107,12 +3157,13 @@ export class QvacAssistant extends ReadyResource {
3107
3157
  .catch(() => ({ chunks: [] }));
3108
3158
  const lastThroughId = lastCompactedThroughId(chunks);
3109
3159
  const lastThroughSeq = chunks.find((chunk) => chunk.id === lastThroughId)?.seq ?? -1;
3110
- const userTurns = chunks.filter((chunk) => chunk.userMessage).length;
3160
+ const beforeSeq = options.requestSeq ?? Infinity;
3161
+ const userTurns = chunks.filter((chunk) => chunk.userMessage && chunk.seq < beforeSeq).length;
3111
3162
  let announced = false;
3112
3163
  let keepTurns = COMPACTION_KEEP_TURNS;
3113
3164
  const senders = await this._senderNames(engine, chunks);
3114
3165
  for (;;) {
3115
- const boundary = compactionBoundary(chunks, keepTurns);
3166
+ const boundary = compactionBoundary(chunks, keepTurns, beforeSeq);
3116
3167
  if (!boundary) {
3117
3168
  return { compacted: false, reason: 'nothing-to-fold' };
3118
3169
  }
@@ -3123,7 +3174,8 @@ export class QvacAssistant extends ReadyResource {
3123
3174
  if (through.seq <= lastThroughSeq) {
3124
3175
  return { compacted: false, reason: 'already-compacted' };
3125
3176
  }
3126
- const inRange = chunks.filter((chunk) => chunk.seq <= through.seq);
3177
+ const folds = foldsThrough(chunks, through);
3178
+ const inRange = chunks.filter((chunk) => chunk.compaction || folds(chunk));
3127
3179
  const folded = historyFromChunks(inRange, staleToolRunIds(inRange, priorAsks ?? [], ''), {
3128
3180
  senderNames: senders
3129
3181
  });
@@ -3209,9 +3261,10 @@ export class QvacAssistant extends ReadyResource {
3209
3261
  }
3210
3262
  return { summary: parseCompactionSummary(text), overflowed: false };
3211
3263
  }
3212
- async _autoTitle(engine, { chatId, messages, model, agent, probe }) {
3264
+ async _autoTitle(engine, { chatId, history, messages, model, agent, probe }) {
3213
3265
  try {
3214
- const first = messages.find((turn) => turn.role === 'user' && turn.content.trim());
3266
+ const first = history.find((chunk) => chunk.userMessage?.text.trim())?.userMessage?.text ??
3267
+ messages.find((turn) => turn.role === 'user' && turn.content.trim())?.content;
3215
3268
  if (!first) {
3216
3269
  return;
3217
3270
  }
@@ -3219,7 +3272,7 @@ export class QvacAssistant extends ReadyResource {
3219
3272
  if (!isDefaultChatTitle(chat?.title)) {
3220
3273
  return;
3221
3274
  }
3222
- await this._generateChatTitle({ engine, chatId, input: first.content, model, agent, probe });
3275
+ await this._generateChatTitle({ engine, chatId, input: first, model, agent, probe });
3223
3276
  }
3224
3277
  catch (err) {
3225
3278
  this._log?.warn('runs', 'auto-title skipped', { chatId, err });
@@ -3697,7 +3750,7 @@ function toolCallChunk(event) {
3697
3750
  function toolResultChunk(event) {
3698
3751
  return {
3699
3752
  type: 'tool-result',
3700
- toolResult: { name: event.name, result: withoutDataChannel(event.result) }
3753
+ toolResult: { name: event.name, result: withoutDataChannel(event.result) ?? null }
3701
3754
  };
3702
3755
  }
3703
3756
  function runMetricsSummary(metrics) {
@@ -1,5 +1,6 @@
1
1
  import ReadyResource from 'ready-resource';
2
2
  import { reconnectingWatch } from './reconnecting-watch.js';
3
+ import { readConfigAnswer, readConfigFrames } from './api/config.js';
3
4
  import { openLocalEngineSpec } from './spec-loader.js';
4
5
  import { settleOnClose } from '@qvac/harness/rpc';
5
6
  export class Client extends ReadyResource {
@@ -28,7 +29,15 @@ export class Client extends ReadyResource {
28
29
  const modelDownloadsWatch = this._reconnecting(capabilities.modelDownloadsWatch, 'model-download');
29
30
  const modelCatalogWatch = this._reconnecting(capabilities.modelCatalogWatch, 'model-catalog');
30
31
  const modelsWatch = this._reconnecting(capabilities.modelsWatch, 'models');
31
- Object.assign(this, capabilities, { modelCatalogWatch, modelDownloadsWatch, modelsWatch });
32
+ const config = {
33
+ configGet: async (input) => readConfigAnswer(await capabilities.configGet(input)),
34
+ configWatch: (input) => readConfigFrames(capabilities.configWatch(input))
35
+ };
36
+ Object.assign(this, capabilities, config, {
37
+ modelCatalogWatch,
38
+ modelDownloadsWatch,
39
+ modelsWatch
40
+ });
32
41
  this._log?.info('lifecycle', 'client open');
33
42
  }
34
43
  _reconnecting(open, scope) {
@@ -14,7 +14,8 @@ export interface CompactionBoundary {
14
14
  throughId: string;
15
15
  foldedTurns: number;
16
16
  }
17
- export declare function compactionBoundary(chunks: ReadonlyArray<AssistantChunk>, keepTurns?: number): CompactionBoundary | null;
17
+ export declare function compactionBoundary(chunks: ReadonlyArray<AssistantChunk>, keepTurns?: number, beforeSeq?: number): CompactionBoundary | null;
18
+ export declare function foldsThrough(chunks: ReadonlyArray<AssistantChunk>, through: AssistantChunk): (chunk: AssistantChunk) => boolean;
18
19
  export declare function lastCompactedThroughId(chunks: ReadonlyArray<AssistantChunk>): string | null;
19
20
  export interface FoldedChunks {
20
21
  chunks: ReadonlyArray<AssistantChunk>;
@@ -35,10 +35,10 @@ export function parseCompactionSummary(text) {
35
35
  const summary = text.trim();
36
36
  return summary.length > 0 ? summary : null;
37
37
  }
38
- export function compactionBoundary(chunks, keepTurns = COMPACTION_KEEP_TURNS) {
38
+ export function compactionBoundary(chunks, keepTurns = COMPACTION_KEEP_TURNS, beforeSeq = Infinity) {
39
39
  const userTurns = [];
40
40
  for (let index = 0; index < chunks.length; index++) {
41
- if (chunks[index].userMessage) {
41
+ if (chunks[index].userMessage && chunks[index].seq < beforeSeq) {
42
42
  userTurns.push(index);
43
43
  }
44
44
  }
@@ -52,12 +52,21 @@ export function compactionBoundary(chunks, keepTurns = COMPACTION_KEEP_TURNS) {
52
52
  from = index;
53
53
  }
54
54
  }
55
- const boundary = chunks[from - 1];
55
+ const boundary = chunks.slice(0, from).findLast((chunk) => chunk.type === 'run-request') ?? chunks[from - 1];
56
56
  if (!boundary) {
57
57
  return null;
58
58
  }
59
59
  return { throughId: boundary.id, foldedTurns: userTurns.length - keepTurns };
60
60
  }
61
+ export function foldsThrough(chunks, through) {
62
+ const requested = new Map();
63
+ for (const chunk of chunks) {
64
+ if (chunk.type === 'run-request' && chunk.runId && !requested.has(chunk.runId)) {
65
+ requested.set(chunk.runId, chunk.seq);
66
+ }
67
+ }
68
+ return (chunk) => (chunk.runId ? (requested.get(chunk.runId) ?? chunk.seq) : chunk.seq) <= through.seq;
69
+ }
61
70
  function newestCompaction(chunks) {
62
71
  let newest = null;
63
72
  for (const chunk of chunks) {
@@ -79,8 +88,9 @@ export function foldCompactedChunks(chunks) {
79
88
  if (!through) {
80
89
  return { chunks, summary: null };
81
90
  }
91
+ const folded = foldsThrough(chunks, through);
82
92
  return {
83
- chunks: chunks.filter((chunk) => chunk.seq > through.seq && !chunk.compaction),
93
+ chunks: chunks.filter((chunk) => !folded(chunk) && !chunk.compaction),
84
94
  summary: compaction.summary
85
95
  };
86
96
  }
@@ -9,5 +9,8 @@ export declare const MODEL_MEMORY_CAP_KEY = "model.memoryCapPercent";
9
9
  export declare const MODEL_MEMORY_CAP_DEFAULT = 85;
10
10
  export declare const MODEL_MEMORY_CAP_MIN = 10;
11
11
  export declare const MODEL_MEMORY_CAP_MAX = 100;
12
+ export declare const KV_CACHE_RETENTION_KEY = "kvCache.retentionDays";
13
+ export declare const KV_CACHE_RETENTION_DAYS_DEFAULT = 14;
14
+ export declare const KV_CACHE_RETENTION_NEVER = 0;
12
15
  export declare const FAKE_MODELS_KEY = "debug.fakeModels";
13
16
  export declare const QUACK_MODEL = "quack";
@@ -11,5 +11,8 @@ export const MODEL_MEMORY_CAP_KEY = 'model.memoryCapPercent';
11
11
  export const MODEL_MEMORY_CAP_DEFAULT = 85;
12
12
  export const MODEL_MEMORY_CAP_MIN = 10;
13
13
  export const MODEL_MEMORY_CAP_MAX = 100;
14
+ export const KV_CACHE_RETENTION_KEY = 'kvCache.retentionDays';
15
+ export const KV_CACHE_RETENTION_DAYS_DEFAULT = 14;
16
+ export const KV_CACHE_RETENTION_NEVER = 0;
14
17
  export const FAKE_MODELS_KEY = 'debug.fakeModels';
15
18
  export const QUACK_MODEL = 'quack';
@@ -13,6 +13,7 @@ export async function withArchivedMesh(row, opts, fn) {
13
13
  key: row.key,
14
14
  deviceId: opts.deviceId,
15
15
  writerSeed: row.writerSeed ?? undefined,
16
+ compute: row.compute,
16
17
  logger: opts.logger
17
18
  });
18
19
  await mesh.ready();
@@ -1,5 +1,6 @@
1
+ import b4a from 'b4a';
1
2
  import ScopeLock from 'scope-lock';
2
- import { findChunksOfType } from './chunk.js';
3
+ import { findChunksOfTypes } from './chunk.js';
3
4
  import { ACTIVE_CHATS, CHUNKS } from './collections.js';
4
5
  const TERMINAL_RUN_STATES = new Set(['completed', 'failed', 'canceled']);
5
6
  const ACTIVITY_CHUNK_TYPES = [
@@ -39,15 +40,16 @@ class ChatActivityCatalog {
39
40
  try {
40
41
  const head = store.view.db.head();
41
42
  const forwards = store.autobee.stats.fastForwards;
42
- if (forwards !== this._forwards) {
43
- this._head = null;
44
- }
45
- this._forwards = forwards;
43
+ const from = forwards === this._forwards && this._head && head && b4a.equals(this._head.key, head.key)
44
+ ? this._head
45
+ : null;
46
46
  const snap = store.view.snapshot();
47
47
  try {
48
48
  const chats = await snap.find(ACTIVE_CHATS).toArray();
49
49
  const activeChatIds = new Set(chats.map((chat) => chat.id));
50
- await this._refresh(snap, head, activeChatIds);
50
+ await this._refresh(snap, from, activeChatIds);
51
+ this._head = head;
52
+ this._forwards = forwards;
51
53
  const window = chatIds && chatIds.length > 0 ? new Set(chatIds) : null;
52
54
  return [...this._states()]
53
55
  .filter(([id]) => !window || window.has(id))
@@ -62,18 +64,17 @@ class ChatActivityCatalog {
62
64
  this._lock.unlock();
63
65
  }
64
66
  }
65
- async _refresh(snap, head, activeChatIds) {
66
- if (!this._head || !head) {
67
+ async _refresh(snap, from, activeChatIds) {
68
+ if (!from) {
67
69
  await this._rebuild(snap, activeChatIds);
68
70
  }
69
71
  else {
70
72
  this._prune(activeChatIds);
71
73
  await this._loadMissing(snap, activeChatIds);
72
- if (!(await this._applyChanges(snap, this._head, activeChatIds))) {
74
+ if (!(await this._applyChanges(snap, from, activeChatIds))) {
73
75
  await this._rebuild(snap, activeChatIds);
74
76
  }
75
77
  }
76
- this._head = head;
77
78
  }
78
79
  async _rebuild(snap, activeChatIds) {
79
80
  this._chats.clear();
@@ -98,7 +99,7 @@ class ChatActivityCatalog {
98
99
  async _loadChat(snap, chatId) {
99
100
  const activity = { runs: new Map() };
100
101
  this._chats.set(chatId, activity);
101
- for (const chunk of await activityChunks(snap, chatId)) {
102
+ for (const chunk of await findChunksOfTypes(snap, { chatId, types: ACTIVITY_CHUNK_TYPES })) {
102
103
  applyChunk(activity, chunk);
103
104
  }
104
105
  }
@@ -252,10 +253,6 @@ function applyApproval(run, chunk) {
252
253
  run.pendingApprovals.delete(chunk.id);
253
254
  }
254
255
  }
255
- async function activityChunks(snap, chatId) {
256
- const groups = await Promise.all(ACTIVITY_CHUNK_TYPES.map((type) => findChunksOfType(snap, { chatId, type })));
257
- return groups.flat().sort((left, right) => left.seq - right.seq);
258
- }
259
256
  function settleTool(tools, name) {
260
257
  for (let index = tools.length - 1; index >= 0; index -= 1) {
261
258
  if (tools[index]?.name !== name)
@@ -1,5 +1,6 @@
1
1
  import type HyperDB from 'hyperdb';
2
2
  import type { QvacChunk, QvacUpdateChunkOperation } from '../../spec/mesh/hyperschema/types.d.ts';
3
+ import type { AssistantChunkContentToolResult } from '../../spec/hyperschema/types.d.ts';
3
4
  import type { Mesh, MeshDispatcher } from './mesh.ts';
4
5
  import { type Bounds } from './range.ts';
5
6
  type ChunkPayload = Pick<QvacChunk, 'userMessage' | 'content' | 'thinking' | 'toolCall' | 'toolResult' | 'attachment' | 'approvalRequest' | 'runStatus' | 'runRequest' | 'sources' | 'compaction'>;
@@ -18,13 +19,16 @@ type ListAttachmentsByKindInput = Bounds & {
18
19
  kind: MediaKind;
19
20
  origin?: AttachmentOrigin;
20
21
  };
21
- export type ReadChunk = QvacChunk & {
22
+ export type ReadChunk = Omit<QvacChunk, 'toolResult' | 'attachment'> & {
23
+ readonly toolResult: AssistantChunkContentToolResult | null;
22
24
  readonly attachment?: (NonNullable<QvacChunk['attachment']> & {
23
25
  readonly url?: string;
24
26
  }) | null;
25
27
  };
28
+ export declare function readChunk({ toolResult, ...chunk }: QvacChunk): ReadChunk;
29
+ export declare function linkChunks(store: Mesh, chunks: QvacChunk[]): ReadChunk[];
26
30
  export declare function recordLinks(store: Mesh, chunks: ReadChunk[]): Promise<ReadChunk[]>;
27
- type ListChunksInput = Bounds & {
31
+ export type ListChunksInput = Bounds & {
28
32
  chatId: string;
29
33
  type?: QvacChunk['type'];
30
34
  types?: ReadonlyArray<QvacChunk['type']>;
@@ -34,8 +38,15 @@ export interface FindChunksOfTypeInput extends Bounds {
34
38
  readonly type: QvacChunk['type'];
35
39
  }
36
40
  export declare function findChunksOfType(snap: HyperDB, { chatId, type, ...bounds }: FindChunksOfTypeInput): Promise<QvacChunk[]>;
37
- export declare function listChunks(store: Mesh, { chatId, type, types, ...bounds }: ListChunksInput): Promise<ReadChunk[]>;
41
+ export interface FindChunksOfTypesInput extends Omit<Bounds, 'limit'> {
42
+ readonly chatId: string;
43
+ readonly types: ReadonlyArray<QvacChunk['type']>;
44
+ }
45
+ export declare function findChunksOfTypes(db: HyperDB, { chatId, types, ...bounds }: FindChunksOfTypesInput): Promise<QvacChunk[]>;
46
+ export declare function listChunks(store: Mesh, input: ListChunksInput): Promise<ReadChunk[]>;
47
+ export declare function readChunks(db: HyperDB, { chatId, type, types, ...bounds }: ListChunksInput): Promise<QvacChunk[]>;
38
48
  export declare function listAttachmentsByKind(store: Mesh, { kind, origin, ...bounds }: ListAttachmentsByKindInput): Promise<ReadChunk[]>;
49
+ export declare function selectsChunk({ chatId, type, types, gt, gte, lt, lte }: ListChunksInput, chunk: QvacChunk): boolean;
39
50
  export declare function getChunk(store: Mesh, id: string): Promise<QvacChunk | null>;
40
51
  export interface AppendedChunk {
41
52
  readonly id: string;