@qvac/core 1.0.0-rc.5 → 1.0.0-rc.7

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 (131) hide show
  1. package/CHANGELOG.md +43 -2
  2. package/README.md +55 -4
  3. package/dist-lib/lib/api/agents.d.ts +2 -1
  4. package/dist-lib/lib/api/agents.js +10 -3
  5. package/dist-lib/lib/api/app-config.d.ts +3 -0
  6. package/dist-lib/lib/api/app-config.js +20 -0
  7. package/dist-lib/lib/api/approval-rules.js +8 -5
  8. package/dist-lib/lib/api/chat.d.ts +1 -2
  9. package/dist-lib/lib/api/chat.js +4 -18
  10. package/dist-lib/lib/api/device-routes.d.ts +1 -1
  11. package/dist-lib/lib/api/device-routes.js +3 -1
  12. package/dist-lib/lib/api/index.d.ts +0 -1
  13. package/dist-lib/lib/api/index.js +3 -3
  14. package/dist-lib/lib/api/mesh.d.ts +1 -1
  15. package/dist-lib/lib/api/mesh.js +1 -1
  16. package/dist-lib/lib/api/projects.js +3 -4
  17. package/dist-lib/lib/api/served.d.ts +2 -2
  18. package/dist-lib/lib/api/served.js +24 -1
  19. package/dist-lib/lib/api/sync.js +12 -2
  20. package/dist-lib/lib/assistant/harness.d.ts +3 -0
  21. package/dist-lib/lib/assistant/history.js +14 -2
  22. package/dist-lib/lib/assistant/message.d.ts +1 -0
  23. package/dist-lib/lib/assistant/model-round.d.ts +3 -0
  24. package/dist-lib/lib/assistant/model-round.js +16 -2
  25. package/dist-lib/lib/assistant/round-host.d.ts +2 -0
  26. package/dist-lib/lib/assistant/round-host.js +8 -0
  27. package/dist-lib/lib/assistant-tree.d.ts +2 -0
  28. package/dist-lib/lib/assistant-tree.js +4 -2
  29. package/dist-lib/lib/assistant.d.ts +11 -2
  30. package/dist-lib/lib/assistant.js +124 -20
  31. package/dist-lib/lib/client.js +4 -1
  32. package/dist-lib/lib/core/agent-copies.js +2 -1
  33. package/dist-lib/lib/core/agent-hosting.js +9 -4
  34. package/dist-lib/lib/core/agent.js +2 -1
  35. package/dist-lib/lib/core/archived-mesh.d.ts +1 -0
  36. package/dist-lib/lib/core/archived-mesh.js +11 -14
  37. package/dist-lib/lib/core/chat-copy.d.ts +1 -1
  38. package/dist-lib/lib/core/chat-copy.js +14 -4
  39. package/dist-lib/lib/core/chat-move.d.ts +3 -1
  40. package/dist-lib/lib/core/chat-move.js +5 -2
  41. package/dist-lib/lib/core/chat.d.ts +0 -1
  42. package/dist-lib/lib/core/chat.js +0 -3
  43. package/dist-lib/lib/core/collections.d.ts +7 -7
  44. package/dist-lib/lib/core/collections.js +7 -7
  45. package/dist-lib/lib/core/contract/joined-meshes.js +2 -1
  46. package/dist-lib/lib/core/contract/project-device-keys.d.ts +4 -2
  47. package/dist-lib/lib/core/contract/project-device-keys.js +12 -12
  48. package/dist-lib/lib/core/contract/project-host-keys.d.ts +5 -3
  49. package/dist-lib/lib/core/contract/project-host-keys.js +22 -11
  50. package/dist-lib/lib/core/contract/projects.d.ts +4 -2
  51. package/dist-lib/lib/core/contract/projects.js +29 -17
  52. package/dist-lib/lib/core/deleted-agent-runs.d.ts +1 -0
  53. package/dist-lib/lib/core/deleted-agent-runs.js +22 -11
  54. package/dist-lib/lib/core/helpers/index.d.ts +1 -1
  55. package/dist-lib/lib/core/helpers/index.js +2 -2
  56. package/dist-lib/lib/core/local/index.d.ts +2 -1
  57. package/dist-lib/lib/core/local/index.js +3 -2
  58. package/dist-lib/lib/core/mesh.d.ts +1 -0
  59. package/dist-lib/lib/core/mesh.js +4 -4
  60. package/dist-lib/lib/core/project-keys.d.ts +5 -2
  61. package/dist-lib/lib/core/project-keys.js +15 -6
  62. package/dist-lib/lib/core/public-mesh.d.ts +6 -8
  63. package/dist-lib/lib/core/public-mesh.js +19 -29
  64. package/dist-lib/lib/core/sync/index.js +6 -6
  65. package/dist-lib/lib/core.d.ts +17 -8
  66. package/dist-lib/lib/core.js +317 -224
  67. package/dist-lib/lib/errors.d.ts +4 -12
  68. package/dist-lib/lib/errors.js +4 -12
  69. package/dist-lib/lib/kv-cache-ledger.d.ts +22 -0
  70. package/dist-lib/lib/kv-cache-ledger.js +72 -0
  71. package/dist-lib/lib/kv-cache-retention.d.ts +1 -0
  72. package/dist-lib/lib/kv-cache-retention.js +5 -2
  73. package/dist-lib/lib/model-memory-cap.d.ts +3 -0
  74. package/dist-lib/lib/model-memory-cap.js +15 -6
  75. package/dist-lib/lib/run-replay.d.ts +3 -2
  76. package/dist-lib/lib/run-replay.js +16 -5
  77. package/dist-lib/lib/run-transcript.js +1 -1
  78. package/dist-lib/schema/engine.js +64 -4
  79. package/dist-lib/schema/local.js +4 -2
  80. package/dist-lib/schema/qvac.js +64 -2
  81. package/dist-lib/spec/bind.js +3 -0
  82. package/dist-lib/spec/calls.js +1 -0
  83. package/dist-lib/spec/capabilities.d.ts +4 -2
  84. package/dist-lib/spec/hrpc/hrpc.json +14 -1
  85. package/dist-lib/spec/hrpc/index.js +16 -4
  86. package/dist-lib/spec/hrpc/messages.js +508 -235
  87. package/dist-lib/spec/hyperschema/index.js +508 -235
  88. package/dist-lib/spec/hyperschema/schema.json +214 -6
  89. package/dist-lib/spec/hyperschema/types.d.ts +40 -1
  90. package/dist-lib/spec/local/hyperdb/messages.js +26 -12
  91. package/dist-lib/spec/local/hyperschema/index.js +13 -6
  92. package/dist-lib/spec/local/hyperschema/schema.json +10 -0
  93. package/dist-lib/spec/local/hyperschema/types.d.ts +2 -0
  94. package/dist-lib/spec/mesh/hyperdb/db.json +110 -0
  95. package/dist-lib/spec/mesh/hyperdb/index.js +498 -2
  96. package/dist-lib/spec/mesh/hyperdb/messages.js +895 -416
  97. package/dist-lib/spec/mesh/hyperdispatch/messages.js +259 -6
  98. package/dist-lib/spec/mesh/hyperschema/index.js +259 -6
  99. package/dist-lib/spec/mesh/hyperschema/schema.json +160 -0
  100. package/dist-lib/spec/mesh/hyperschema/types.d.ts +38 -0
  101. package/package.json +27 -11
  102. package/tui/README.md +190 -0
  103. package/tui/app.mjs +8282 -0
  104. package/tui/boot.mjs +159 -0
  105. package/tui/index.mjs +89 -0
  106. package/tui/lib/boot-timing.mjs +41 -0
  107. package/tui/lib/busy-words.mjs +116 -0
  108. package/tui/lib/connect-sheet.mjs +350 -0
  109. package/tui/lib/device-target.mjs +64 -0
  110. package/tui/lib/export.mjs +194 -0
  111. package/tui/lib/folders-command.mjs +84 -0
  112. package/tui/lib/fork-command.mjs +43 -0
  113. package/tui/lib/knowledge-command.mjs +190 -0
  114. package/tui/lib/knowledge-import.mjs +153 -0
  115. package/tui/lib/local-path.mjs +21 -0
  116. package/tui/lib/move-command.mjs +17 -0
  117. package/tui/lib/render.mjs +225 -0
  118. package/tui/lib/save-file.mjs +88 -0
  119. package/tui/lib/select.mjs +81 -0
  120. package/tui/lib/selection.mjs +18 -0
  121. package/tui/lib/setup.mjs +135 -0
  122. package/tui/lib/sidebar.mjs +63 -0
  123. package/tui/lib/skill-connect.mjs +145 -0
  124. package/tui/lib/splash-art.mjs +107 -0
  125. package/tui/lib/splash.mjs +18 -0
  126. package/tui/lib/upload-frames.mjs +9 -0
  127. package/tui/lib/usage.mjs +59 -0
  128. package/tui/lib/wrap.mjs +143 -0
  129. package/tui/main.mjs +279 -0
  130. package/tui/plugins.mjs +158 -0
  131. package/worker/assistant-sidecar.mjs +1 -0
@@ -12,6 +12,7 @@ export interface MediaRef {
12
12
  readonly frame?: number | undefined;
13
13
  readonly frames?: number | undefined;
14
14
  }
15
+ export declare function wholeImageId(ref: MediaRef | undefined): string | undefined;
15
16
  export interface MediaSource {
16
17
  readonly ref: MediaRef;
17
18
  readonly attachment: AttachmentRef;
@@ -59,6 +60,7 @@ export declare class OwnerRounds {
59
60
  ranHere(round: number | undefined): boolean;
60
61
  retook(): boolean;
61
62
  echoed(type: string, text: string): boolean;
63
+ retire(): Promise<void>;
62
64
  private _round;
63
65
  probe({ kind, prompt, modelId, responseFormat, signal }: Probe): Promise<string>;
64
66
  private _probed;
@@ -68,6 +70,7 @@ export declare class OwnerRounds {
68
70
  private _switchedTo;
69
71
  private _sendContext;
70
72
  private _options;
73
+ private _namedImages;
71
74
  private _media;
72
75
  private _knownSets;
73
76
  private _readKnownSets;
@@ -9,6 +9,9 @@ import { PdfBoundsSchema } from './pdf-pages.js';
9
9
  import { isProbeRound, PROBE_KINDS } from '../probe-rounds.js';
10
10
  const message = z.custom((value) => isRecord(value) && typeof value.role === 'string' && typeof value.content === 'string');
11
11
  const record = () => z.custom((value) => isRecord(value));
12
+ export function wholeImageId(ref) {
13
+ return ref && ref.page === undefined && ref.frame === undefined ? ref.id : undefined;
14
+ }
12
15
  const mediaRef = z.object({
13
16
  id: z.string(),
14
17
  page: z.number().optional(),
@@ -70,6 +73,13 @@ export class OwnerRounds {
70
73
  this._echoes.splice(at, 1);
71
74
  return true;
72
75
  }
76
+ async retire() {
77
+ const { engine, chatId, runId } = this._opts;
78
+ const { chunks } = await engine.listChunks({ chatId, type: 'model-request' });
79
+ const live = (chunks ?? []).filter((chunk) => chunk.runId === runId &&
80
+ (chunk.modelRequest?.state === 'open' || chunk.modelRequest?.state === 'taken'));
81
+ await Promise.all(live.map((chunk) => engine.cancelModelRequest({ chunkId: chunk.id }).catch(noop)));
82
+ }
73
83
  async _round(request, io) {
74
84
  if (io.signal.aborted)
75
85
  throw new AbortedError();
@@ -83,7 +93,7 @@ export class OwnerRounds {
83
93
  round: request.round,
84
94
  cacheKey: request.cacheKey,
85
95
  promptPrefix: request.promptPrefix,
86
- toolTurns: request.history.slice(input),
96
+ toolTurns: request.history.slice(input).map((turn) => this._namedImages(turn)),
87
97
  options: await this._options(request, system)
88
98
  };
89
99
  const agentId = (await this._switchedTo()) ?? this._opts.agentId;
@@ -254,6 +264,10 @@ export class OwnerRounds {
254
264
  }
255
265
  return options;
256
266
  }
267
+ _namedImages(turn) {
268
+ const imageIds = (turn.attachments ?? []).flatMap(({ path }) => wholeImageId(this._opts.media?.(path)) ?? []);
269
+ return imageIds.length ? { ...turn, imageIds } : turn;
270
+ }
257
271
  _media(history) {
258
272
  const media = history.flatMap((turn, at) => {
259
273
  const refs = (turn.attachments ?? []).flatMap(({ path }) => this._opts.media?.(path) ?? []);
@@ -510,7 +524,7 @@ export async function servedRequest(engine, round, media) {
510
524
  const history = [
511
525
  ...(system === null ? [] : [{ role: 'system', content: system }]),
512
526
  ...input,
513
- ...toolTurns.data.map(({ attachments: _owners, ...turn }) => turn)
527
+ ...toolTurns.data.map(({ attachments: _owners, imageIds: _named, ...turn }) => turn)
514
528
  ];
515
529
  await showMedia(history, {
516
530
  all,
@@ -9,6 +9,7 @@ export interface RoundHostOptions {
9
9
  readonly serve: NonNullable<Harness['serveModelRound']>;
10
10
  readonly deviceId: () => Buffer;
11
11
  readonly media?: RoundMedia | null;
12
+ readonly built?: (meshId: string, chatId: string) => void;
12
13
  readonly log?: LoggerInterface | null;
13
14
  }
14
15
  export interface RoundMedia {
@@ -28,6 +29,7 @@ export declare class RoundHost {
28
29
  constructor(opts: RoundHostOptions);
29
30
  follow(meshId: string, frames: WatchStream<AssistantChunkList>): Promise<void>;
30
31
  left(chatId: string, agentId: string): string[];
32
+ holds(chatId: string): boolean;
31
33
  stop(): void;
32
34
  private _reconcile;
33
35
  private _pick;
@@ -41,6 +41,10 @@ export class RoundHost {
41
41
  this._built.delete(at);
42
42
  return keys;
43
43
  }
44
+ holds(chatId) {
45
+ const base = chatKvCacheKey(chatId);
46
+ return [...this._keys.keys()].some((key) => key.startsWith(`${base}/`));
47
+ }
44
48
  stop() {
45
49
  for (const { controller } of this._serving.values()) {
46
50
  controller.abort(LEFT);
@@ -78,6 +82,7 @@ export class RoundHost {
78
82
  }
79
83
  _serve(meshId, round) {
80
84
  const serving = {
85
+ meshId,
81
86
  controller: new AbortController(),
82
87
  taken: false,
83
88
  takenAt: 0
@@ -132,6 +137,9 @@ export class RoundHost {
132
137
  return this._probe(engine, { round, request, serving });
133
138
  }
134
139
  const key = this._claimKey(round, request.cacheKey);
140
+ if (key && round.chatId) {
141
+ this._opts.built?.(serving.meshId, round.chatId);
142
+ }
135
143
  const transcript = new RunTranscript(engine, {
136
144
  chatId: round.chatId,
137
145
  runId: request.runId,
@@ -24,6 +24,7 @@ export interface AssistantTreeOptions {
24
24
  imageCacheDir?: string;
25
25
  inferenceCallsDir?: string;
26
26
  runReplayDir?: string;
27
+ kvCacheChatsDir?: string;
27
28
  imageCacheMaxBytes?: number;
28
29
  imageCacheMaxAgeMs?: number;
29
30
  kvCacheRetentionMs?: number;
@@ -42,6 +43,7 @@ export interface AssistantChildOptions {
42
43
  imageCacheDir?: string;
43
44
  inferenceCallsDir?: string;
44
45
  runReplayDir?: string;
46
+ kvCacheChatsDir?: string;
45
47
  imageCacheMaxBytes?: number;
46
48
  imageCacheMaxAgeMs?: number;
47
49
  kvCacheRetentionMs?: number;
@@ -8,7 +8,7 @@ import { startModelMemoryCap } from './model-memory-cap.js';
8
8
  import { startDownloadsAllowedPusher } from './downloads-allowed-pusher.js';
9
9
  import { duplexPair } from '@qvac/harness/transport';
10
10
  export function assistantTree(opts) {
11
- const { core, harness, logger = null, imageCacheDir, inferenceCallsDir, runReplayDir, imageCacheMaxBytes, imageCacheMaxAgeMs, kvCacheRetentionMs, embedder, extractors, onHarness, platform, preloadCurrentAgentModel, fakeModels = false } = opts;
11
+ const { core, harness, logger = null, imageCacheDir, inferenceCallsDir, runReplayDir, kvCacheChatsDir, imageCacheMaxBytes, imageCacheMaxAgeMs, kvCacheRetentionMs, embedder, extractors, onHarness, platform, preloadCurrentAgentModel, fakeModels = false } = opts;
12
12
  const sup = new Supervisor();
13
13
  const logged = ['child-died', 'child-restarting', 'child-ready', 'gave-up', 'stall'];
14
14
  for (const event of logged) {
@@ -75,6 +75,7 @@ export function assistantTree(opts) {
75
75
  imageCacheDir,
76
76
  inferenceCallsDir,
77
77
  runReplayDir,
78
+ kvCacheChatsDir,
78
79
  imageCacheMaxBytes,
79
80
  imageCacheMaxAgeMs,
80
81
  kvCacheRetentionMs,
@@ -239,7 +240,7 @@ export function assistantTree(opts) {
239
240
  return sup;
240
241
  }
241
242
  export function assistantChild(opts) {
242
- const { harness, core = 'core', logger = null, imageCacheDir, inferenceCallsDir, runReplayDir, imageCacheMaxBytes, imageCacheMaxAgeMs, kvCacheRetentionMs, embedder, extractors, onHarness, preloadCurrentAgentModel, fakeModels, ...spec } = opts;
243
+ const { harness, core = 'core', logger = null, imageCacheDir, inferenceCallsDir, runReplayDir, kvCacheChatsDir, imageCacheMaxBytes, imageCacheMaxAgeMs, kvCacheRetentionMs, embedder, extractors, onHarness, preloadCurrentAgentModel, fakeModels, ...spec } = opts;
243
244
  return {
244
245
  ...spec,
245
246
  deps: [core],
@@ -251,6 +252,7 @@ export function assistantChild(opts) {
251
252
  imageCacheDir,
252
253
  inferenceCallsDir,
253
254
  runReplayDir,
255
+ kvCacheChatsDir,
254
256
  imageCacheMaxBytes,
255
257
  imageCacheMaxAgeMs,
256
258
  kvCacheRetentionMs,
@@ -25,6 +25,7 @@ export interface AssistantOptions {
25
25
  imageCacheDir?: string;
26
26
  inferenceCallsDir?: string;
27
27
  runReplayDir?: string;
28
+ kvCacheChatsDir?: string;
28
29
  imageCacheMaxBytes?: number;
29
30
  imageCacheMaxAgeMs?: number;
30
31
  kvCacheRetentionMs?: number;
@@ -66,7 +67,6 @@ export declare class QvacAssistant extends ReadyResource {
66
67
  private _deviceId;
67
68
  private _info;
68
69
  private readonly _lastCapabilities;
69
- private _ownWorkspace;
70
70
  private _listed;
71
71
  private _accountHosted;
72
72
  private _readySkills;
@@ -75,6 +75,7 @@ export declare class QvacAssistant extends ReadyResource {
75
75
  private _reconciling;
76
76
  private readonly _onlineBoot;
77
77
  private readonly _accountSeen;
78
+ private readonly _meshesSeen;
78
79
  private _capability;
79
80
  private _downloads;
80
81
  private _state;
@@ -101,6 +102,8 @@ export declare class QvacAssistant extends ReadyResource {
101
102
  private readonly _runReplay;
102
103
  private readonly _roundHost;
103
104
  private readonly _kvRetention;
105
+ private readonly _kvLedger;
106
+ private _orphanSweep;
104
107
  private readonly _lifecycle;
105
108
  private readonly _suggesting;
106
109
  private readonly _audioTranscripts;
@@ -115,7 +118,7 @@ export declare class QvacAssistant extends ReadyResource {
115
118
  private readonly _modelCatalog;
116
119
  private readonly _onHarnessStatus;
117
120
  private readonly _onModelCatalogEvent;
118
- constructor(engine: Capabilities, harness: Harness, { logger, imageCacheDir, inferenceCallsDir, runReplayDir, imageCacheMaxBytes, imageCacheMaxAgeMs, kvCacheRetentionMs, downloadRetryMs, platform, preloadCurrentAgentModel, drives, blobReadTimeoutMs, runIdleTimeoutMs, modelReloadDelayMs, pdfRasterizer: pdfRasterizerOption }?: AssistantOptions);
121
+ constructor(engine: Capabilities, harness: Harness, { logger, imageCacheDir, inferenceCallsDir, runReplayDir, kvCacheChatsDir, imageCacheMaxBytes, imageCacheMaxAgeMs, kvCacheRetentionMs, downloadRetryMs, platform, preloadCurrentAgentModel, drives, blobReadTimeoutMs, runIdleTimeoutMs, modelReloadDelayMs, pdfRasterizer: pdfRasterizerOption }?: AssistantOptions);
119
122
  private _requireDeviceId;
120
123
  _open(): Promise<void>;
121
124
  private _triggerModelWarm;
@@ -185,6 +188,11 @@ export declare class QvacAssistant extends ReadyResource {
185
188
  deleteChatKvCache(chatId: string): Promise<AssistantKvCacheDeletionResponse>;
186
189
  private _reclaimChatKvCache;
187
190
  clearKvCache(): Promise<AssistantKvCacheDeletionResponse>;
191
+ private _recordKvCache;
192
+ private _reclaimOrphanedKvCaches;
193
+ private _sweepOrphanedKvCaches;
194
+ private _knownChats;
195
+ private _kvCacheInUse;
188
196
  private _clearPreReplayCaches;
189
197
  private _inspectCall;
190
198
  private _inspectRound;
@@ -237,6 +245,7 @@ export declare class QvacAssistant extends ReadyResource {
237
245
  private _openVideo;
238
246
  private _hearVideo;
239
247
  private _hydrateImages;
248
+ private _showImage;
240
249
  private _hydrateTurnImages;
241
250
  private _servedMedia;
242
251
  private _servedFile;
@@ -38,12 +38,13 @@ import { persistentDir } from './persistent-dir.js';
38
38
  import { ModelCatalogRevisionHub } from './model-catalog-revision.js';
39
39
  import { agentRekeyFingerprint, agentSwitched, chatKvCacheKey, chatRepairKvCacheKey, disappearedChatIds } from './kv-cache-reclaim.js';
40
40
  import { KvCacheRetention } from './kv-cache-retention.js';
41
+ import { KvCacheLedger, orphanedChats } from './kv-cache-ledger.js';
41
42
  import { KV_CACHE_RETENTION_KEY } from './constants.js';
42
43
  import { buildCompactionPrompt, canCompact, foldReducesPrompt, COMPACTION_KEEP_TURNS, COMPACTION_TURN_PREFIX, isContextOverflowFailure, nearContextLimit, compactionBoundary, foldsThrough, lastCompactedThroughId, parseCompactionSummary } from './compaction.js';
43
44
  import { ModelDownloadReconciler, musicModelChoiceOf } from './model-download.js';
44
45
  import { AbortedError, raceAbort } from '@qvac/harness/race-abort';
45
46
  import { RunTranscript } from './run-transcript.js';
46
- import { OwnerRounds, listRoundChunks } from './assistant/model-round.js';
47
+ import { OwnerRounds, listRoundChunks, wholeImageId } from './assistant/model-round.js';
47
48
  import { RoundHost } from './assistant/round-host.js';
48
49
  import { searchKnowledge } from './knowledge-search.js';
49
50
  import { isLifecycleTeardown, knownAbortOutcome, RUN_ABORT_REASONS, RUN_FAILURE_REASONS, runFailureNotice } from './run-notices.js';
@@ -280,7 +281,6 @@ export class QvacAssistant extends ReadyResource {
280
281
  _deviceId;
281
282
  _info;
282
283
  _lastCapabilities = new Map();
283
- _ownWorkspace = null;
284
284
  _listed = [];
285
285
  _accountHosted = [];
286
286
  _readySkills;
@@ -289,6 +289,7 @@ export class QvacAssistant extends ReadyResource {
289
289
  _reconciling;
290
290
  _onlineBoot = new ReadyGuard();
291
291
  _accountSeen = Promise.withResolvers();
292
+ _meshesSeen = Promise.withResolvers();
292
293
  _capability;
293
294
  _downloads;
294
295
  _state;
@@ -315,6 +316,8 @@ export class QvacAssistant extends ReadyResource {
315
316
  _runReplay;
316
317
  _roundHost;
317
318
  _kvRetention;
319
+ _kvLedger;
320
+ _orphanSweep = Promise.resolve();
318
321
  _lifecycle;
319
322
  _suggesting = new Map();
320
323
  _audioTranscripts;
@@ -329,7 +332,7 @@ export class QvacAssistant extends ReadyResource {
329
332
  _modelCatalog = new ModelCatalogRevisionHub();
330
333
  _onHarnessStatus;
331
334
  _onModelCatalogEvent;
332
- constructor(engine, harness, { logger = null, imageCacheDir, inferenceCallsDir, runReplayDir, imageCacheMaxBytes, imageCacheMaxAgeMs, kvCacheRetentionMs, downloadRetryMs, platform = os.platform(), preloadCurrentAgentModel = true, drives, blobReadTimeoutMs = BLOB_READ_TIMEOUT_MS, runIdleTimeoutMs = RUN_IDLE_TIMEOUT_MS, modelReloadDelayMs = MODEL_RELOAD_DELAY_MS, pdfRasterizer: pdfRasterizerOption } = {}) {
335
+ constructor(engine, harness, { logger = null, imageCacheDir, inferenceCallsDir, runReplayDir, kvCacheChatsDir, imageCacheMaxBytes, imageCacheMaxAgeMs, kvCacheRetentionMs, downloadRetryMs, platform = os.platform(), preloadCurrentAgentModel = true, drives, blobReadTimeoutMs = BLOB_READ_TIMEOUT_MS, runIdleTimeoutMs = RUN_IDLE_TIMEOUT_MS, modelReloadDelayMs = MODEL_RELOAD_DELAY_MS, pdfRasterizer: pdfRasterizerOption } = {}) {
333
336
  super();
334
337
  this._engine = engine;
335
338
  this._harness = harness;
@@ -356,6 +359,7 @@ export class QvacAssistant extends ReadyResource {
356
359
  resolve: (sources, served) => this._servedMedia(sources, served),
357
360
  release: (id) => this._runImageCacheNames.delete(`round:${id}`)
358
361
  },
362
+ built: (meshId, chatId) => this._recordKvCache(meshId, chatId),
359
363
  log: logger
360
364
  })
361
365
  : null;
@@ -382,9 +386,10 @@ export class QvacAssistant extends ReadyResource {
382
386
  this._backgroundReplays = new Set();
383
387
  this._inferenceCalls = new InferenceCallStore(inferenceCallsDir ?? persistentDir('inspector'));
384
388
  this._runReplay = new RunReplayStore(runReplayDir ?? persistentDir('run-replay'));
389
+ this._kvLedger = new KvCacheLedger(kvCacheChatsDir ?? persistentDir('kv-cache-chats'));
385
390
  this._kvRetention = new KvCacheRetention(engine, {
386
391
  active: () => !this.closing && !this.suspended,
387
- running: (chatId) => [...this._runs.values()].some((run) => run.chatId === chatId),
392
+ running: (chatId) => this._kvCacheInUse(chatId),
388
393
  deleteChatKvCache: (chatId) => this._deleteChatKvCache(chatId, 'idle past retention'),
389
394
  defaultMs: kvCacheRetentionMs,
390
395
  logger
@@ -542,6 +547,8 @@ export class QvacAssistant extends ReadyResource {
542
547
  finally {
543
548
  this._onlineBoot.exit();
544
549
  }
550
+ await this._meshesSeen.promise;
551
+ await this._reclaimOrphanedKvCaches();
545
552
  }
546
553
  async onlineReady() {
547
554
  if (!this.opened)
@@ -1353,6 +1360,7 @@ export class QvacAssistant extends ReadyResource {
1353
1360
  };
1354
1361
  if (result.status === 'success') {
1355
1362
  await this._runReplay.forget(chatId, { holder: LOCAL_HOLDER, reason });
1363
+ await this._kvLedger.forget(chatId);
1356
1364
  }
1357
1365
  return result;
1358
1366
  }
@@ -1360,9 +1368,65 @@ export class QvacAssistant extends ReadyResource {
1360
1368
  const result = await harnessClearKvCache(this._harness)();
1361
1369
  if (result.status === 'success') {
1362
1370
  await this._runReplay.clear(LOCAL_HOLDER);
1371
+ await this._kvLedger.clear();
1363
1372
  }
1364
1373
  return result;
1365
1374
  }
1375
+ _recordKvCache(meshId, chatId) {
1376
+ const served = this._listed.some((mesh) => mesh.meshId === meshId && mesh.served);
1377
+ void this._kvLedger
1378
+ .record({ meshId, chatId, served })
1379
+ .catch((err) => this._log?.warn('assistant', 'kv-cache chat not recorded', { chatId, err }));
1380
+ }
1381
+ _reclaimOrphanedKvCaches(gone = new Set()) {
1382
+ this._orphanSweep = this._orphanSweep
1383
+ .then(() => this._sweepOrphanedKvCaches(gone))
1384
+ .catch((err) => this._log?.warn('assistant', 'orphaned kv-cache sweep failed', { err }));
1385
+ return this._orphanSweep;
1386
+ }
1387
+ async _sweepOrphanedKvCaches(gone) {
1388
+ if (this.closing || this.suspended)
1389
+ return;
1390
+ if ((await this._kvRetention.retentionMs()) === null)
1391
+ return;
1392
+ const entries = await this._kvLedger.entries();
1393
+ if (entries.length === 0) {
1394
+ return;
1395
+ }
1396
+ for (const chatId of orphanedChats(entries, { ...(await this._knownChats()), gone })) {
1397
+ if (this.closing || this.suspended)
1398
+ return;
1399
+ if (!this._kvCacheInUse(chatId)) {
1400
+ await this._deleteChatKvCache(chatId, 'no mesh lists the chat');
1401
+ }
1402
+ }
1403
+ }
1404
+ async _knownChats() {
1405
+ const live = new Set();
1406
+ const read = new Set();
1407
+ const closed = new Set();
1408
+ for (const mesh of this._listed) {
1409
+ if (mesh.account)
1410
+ continue;
1411
+ if (!mesh.open) {
1412
+ closed.add(mesh.meshId);
1413
+ continue;
1414
+ }
1415
+ const { chats } = await this._engine.listChats({ meshId: mesh.meshId });
1416
+ read.add(mesh.meshId);
1417
+ for (const chat of chats ?? []) {
1418
+ live.add(chat.id);
1419
+ }
1420
+ }
1421
+ return { live, read, closed };
1422
+ }
1423
+ _kvCacheInUse(chatId) {
1424
+ for (const run of this._runs.values()) {
1425
+ if (run.chatId === chatId)
1426
+ return true;
1427
+ }
1428
+ return this._roundHost?.holds(chatId) ?? false;
1429
+ }
1366
1430
  async _clearPreReplayCaches() {
1367
1431
  if (await this._runReplay.started())
1368
1432
  return;
@@ -1453,10 +1517,22 @@ export class QvacAssistant extends ReadyResource {
1453
1517
  }
1454
1518
  async _watchMeshList() {
1455
1519
  this._meshList = this._engine.meshesWatch({});
1456
- for await (const { meshes } of this._meshList) {
1457
- if (this.closing || this.suspended)
1458
- return;
1459
- this._syncMeshConsumers(meshes ?? []);
1520
+ try {
1521
+ for await (const { meshes } of this._meshList) {
1522
+ if (this.closing || this.suspended)
1523
+ return;
1524
+ const before = this._listed;
1525
+ this._syncMeshConsumers(meshes ?? []);
1526
+ this._meshesSeen.resolve();
1527
+ const listed = new Set(this._listed.map((mesh) => mesh.meshId));
1528
+ const gone = new Set(before.filter((mesh) => !listed.has(mesh.meshId)).map((mesh) => mesh.meshId));
1529
+ if (gone.size > 0) {
1530
+ void this._reclaimOrphanedKvCaches(gone);
1531
+ }
1532
+ }
1533
+ }
1534
+ finally {
1535
+ this._meshesSeen.resolve();
1460
1536
  }
1461
1537
  }
1462
1538
  async _watchAccount() {
@@ -1475,11 +1551,9 @@ export class QvacAssistant extends ReadyResource {
1475
1551
  }
1476
1552
  _syncMeshConsumers(meshes) {
1477
1553
  this._listed = meshes;
1478
- const consumed = meshes.filter((mesh) => mesh.open && mesh.meshId !== this._ownWorkspace);
1479
- const open = new Set(consumed.map((mesh) => mesh.meshId));
1480
- const served = new Set(consumed.filter((mesh) => mesh.served).map((mesh) => mesh.meshId));
1554
+ const consumed = new Map(meshes.filter((mesh) => mesh.open).map((mesh) => [mesh.meshId, mesh]));
1481
1555
  for (const [meshId, consumer] of this._meshConsumers) {
1482
- if (open.has(meshId))
1556
+ if (consumed.has(meshId))
1483
1557
  continue;
1484
1558
  consumer.stop();
1485
1559
  this._meshConsumers.delete(meshId);
@@ -1489,18 +1563,14 @@ export class QvacAssistant extends ReadyResource {
1489
1563
  }
1490
1564
  this._servedChats.delete(meshId);
1491
1565
  }
1492
- for (const meshId of open) {
1493
- const consumer = this._meshConsumers.get(meshId) ?? this._meshConsumer(meshId, served.has(meshId));
1566
+ for (const [meshId, entry] of consumed) {
1567
+ const consumer = this._meshConsumers.get(meshId) ?? this._meshConsumer(entry);
1494
1568
  this._meshConsumers.set(meshId, consumer);
1495
1569
  consumer.start();
1496
1570
  }
1497
1571
  }
1498
1572
  _followAccount(account) {
1499
1573
  this._accountMeshId = account.meshId ?? null;
1500
- if ((account.ownWorkspace ?? null) !== this._ownWorkspace) {
1501
- this._ownWorkspace = account.ownWorkspace ?? null;
1502
- this._syncMeshConsumers(this._listed);
1503
- }
1504
1574
  const on = account.meshId ? `${account.meshId}:${account.writable}` : null;
1505
1575
  if (on === null) {
1506
1576
  this._advertisedOn = null;
@@ -1516,7 +1586,11 @@ export class QvacAssistant extends ReadyResource {
1516
1586
  void this._reconcileDeviceProfile();
1517
1587
  this._triggerModelWarm();
1518
1588
  }
1519
- _meshConsumer(meshId, served) {
1589
+ _meshConsumer({ meshId, served = false, account }) {
1590
+ if (account) {
1591
+ const agents = this._streamWatch(`agent:${meshId}`, () => this._engine.agentsWatch({ meshId }), (frames) => this._followAgentEdits(meshId, frames));
1592
+ return { start: () => agents.start(), stop: () => agents.stop() };
1593
+ }
1520
1594
  const watches = [
1521
1595
  this._streamWatch(`chat-list:${meshId}`, () => this._engine.chatsWatch({ meshId }), (frames) => this._reclaimDeletedChats(frames, meshId, served)),
1522
1596
  this._streamWatch(`kv-cache-retention:${meshId}`, () => this._engine.configWatch({ key: KV_CACHE_RETENTION_KEY }), (frames) => this._sweepIdleChatCaches(meshId, frames))
@@ -1926,6 +2000,7 @@ export class QvacAssistant extends ReadyResource {
1926
2000
  media: (file) => this._mediaRefs.get(path.basename(file)),
1927
2001
  inspect: (call) => void this._inspectRound(chatId, { runId, at: Date.now(), call })
1928
2002
  });
2003
+ await rounds.retire();
1929
2004
  const folded = !resumed &&
1930
2005
  (await this._compactIfNearFull({
1931
2006
  chatId,
@@ -2325,7 +2400,10 @@ export class QvacAssistant extends ReadyResource {
2325
2400
  const cacheKey = diagramRepair || stale.runIds.size > 0
2326
2401
  ? undefined
2327
2402
  : chatRepairKvCacheKey(chatId, acceptedRepairs);
2328
- sentRun = trackSentRun(cacheKey);
2403
+ sentRun = trackSentRun(cacheKey, (file) => wholeImageId(this._mediaRefs.get(path.basename(file))));
2404
+ if (cacheKey) {
2405
+ this._recordKvCache(meshId, chatId);
2406
+ }
2329
2407
  const unloading = this._unloading.get(model);
2330
2408
  if (unloading) {
2331
2409
  await raceAbort(unloading.catch(noop), controller.signal);
@@ -2376,6 +2454,7 @@ export class QvacAssistant extends ReadyResource {
2376
2454
  approve: idle.parks(approvals.approve),
2377
2455
  attach: (input) => this._uploadGenerated(input, { meshId, chatId, runId, deviceId }),
2378
2456
  fetchAttachment: (query) => this._fetchAttachment(engine, chatId, query),
2457
+ showImage: ({ attachmentId }) => this._showImage(engine, { chatId, runId, model, signal: controller.signal }, attachmentId),
2379
2458
  progress: (update) => {
2380
2459
  idle.touch();
2381
2460
  modelWatch.image(update);
@@ -3141,6 +3220,31 @@ export class QvacAssistant extends ReadyResource {
3141
3220
  }
3142
3221
  return unreadable;
3143
3222
  }
3223
+ async _showImage(engine, { chatId, runId, model, signal }, attachmentId) {
3224
+ const { chunks } = await engine.listChunks({ chatId, type: 'attachment' });
3225
+ const image = (chunks ?? []).find((chunk) => !chunk.sourceId && chunk.attachment?.id === attachmentId)?.attachment;
3226
+ if (!image || image.deletedAt || !IMAGE_MIMES.has(image.mimeType.toLowerCase())) {
3227
+ return null;
3228
+ }
3229
+ if (!(await this._seesImages(model))) {
3230
+ return null;
3231
+ }
3232
+ const pinned = this._runImageCacheNames.get(runId) ?? new Set();
3233
+ this._runImageCacheNames.set(runId, pinned);
3234
+ pinned.add(imageCacheName(image));
3235
+ const dir = this._imageCacheDir;
3236
+ await fs.promises.mkdir(dir, { recursive: true, mode: 0o700 });
3237
+ try {
3238
+ return await this._hydrateImage(dir, image, signal);
3239
+ }
3240
+ catch (err) {
3241
+ if (signal.aborted) {
3242
+ throw err;
3243
+ }
3244
+ this._log?.error('assistant', `image hydration failed — not shown ${image.fileName}`, err);
3245
+ return null;
3246
+ }
3247
+ }
3144
3248
  async _hydrateTurnImages(turn, { dir, signal }) {
3145
3249
  const dropped = [];
3146
3250
  for (const image of turn.images ?? []) {
@@ -23,7 +23,10 @@ export class Client extends ReadyResource {
23
23
  this._stream = await this._transport();
24
24
  const { bindApi, engineClient, HRPC } = await this._spec();
25
25
  const rpc = settleOnClose(new HRPC(this._stream));
26
- const declined = { storeIdentityPhrase: () => ({ ok: false }) };
26
+ const declined = {
27
+ storeIdentityPhrase: () => ({ ok: false }),
28
+ skillOauth: () => ({ phase: 'unavailable' })
29
+ };
27
30
  bindApi(rpc, { ...declined, ...this._handlers });
28
31
  const capabilities = engineClient(rpc);
29
32
  const modelDownloadsWatch = this._reconnecting(capabilities.modelDownloadsWatch, 'model-download');
@@ -1,4 +1,5 @@
1
- import { cancelRunsForAgent, softDeleteChatsForAgent } from './chat.js';
1
+ import { softDeleteChatsForAgent } from './chat.js';
2
+ import { cancelRunsForAgent } from './deleted-agent-runs.js';
2
3
  import { stripped } from './chat-copy.js';
3
4
  import { AGENTS } from './collections.js';
4
5
  async function putNewer(account, into, { id, copy }) {
@@ -1,10 +1,11 @@
1
1
  import b4a from 'b4a';
2
2
  import crypto from 'hypercore-crypto';
3
- import { readProjectHostKey } from './contract/project-host-keys.js';
3
+ import { projectHostKeys } from './contract/project-host-keys.js';
4
4
  import { listedProject, readProject } from './contract/projects.js';
5
5
  import { liveAgentsOn, readPublicSeed } from './contract/public-seeds.js';
6
6
  import { signWriterClaim } from './device-attestation.js';
7
7
  import { openSealedToDevice } from './device-seal.js';
8
+ import { seedOpens } from './project-keys.js';
8
9
  import { isRecord } from '../guards.js';
9
10
  import { AGENT_HOSTS, AGENT_USERS, AGENTS } from './collections.js';
10
11
  export function agentJoin(mesh, { device, writerKey, agentId, at }) {
@@ -25,9 +26,13 @@ async function targetOf(workspace, { userId, project }, device) {
25
26
  if (!row || row.deletedAt || (ours && listedProject(row, ours))) {
26
27
  return null;
27
28
  }
28
- const sealed = await readProjectHostKey(workspace.view, row.id, device.publicKey);
29
- const seed = sealed && openSealedToDevice(sealed.sealedSeed, device);
30
- return seed && { meshKey: row.id, seed, role: 'project' };
29
+ for (const sealed of await projectHostKeys(workspace.view, row.id, device.publicKey)) {
30
+ const seed = openSealedToDevice(sealed.sealedSeed, device);
31
+ if (seed && seedOpens(row, seed)) {
32
+ return { meshKey: row.id, seed, role: 'project' };
33
+ }
34
+ }
35
+ return null;
31
36
  }
32
37
  if (!ours || b4a.equals(userId, ours)) {
33
38
  return null;
@@ -1,4 +1,5 @@
1
- import { cancelRunsForAgent, softDeleteChatsForAgent } from './chat.js';
1
+ import { softDeleteChatsForAgent } from './chat.js';
2
+ import { cancelRunsForAgent } from './deleted-agent-runs.js';
2
3
  import { randomId } from './id.js';
3
4
  import { softDeleteToolConfigsForAgent } from './tool-configs.js';
4
5
  import b4a from 'b4a';
@@ -13,6 +13,7 @@ export interface ArchivedMeshContents extends ArchivedMeshCounts {
13
13
  readonly meshId: string;
14
14
  }
15
15
  export interface ArchivedMeshMigration extends ArchivedMeshContents {
16
+ readonly workspaceId: string;
16
17
  readonly migrated: boolean;
17
18
  readonly migratedAt: number;
18
19
  }
@@ -1,7 +1,7 @@
1
1
  import b4a from 'b4a';
2
2
  import crypto from 'hypercore-crypto';
3
3
  import { copyBlob, copyChunks, stripped } from './chat-copy.js';
4
- import { ownWorkspaceSeed, publicMeshKey, publicSeed } from './public-mesh.js';
4
+ import { publicMeshKey, publicSeed } from './public-mesh.js';
5
5
  import { Mesh } from './mesh.js';
6
6
  import { recordedRole } from './local/index.js';
7
7
  import { ACTIVE_AGENTS, ACTIVE_CHATS, ACTIVE_CHAT_GROUPS, ACTIVE_TOOL_CONFIGS_BY_AGENT, AGENTS, KNOWLEDGE, MESH_SEEDS } from './collections.js';
@@ -37,26 +37,23 @@ export async function withArchivedMesh(row, opts, fn) {
37
37
  await mesh.close();
38
38
  }
39
39
  }
40
- async function ownWorkspaceKey(account) {
41
- const seed = ownWorkspaceSeed(account.seed);
42
- const keys = [];
43
- for await (const row of account.view.find(MESH_SEEDS)) {
44
- if (b4a.equals(row.seed, seed)) {
45
- keys.push(row.meshKey);
40
+ async function migratedWorkspace(account, opts) {
41
+ for await (const { meshKey } of account.view.find(MESH_SEEDS)) {
42
+ const record = await opts.publicRecord?.(meshKey);
43
+ if (record?.migrated) {
44
+ return { workspaceKey: meshKey, record };
46
45
  }
47
46
  }
48
- return keys.sort(b4a.compare)[0] ?? null;
47
+ return null;
49
48
  }
50
49
  async function openArchivedPublic(account, opts) {
51
- const workspaceKey = await ownWorkspaceKey(account);
52
- const record = workspaceKey && (await opts.publicRecord?.(workspaceKey));
50
+ const migrated = await migratedWorkspace(account, opts);
51
+ const workspaceKey = migrated?.workspaceKey;
52
+ const record = migrated?.record;
53
53
  const key = record?.key
54
54
  ? b4a.from(record.key, 'hex')
55
55
  : workspaceKey && (await publicMeshKey(account, workspaceKey));
56
- if (!workspaceKey ||
57
- !record?.migrated ||
58
- !key ||
59
- !(await opts.storage.hasCore(crypto.discoveryKey(key)))) {
56
+ if (!workspaceKey || !record || !key || !(await opts.storage.hasCore(crypto.discoveryKey(key)))) {
60
57
  return null;
61
58
  }
62
59
  const mesh = new Mesh(opts.store, {
@@ -35,7 +35,7 @@ export declare function stripped<T extends {
35
35
  export interface ContentCopy extends MeshCopy {
36
36
  readonly deviceId: Buffer;
37
37
  readonly chats?: ReadonlySet<string>;
38
- readonly unlinkProjects?: boolean;
38
+ readonly endsOpenRuns?: boolean;
39
39
  }
40
40
  export declare function copyContent(copy: ContentCopy): Promise<void>;
41
41
  export declare const UPGRADED_RUN_REASON = "the app was updated before this was answered, send it again";