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

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 (108) 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 +5 -1
  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/device-routes.d.ts +1 -1
  8. package/dist-lib/lib/api/device-routes.js +3 -1
  9. package/dist-lib/lib/api/index.js +2 -1
  10. package/dist-lib/lib/api/projects.js +3 -4
  11. package/dist-lib/lib/api/served.d.ts +2 -2
  12. package/dist-lib/lib/api/served.js +24 -1
  13. package/dist-lib/lib/assistant/harness.d.ts +3 -0
  14. package/dist-lib/lib/assistant/history.js +14 -2
  15. package/dist-lib/lib/assistant/message.d.ts +1 -0
  16. package/dist-lib/lib/assistant/model-round.d.ts +3 -0
  17. package/dist-lib/lib/assistant/model-round.js +16 -2
  18. package/dist-lib/lib/assistant/round-host.d.ts +2 -0
  19. package/dist-lib/lib/assistant/round-host.js +8 -0
  20. package/dist-lib/lib/assistant-tree.d.ts +2 -0
  21. package/dist-lib/lib/assistant-tree.js +4 -2
  22. package/dist-lib/lib/assistant.d.ts +11 -1
  23. package/dist-lib/lib/assistant.js +115 -8
  24. package/dist-lib/lib/client.js +4 -1
  25. package/dist-lib/lib/core/agent-copies.js +2 -1
  26. package/dist-lib/lib/core/agent-hosting.js +9 -4
  27. package/dist-lib/lib/core/agent.js +2 -1
  28. package/dist-lib/lib/core/chat-copy.d.ts +1 -0
  29. package/dist-lib/lib/core/chat-copy.js +13 -2
  30. package/dist-lib/lib/core/chat.d.ts +0 -1
  31. package/dist-lib/lib/core/chat.js +0 -3
  32. package/dist-lib/lib/core/collections.d.ts +7 -7
  33. package/dist-lib/lib/core/collections.js +7 -7
  34. package/dist-lib/lib/core/contract/project-device-keys.d.ts +4 -2
  35. package/dist-lib/lib/core/contract/project-device-keys.js +12 -12
  36. package/dist-lib/lib/core/contract/project-host-keys.d.ts +5 -3
  37. package/dist-lib/lib/core/contract/project-host-keys.js +22 -11
  38. package/dist-lib/lib/core/contract/projects.d.ts +4 -2
  39. package/dist-lib/lib/core/contract/projects.js +29 -17
  40. package/dist-lib/lib/core/deleted-agent-runs.d.ts +1 -0
  41. package/dist-lib/lib/core/deleted-agent-runs.js +22 -11
  42. package/dist-lib/lib/core/helpers/index.d.ts +1 -1
  43. package/dist-lib/lib/core/helpers/index.js +2 -2
  44. package/dist-lib/lib/core/mesh.d.ts +1 -0
  45. package/dist-lib/lib/core/mesh.js +4 -4
  46. package/dist-lib/lib/core/project-keys.d.ts +5 -2
  47. package/dist-lib/lib/core/project-keys.js +15 -6
  48. package/dist-lib/lib/core/sync/index.js +5 -5
  49. package/dist-lib/lib/core.d.ts +2 -0
  50. package/dist-lib/lib/core.js +102 -25
  51. package/dist-lib/lib/kv-cache-ledger.d.ts +22 -0
  52. package/dist-lib/lib/kv-cache-ledger.js +72 -0
  53. package/dist-lib/lib/kv-cache-retention.d.ts +1 -0
  54. package/dist-lib/lib/kv-cache-retention.js +5 -2
  55. package/dist-lib/lib/model-memory-cap.d.ts +3 -0
  56. package/dist-lib/lib/model-memory-cap.js +15 -6
  57. package/dist-lib/lib/run-replay.d.ts +3 -2
  58. package/dist-lib/lib/run-replay.js +16 -5
  59. package/dist-lib/lib/run-transcript.js +1 -1
  60. package/dist-lib/schema/engine.js +54 -1
  61. package/dist-lib/schema/qvac.js +62 -1
  62. package/dist-lib/spec/bind.js +3 -0
  63. package/dist-lib/spec/calls.js +1 -0
  64. package/dist-lib/spec/capabilities.d.ts +2 -0
  65. package/dist-lib/spec/hrpc/hrpc.json +13 -0
  66. package/dist-lib/spec/hrpc/index.js +15 -3
  67. package/dist-lib/spec/hrpc/messages.js +253 -3
  68. package/dist-lib/spec/hyperschema/index.js +253 -3
  69. package/dist-lib/spec/hyperschema/schema.json +187 -0
  70. package/dist-lib/spec/hyperschema/types.d.ts +33 -0
  71. package/dist-lib/spec/mesh/hyperdb/db.json +110 -0
  72. package/dist-lib/spec/mesh/hyperdb/index.js +498 -2
  73. package/dist-lib/spec/mesh/hyperdb/messages.js +865 -410
  74. package/dist-lib/spec/mesh/hyperdispatch/messages.js +244 -3
  75. package/dist-lib/spec/mesh/hyperschema/index.js +244 -3
  76. package/dist-lib/spec/mesh/hyperschema/schema.json +155 -0
  77. package/dist-lib/spec/mesh/hyperschema/types.d.ts +37 -0
  78. package/package.json +27 -11
  79. package/tui/README.md +190 -0
  80. package/tui/app.mjs +8247 -0
  81. package/tui/boot.mjs +159 -0
  82. package/tui/index.mjs +89 -0
  83. package/tui/lib/boot-timing.mjs +41 -0
  84. package/tui/lib/busy-words.mjs +116 -0
  85. package/tui/lib/connect-sheet.mjs +350 -0
  86. package/tui/lib/device-target.mjs +64 -0
  87. package/tui/lib/export.mjs +194 -0
  88. package/tui/lib/folders-command.mjs +84 -0
  89. package/tui/lib/fork-command.mjs +43 -0
  90. package/tui/lib/knowledge-command.mjs +190 -0
  91. package/tui/lib/knowledge-import.mjs +153 -0
  92. package/tui/lib/local-path.mjs +21 -0
  93. package/tui/lib/move-command.mjs +17 -0
  94. package/tui/lib/render.mjs +225 -0
  95. package/tui/lib/save-file.mjs +88 -0
  96. package/tui/lib/select.mjs +81 -0
  97. package/tui/lib/selection.mjs +18 -0
  98. package/tui/lib/setup.mjs +135 -0
  99. package/tui/lib/sidebar.mjs +63 -0
  100. package/tui/lib/skill-connect.mjs +145 -0
  101. package/tui/lib/splash-art.mjs +107 -0
  102. package/tui/lib/splash.mjs +18 -0
  103. package/tui/lib/upload-frames.mjs +9 -0
  104. package/tui/lib/usage.mjs +59 -0
  105. package/tui/lib/wrap.mjs +143 -0
  106. package/tui/main.mjs +279 -0
  107. package/tui/plugins.mjs +158 -0
  108. package/worker/assistant-sidecar.mjs +1 -0
@@ -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;
@@ -75,6 +76,7 @@ export declare class QvacAssistant extends ReadyResource {
75
76
  private _reconciling;
76
77
  private readonly _onlineBoot;
77
78
  private readonly _accountSeen;
79
+ private readonly _meshesSeen;
78
80
  private _capability;
79
81
  private _downloads;
80
82
  private _state;
@@ -101,6 +103,8 @@ export declare class QvacAssistant extends ReadyResource {
101
103
  private readonly _runReplay;
102
104
  private readonly _roundHost;
103
105
  private readonly _kvRetention;
106
+ private readonly _kvLedger;
107
+ private _orphanSweep;
104
108
  private readonly _lifecycle;
105
109
  private readonly _suggesting;
106
110
  private readonly _audioTranscripts;
@@ -115,7 +119,7 @@ export declare class QvacAssistant extends ReadyResource {
115
119
  private readonly _modelCatalog;
116
120
  private readonly _onHarnessStatus;
117
121
  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);
122
+ constructor(engine: Capabilities, harness: Harness, { logger, imageCacheDir, inferenceCallsDir, runReplayDir, kvCacheChatsDir, imageCacheMaxBytes, imageCacheMaxAgeMs, kvCacheRetentionMs, downloadRetryMs, platform, preloadCurrentAgentModel, drives, blobReadTimeoutMs, runIdleTimeoutMs, modelReloadDelayMs, pdfRasterizer: pdfRasterizerOption }?: AssistantOptions);
119
123
  private _requireDeviceId;
120
124
  _open(): Promise<void>;
121
125
  private _triggerModelWarm;
@@ -185,6 +189,11 @@ export declare class QvacAssistant extends ReadyResource {
185
189
  deleteChatKvCache(chatId: string): Promise<AssistantKvCacheDeletionResponse>;
186
190
  private _reclaimChatKvCache;
187
191
  clearKvCache(): Promise<AssistantKvCacheDeletionResponse>;
192
+ private _recordKvCache;
193
+ private _reclaimOrphanedKvCaches;
194
+ private _sweepOrphanedKvCaches;
195
+ private _knownChats;
196
+ private _kvCacheInUse;
188
197
  private _clearPreReplayCaches;
189
198
  private _inspectCall;
190
199
  private _inspectRound;
@@ -237,6 +246,7 @@ export declare class QvacAssistant extends ReadyResource {
237
246
  private _openVideo;
238
247
  private _hearVideo;
239
248
  private _hydrateImages;
249
+ private _showImage;
240
250
  private _hydrateTurnImages;
241
251
  private _servedMedia;
242
252
  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';
@@ -289,6 +290,7 @@ export class QvacAssistant extends ReadyResource {
289
290
  _reconciling;
290
291
  _onlineBoot = new ReadyGuard();
291
292
  _accountSeen = Promise.withResolvers();
293
+ _meshesSeen = Promise.withResolvers();
292
294
  _capability;
293
295
  _downloads;
294
296
  _state;
@@ -315,6 +317,8 @@ export class QvacAssistant extends ReadyResource {
315
317
  _runReplay;
316
318
  _roundHost;
317
319
  _kvRetention;
320
+ _kvLedger;
321
+ _orphanSweep = Promise.resolve();
318
322
  _lifecycle;
319
323
  _suggesting = new Map();
320
324
  _audioTranscripts;
@@ -329,7 +333,7 @@ export class QvacAssistant extends ReadyResource {
329
333
  _modelCatalog = new ModelCatalogRevisionHub();
330
334
  _onHarnessStatus;
331
335
  _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 } = {}) {
336
+ 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
337
  super();
334
338
  this._engine = engine;
335
339
  this._harness = harness;
@@ -356,6 +360,7 @@ export class QvacAssistant extends ReadyResource {
356
360
  resolve: (sources, served) => this._servedMedia(sources, served),
357
361
  release: (id) => this._runImageCacheNames.delete(`round:${id}`)
358
362
  },
363
+ built: (meshId, chatId) => this._recordKvCache(meshId, chatId),
359
364
  log: logger
360
365
  })
361
366
  : null;
@@ -382,9 +387,10 @@ export class QvacAssistant extends ReadyResource {
382
387
  this._backgroundReplays = new Set();
383
388
  this._inferenceCalls = new InferenceCallStore(inferenceCallsDir ?? persistentDir('inspector'));
384
389
  this._runReplay = new RunReplayStore(runReplayDir ?? persistentDir('run-replay'));
390
+ this._kvLedger = new KvCacheLedger(kvCacheChatsDir ?? persistentDir('kv-cache-chats'));
385
391
  this._kvRetention = new KvCacheRetention(engine, {
386
392
  active: () => !this.closing && !this.suspended,
387
- running: (chatId) => [...this._runs.values()].some((run) => run.chatId === chatId),
393
+ running: (chatId) => this._kvCacheInUse(chatId),
388
394
  deleteChatKvCache: (chatId) => this._deleteChatKvCache(chatId, 'idle past retention'),
389
395
  defaultMs: kvCacheRetentionMs,
390
396
  logger
@@ -542,6 +548,8 @@ export class QvacAssistant extends ReadyResource {
542
548
  finally {
543
549
  this._onlineBoot.exit();
544
550
  }
551
+ await this._meshesSeen.promise;
552
+ await this._reclaimOrphanedKvCaches();
545
553
  }
546
554
  async onlineReady() {
547
555
  if (!this.opened)
@@ -1353,6 +1361,7 @@ export class QvacAssistant extends ReadyResource {
1353
1361
  };
1354
1362
  if (result.status === 'success') {
1355
1363
  await this._runReplay.forget(chatId, { holder: LOCAL_HOLDER, reason });
1364
+ await this._kvLedger.forget(chatId);
1356
1365
  }
1357
1366
  return result;
1358
1367
  }
@@ -1360,9 +1369,65 @@ export class QvacAssistant extends ReadyResource {
1360
1369
  const result = await harnessClearKvCache(this._harness)();
1361
1370
  if (result.status === 'success') {
1362
1371
  await this._runReplay.clear(LOCAL_HOLDER);
1372
+ await this._kvLedger.clear();
1363
1373
  }
1364
1374
  return result;
1365
1375
  }
1376
+ _recordKvCache(meshId, chatId) {
1377
+ const served = this._listed.some((mesh) => mesh.meshId === meshId && mesh.served);
1378
+ void this._kvLedger
1379
+ .record({ meshId, chatId, served })
1380
+ .catch((err) => this._log?.warn('assistant', 'kv-cache chat not recorded', { chatId, err }));
1381
+ }
1382
+ _reclaimOrphanedKvCaches(gone = new Set()) {
1383
+ this._orphanSweep = this._orphanSweep
1384
+ .then(() => this._sweepOrphanedKvCaches(gone))
1385
+ .catch((err) => this._log?.warn('assistant', 'orphaned kv-cache sweep failed', { err }));
1386
+ return this._orphanSweep;
1387
+ }
1388
+ async _sweepOrphanedKvCaches(gone) {
1389
+ if (this.closing || this.suspended)
1390
+ return;
1391
+ if ((await this._kvRetention.retentionMs()) === null)
1392
+ return;
1393
+ const entries = await this._kvLedger.entries();
1394
+ if (entries.length === 0) {
1395
+ return;
1396
+ }
1397
+ for (const chatId of orphanedChats(entries, { ...(await this._knownChats()), gone })) {
1398
+ if (this.closing || this.suspended)
1399
+ return;
1400
+ if (!this._kvCacheInUse(chatId)) {
1401
+ await this._deleteChatKvCache(chatId, 'no mesh lists the chat');
1402
+ }
1403
+ }
1404
+ }
1405
+ async _knownChats() {
1406
+ const live = new Set();
1407
+ const read = new Set();
1408
+ const closed = new Set();
1409
+ for (const mesh of this._listed) {
1410
+ if (mesh.meshId === this._ownWorkspace)
1411
+ continue;
1412
+ if (!mesh.open) {
1413
+ closed.add(mesh.meshId);
1414
+ continue;
1415
+ }
1416
+ const { chats } = await this._engine.listChats({ meshId: mesh.meshId });
1417
+ read.add(mesh.meshId);
1418
+ for (const chat of chats ?? []) {
1419
+ live.add(chat.id);
1420
+ }
1421
+ }
1422
+ return { live, read, closed };
1423
+ }
1424
+ _kvCacheInUse(chatId) {
1425
+ for (const run of this._runs.values()) {
1426
+ if (run.chatId === chatId)
1427
+ return true;
1428
+ }
1429
+ return this._roundHost?.holds(chatId) ?? false;
1430
+ }
1366
1431
  async _clearPreReplayCaches() {
1367
1432
  if (await this._runReplay.started())
1368
1433
  return;
@@ -1453,10 +1518,22 @@ export class QvacAssistant extends ReadyResource {
1453
1518
  }
1454
1519
  async _watchMeshList() {
1455
1520
  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 ?? []);
1521
+ try {
1522
+ for await (const { meshes } of this._meshList) {
1523
+ if (this.closing || this.suspended)
1524
+ return;
1525
+ const before = this._listed;
1526
+ this._syncMeshConsumers(meshes ?? []);
1527
+ this._meshesSeen.resolve();
1528
+ const listed = new Set(this._listed.map((mesh) => mesh.meshId));
1529
+ const gone = new Set(before.filter((mesh) => !listed.has(mesh.meshId)).map((mesh) => mesh.meshId));
1530
+ if (gone.size > 0) {
1531
+ void this._reclaimOrphanedKvCaches(gone);
1532
+ }
1533
+ }
1534
+ }
1535
+ finally {
1536
+ this._meshesSeen.resolve();
1460
1537
  }
1461
1538
  }
1462
1539
  async _watchAccount() {
@@ -1926,6 +2003,7 @@ export class QvacAssistant extends ReadyResource {
1926
2003
  media: (file) => this._mediaRefs.get(path.basename(file)),
1927
2004
  inspect: (call) => void this._inspectRound(chatId, { runId, at: Date.now(), call })
1928
2005
  });
2006
+ await rounds.retire();
1929
2007
  const folded = !resumed &&
1930
2008
  (await this._compactIfNearFull({
1931
2009
  chatId,
@@ -2325,7 +2403,10 @@ export class QvacAssistant extends ReadyResource {
2325
2403
  const cacheKey = diagramRepair || stale.runIds.size > 0
2326
2404
  ? undefined
2327
2405
  : chatRepairKvCacheKey(chatId, acceptedRepairs);
2328
- sentRun = trackSentRun(cacheKey);
2406
+ sentRun = trackSentRun(cacheKey, (file) => wholeImageId(this._mediaRefs.get(path.basename(file))));
2407
+ if (cacheKey) {
2408
+ this._recordKvCache(meshId, chatId);
2409
+ }
2329
2410
  const unloading = this._unloading.get(model);
2330
2411
  if (unloading) {
2331
2412
  await raceAbort(unloading.catch(noop), controller.signal);
@@ -2376,6 +2457,7 @@ export class QvacAssistant extends ReadyResource {
2376
2457
  approve: idle.parks(approvals.approve),
2377
2458
  attach: (input) => this._uploadGenerated(input, { meshId, chatId, runId, deviceId }),
2378
2459
  fetchAttachment: (query) => this._fetchAttachment(engine, chatId, query),
2460
+ showImage: ({ attachmentId }) => this._showImage(engine, { chatId, runId, model, signal: controller.signal }, attachmentId),
2379
2461
  progress: (update) => {
2380
2462
  idle.touch();
2381
2463
  modelWatch.image(update);
@@ -3141,6 +3223,31 @@ export class QvacAssistant extends ReadyResource {
3141
3223
  }
3142
3224
  return unreadable;
3143
3225
  }
3226
+ async _showImage(engine, { chatId, runId, model, signal }, attachmentId) {
3227
+ const { chunks } = await engine.listChunks({ chatId, type: 'attachment' });
3228
+ const image = (chunks ?? []).find((chunk) => !chunk.sourceId && chunk.attachment?.id === attachmentId)?.attachment;
3229
+ if (!image || image.deletedAt || !IMAGE_MIMES.has(image.mimeType.toLowerCase())) {
3230
+ return null;
3231
+ }
3232
+ if (!(await this._seesImages(model))) {
3233
+ return null;
3234
+ }
3235
+ const pinned = this._runImageCacheNames.get(runId) ?? new Set();
3236
+ this._runImageCacheNames.set(runId, pinned);
3237
+ pinned.add(imageCacheName(image));
3238
+ const dir = this._imageCacheDir;
3239
+ await fs.promises.mkdir(dir, { recursive: true, mode: 0o700 });
3240
+ try {
3241
+ return await this._hydrateImage(dir, image, signal);
3242
+ }
3243
+ catch (err) {
3244
+ if (signal.aborted) {
3245
+ throw err;
3246
+ }
3247
+ this._log?.error('assistant', `image hydration failed — not shown ${image.fileName}`, err);
3248
+ return null;
3249
+ }
3250
+ }
3144
3251
  async _hydrateTurnImages(turn, { dir, signal }) {
3145
3252
  const dropped = [];
3146
3253
  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';
@@ -36,6 +36,7 @@ export interface ContentCopy extends MeshCopy {
36
36
  readonly deviceId: Buffer;
37
37
  readonly chats?: ReadonlySet<string>;
38
38
  readonly unlinkProjects?: boolean;
39
+ readonly endsOpenRuns?: boolean;
39
40
  }
40
41
  export declare function copyContent(copy: ContentCopy): Promise<void>;
41
42
  export declare const UPGRADED_RUN_REASON = "the app was updated before this was answered, send it again";
@@ -266,7 +266,7 @@ async function copyChatChunks(snap, chatId, copy) {
266
266
  const { into } = copy;
267
267
  const batch = into.batch();
268
268
  const chunks = await chunksOf(snap, chatId);
269
- const open = copy.chats ? runsOf(chunks).open : new Set();
269
+ const open = copy.endsOpenRuns ? runsOf(chunks).open : new Set();
270
270
  for (const chunk of chunks) {
271
271
  const held = await into.view.get(CHUNKS, { id: chunk.id });
272
272
  if (held) {
@@ -275,7 +275,8 @@ async function copyChatChunks(snap, chatId, copy) {
275
275
  continue;
276
276
  }
277
277
  const { seq, updatedAt, attachment, runnerDeviceId, ...rest } = stripped(chunk);
278
- const op = copy.chats ? { ...rest, senderMemberId: await senderOf(snap, chunk) } : rest;
278
+ const sent = copy.chats ? { ...rest, senderMemberId: await senderOf(snap, chunk) } : rest;
279
+ const op = deniedWhileOpen(sent, open);
279
280
  if (!attachment) {
280
281
  batch.dispatch('append-chunk', op);
281
282
  if (chunk.type === 'run-request' && chunk.runId && open.has(chunk.runId)) {
@@ -295,6 +296,16 @@ async function copyChatChunks(snap, chatId, copy) {
295
296
  }
296
297
  await batch.flush();
297
298
  }
299
+ function deniedWhileOpen(op, open) {
300
+ const ask = op.approvalRequest;
301
+ if (op.type !== 'approval-request' ||
302
+ ask?.status !== 'pending' ||
303
+ !op.runId ||
304
+ !open.has(op.runId)) {
305
+ return op;
306
+ }
307
+ return { ...op, approvalRequest: { ...ask, status: 'denied' } };
308
+ }
298
309
  export const UPGRADED_RUN_REASON = 'the app was updated before this was answered, send it again';
299
310
  async function endUpgradedRun(request, copy, batch) {
300
311
  await appendChunk(copy.into, {
@@ -75,4 +75,3 @@ export declare function removeChatAttachments(store: Mesh, chatId: string, { dev
75
75
  export declare function deleteChat(store: Mesh, deviceId: Buffer | null, id: string): Promise<ChatListing>;
76
76
  export declare function softDeleteChatsForAgent(store: Mesh, deviceId: Buffer | null, agentId: string): Promise<void>;
77
77
  export declare const AGENT_DELETED = "agent-deleted";
78
- export declare function cancelRunsForAgent(store: Mesh, deviceId: Buffer, agentId: string): Promise<void>;
@@ -358,9 +358,6 @@ export function softDeleteChatsForAgent(store, deviceId, agentId) {
358
358
  return eachChatOfAgent(store, agentId, (chatId) => deleteChat(store, deviceId, chatId));
359
359
  }
360
360
  export const AGENT_DELETED = 'agent-deleted';
361
- export function cancelRunsForAgent(store, deviceId, agentId) {
362
- return eachChatOfAgent(store, agentId, (chatId) => cancelActiveRuns(store, chatId, { deviceId, agentId, reason: AGENT_DELETED }));
363
- }
364
361
  async function eachChatOfAgent(store, agentId, fn) {
365
362
  const snap = store.view.snapshot();
366
363
  try {
@@ -74,11 +74,11 @@ export declare const DEVICES_BY_MEMBER = "@qvac/devices-by-member";
74
74
  export declare const AGENT_HOSTS_BY_AGENT = "@qvac/agent-hosts-by-agent";
75
75
  export declare const AGENT_HOSTS_BY_DEVICE = "@qvac/agent-hosts-by-device";
76
76
  export declare const PUBLIC_SEEDS_BY_DEVICE = "@qvac/public-seeds-by-device";
77
- export declare const PROJECT_HOST_KEYS_BY_DEVICE = "@qvac/project-host-keys-by-device";
78
- export declare const PROJECT_DEVICE_KEYS_BY_DEVICE = "@qvac/project-device-keys-by-device";
79
- export declare const PROJECT_DEVICE_KEYS_BY_MEMBER = "@qvac/project-device-keys-by-member";
77
+ export declare const PROJECT_HOST_SEALS_BY_DEVICE = "@qvac/project-host-seals-by-device";
78
+ export declare const PROJECT_DEVICE_SEALS_BY_DEVICE = "@qvac/project-device-seals-by-device";
79
+ export declare const PROJECT_DEVICE_SEALS_BY_MEMBER = "@qvac/project-device-seals-by-member";
80
80
  export declare const AGENT_USERS_BY_USER = "@qvac/agent-users-by-user";
81
- export declare const PROJECT_KEYS_BY_MEMBER = "@qvac/project-keys-by-member";
81
+ export declare const PROJECT_MEMBER_SEALS_BY_MEMBER = "@qvac/project-member-seals-by-member";
82
82
  export declare const TERMINAL_RUN_STATUSES = "@qvac/terminal-run-statuses";
83
83
  export declare const MEMBER_REMOVALS = "@qvac/member-removals";
84
84
  export declare const INVITE_SEQUENCES = "@qvac/invite-sequences";
@@ -103,7 +103,7 @@ export declare const LIVE_MODEL_REQUESTS = "@qvac/live-model-requests";
103
103
  export declare const AGENT_HOSTS = "@qvac/agent-hosts";
104
104
  export declare const AGENT_USERS = "@qvac/agent-users";
105
105
  export declare const MEMBER_KEYS = "@qvac/member-keys";
106
- export declare const PROJECT_KEYS = "@qvac/project-keys";
107
- export declare const PROJECT_HOST_KEYS = "@qvac/project-host-keys";
108
- export declare const PROJECT_DEVICE_KEYS = "@qvac/project-device-keys";
106
+ export declare const PROJECT_MEMBER_SEALS = "@qvac/project-member-seals";
107
+ export declare const PROJECT_HOST_SEALS = "@qvac/project-host-seals";
108
+ export declare const PROJECT_DEVICE_SEALS = "@qvac/project-device-seals";
109
109
  export declare const SKILL_SECRETS = "@qvac/skill-secrets";
@@ -74,11 +74,11 @@ export const DEVICES_BY_MEMBER = '@qvac/devices-by-member';
74
74
  export const AGENT_HOSTS_BY_AGENT = '@qvac/agent-hosts-by-agent';
75
75
  export const AGENT_HOSTS_BY_DEVICE = '@qvac/agent-hosts-by-device';
76
76
  export const PUBLIC_SEEDS_BY_DEVICE = '@qvac/public-seeds-by-device';
77
- export const PROJECT_HOST_KEYS_BY_DEVICE = '@qvac/project-host-keys-by-device';
78
- export const PROJECT_DEVICE_KEYS_BY_DEVICE = '@qvac/project-device-keys-by-device';
79
- export const PROJECT_DEVICE_KEYS_BY_MEMBER = '@qvac/project-device-keys-by-member';
77
+ export const PROJECT_HOST_SEALS_BY_DEVICE = '@qvac/project-host-seals-by-device';
78
+ export const PROJECT_DEVICE_SEALS_BY_DEVICE = '@qvac/project-device-seals-by-device';
79
+ export const PROJECT_DEVICE_SEALS_BY_MEMBER = '@qvac/project-device-seals-by-member';
80
80
  export const AGENT_USERS_BY_USER = '@qvac/agent-users-by-user';
81
- export const PROJECT_KEYS_BY_MEMBER = '@qvac/project-keys-by-member';
81
+ export const PROJECT_MEMBER_SEALS_BY_MEMBER = '@qvac/project-member-seals-by-member';
82
82
  export const TERMINAL_RUN_STATUSES = '@qvac/terminal-run-statuses';
83
83
  export const MEMBER_REMOVALS = '@qvac/member-removals';
84
84
  export const INVITE_SEQUENCES = '@qvac/invite-sequences';
@@ -103,7 +103,7 @@ export const LIVE_MODEL_REQUESTS = '@qvac/live-model-requests';
103
103
  export const AGENT_HOSTS = '@qvac/agent-hosts';
104
104
  export const AGENT_USERS = '@qvac/agent-users';
105
105
  export const MEMBER_KEYS = '@qvac/member-keys';
106
- export const PROJECT_KEYS = '@qvac/project-keys';
107
- export const PROJECT_HOST_KEYS = '@qvac/project-host-keys';
108
- export const PROJECT_DEVICE_KEYS = '@qvac/project-device-keys';
106
+ export const PROJECT_MEMBER_SEALS = '@qvac/project-member-seals';
107
+ export const PROJECT_HOST_SEALS = '@qvac/project-host-seals';
108
+ export const PROJECT_DEVICE_SEALS = '@qvac/project-device-seals';
109
109
  export const SKILL_SECRETS = '@qvac/skill-secrets';
@@ -1,6 +1,8 @@
1
1
  import type HyperDB from 'hyperdb';
2
- import type { QvacProjectDeviceKey } from '../../../spec/mesh/hyperschema/types.d.ts';
3
- export declare function readProjectDeviceKey(tx: HyperDB, projectId: Buffer, deviceId: Buffer): Promise<QvacProjectDeviceKey | null>;
2
+ import type { QvacProjectDeviceSeal } from '../../../spec/mesh/hyperschema/types.d.ts';
3
+ export type DeviceSealSlot = Pick<QvacProjectDeviceSeal, 'projectId' | 'deviceId' | 'sealedBy'>;
4
+ export declare function readProjectDeviceKey(tx: HyperDB, { projectId, deviceId, sealedBy }: DeviceSealSlot): Promise<QvacProjectDeviceSeal | null>;
5
+ export declare function projectDeviceKeys(tx: HyperDB, projectId: Buffer, deviceId?: Buffer): Promise<QvacProjectDeviceSeal[]>;
4
6
  export declare function dropProjectDeviceKeys(tx: HyperDB, projectId: Buffer): Promise<void>;
5
7
  export declare function dropListingDeviceKeys(tx: HyperDB, projectId: Buffer, memberId: Buffer): Promise<void>;
6
8
  export declare function dropMemberDeviceKeys(tx: HyperDB, memberId: Buffer): Promise<void>;
@@ -1,26 +1,26 @@
1
1
  import b4a from 'b4a';
2
- import { PROJECT_DEVICE_KEYS, PROJECT_DEVICE_KEYS_BY_DEVICE, PROJECT_DEVICE_KEYS_BY_MEMBER } from '../collections.js';
3
- export function readProjectDeviceKey(tx, projectId, deviceId) {
4
- return tx.get(PROJECT_DEVICE_KEYS, { projectId, deviceId });
2
+ import { PROJECT_DEVICE_SEALS, PROJECT_DEVICE_SEALS_BY_DEVICE, PROJECT_DEVICE_SEALS_BY_MEMBER } from '../collections.js';
3
+ export function readProjectDeviceKey(tx, { projectId, deviceId, sealedBy }) {
4
+ return tx.get(PROJECT_DEVICE_SEALS, { projectId, deviceId, sealedBy });
5
5
  }
6
- function deviceKeysOf(tx, projectId) {
7
- const range = { gte: { projectId }, lte: { projectId } };
8
- return tx.find(PROJECT_DEVICE_KEYS, range).toArray();
6
+ export function projectDeviceKeys(tx, projectId, deviceId) {
7
+ const key = deviceId ? { projectId, deviceId } : { projectId };
8
+ return tx.find(PROJECT_DEVICE_SEALS, { gte: key, lte: key }).toArray();
9
9
  }
10
10
  async function drop(tx, rows, match) {
11
- for (const row of rows.filter(match)) {
12
- await tx.delete(PROJECT_DEVICE_KEYS, { projectId: row.projectId, deviceId: row.deviceId });
11
+ for (const { projectId, deviceId, sealedBy } of rows.filter(match)) {
12
+ await tx.delete(PROJECT_DEVICE_SEALS, { projectId, deviceId, sealedBy });
13
13
  }
14
14
  }
15
15
  export async function dropProjectDeviceKeys(tx, projectId) {
16
- await drop(tx, await deviceKeysOf(tx, projectId), () => true);
16
+ await drop(tx, await projectDeviceKeys(tx, projectId), () => true);
17
17
  }
18
18
  export async function dropListingDeviceKeys(tx, projectId, memberId) {
19
- await drop(tx, await deviceKeysOf(tx, projectId), (row) => b4a.equals(row.memberId, memberId));
19
+ await drop(tx, await projectDeviceKeys(tx, projectId), (row) => [row.memberId, row.sealedBy].some((id) => b4a.equals(id, memberId)));
20
20
  }
21
21
  export async function dropMemberDeviceKeys(tx, memberId) {
22
22
  const rows = await tx
23
- .find(PROJECT_DEVICE_KEYS_BY_MEMBER, {
23
+ .find(PROJECT_DEVICE_SEALS_BY_MEMBER, {
24
24
  gte: { memberId },
25
25
  lte: { memberId }
26
26
  })
@@ -29,7 +29,7 @@ export async function dropMemberDeviceKeys(tx, memberId) {
29
29
  }
30
30
  export async function dropDeviceKeysOf(tx, deviceId) {
31
31
  const rows = await tx
32
- .find(PROJECT_DEVICE_KEYS_BY_DEVICE, {
32
+ .find(PROJECT_DEVICE_SEALS_BY_DEVICE, {
33
33
  gte: { deviceId },
34
34
  lte: { deviceId }
35
35
  })
@@ -1,7 +1,9 @@
1
1
  import type HyperDB from 'hyperdb';
2
- import type { QvacProjectHostKey } from '../../../spec/mesh/hyperschema/types.d.ts';
3
- export declare function readProjectHostKey(tx: HyperDB, projectId: Buffer, deviceId: Buffer): Promise<QvacProjectHostKey | null>;
2
+ import type { QvacProjectHostSeal, QvacSetProjectListingOperation } from '../../../spec/mesh/hyperschema/types.d.ts';
3
+ export type HostKeySlot = Pick<QvacProjectHostSeal, 'projectId' | 'deviceId' | 'sealedBy'>;
4
+ export declare function readProjectHostKey(tx: HyperDB, { projectId, deviceId, sealedBy }: HostKeySlot): Promise<QvacProjectHostSeal | null>;
5
+ export declare function projectHostKeys(tx: HyperDB, projectId: Buffer, deviceId: Buffer): Promise<QvacProjectHostSeal[]>;
4
6
  export declare function dropProjectHostKeys(tx: HyperDB, projectId: Buffer): Promise<void>;
5
- export declare function dropMemberHostKeys(tx: HyperDB, projectId: Buffer, memberId: Buffer): Promise<void>;
7
+ export declare function dropMemberHostKeys(tx: HyperDB, { projectId, memberId, removed }: Omit<QvacSetProjectListingOperation, 'removedAt'>): Promise<void>;
6
8
  export declare function dropDeviceHostKeys(tx: HyperDB, deviceId: Buffer): Promise<void>;
7
9
  export declare function pruneProjectHostKeys(tx: HyperDB, deviceId: Buffer): Promise<void>;
@@ -1,41 +1,52 @@
1
1
  import b4a from 'b4a';
2
- import { DEVICES, PROJECT_HOST_KEYS, PROJECT_HOST_KEYS_BY_DEVICE } from '../collections.js';
2
+ import { DEVICES, PROJECT_HOST_SEALS, PROJECT_HOST_SEALS_BY_DEVICE } from '../collections.js';
3
3
  import { liveAgentsOn, usesAny } from './public-seeds.js';
4
- export function readProjectHostKey(tx, projectId, deviceId) {
5
- return tx.get(PROJECT_HOST_KEYS, { projectId, deviceId });
4
+ export function readProjectHostKey(tx, { projectId, deviceId, sealedBy }) {
5
+ return tx.get(PROJECT_HOST_SEALS, { projectId, deviceId, sealedBy });
6
+ }
7
+ export function projectHostKeys(tx, projectId, deviceId) {
8
+ const range = { gte: { projectId, deviceId }, lte: { projectId, deviceId } };
9
+ return tx.find(PROJECT_HOST_SEALS, range).toArray();
10
+ }
11
+ function dropRow(tx, { projectId, deviceId, sealedBy }) {
12
+ return tx.delete(PROJECT_HOST_SEALS, { projectId, deviceId, sealedBy });
6
13
  }
7
14
  function hostKeysOf(tx, projectId) {
8
15
  const range = { gte: { projectId }, lte: { projectId } };
9
- return tx.find(PROJECT_HOST_KEYS, range).toArray();
16
+ return tx.find(PROJECT_HOST_SEALS, range).toArray();
10
17
  }
11
18
  export async function dropProjectHostKeys(tx, projectId) {
12
19
  for (const row of await hostKeysOf(tx, projectId)) {
13
- await tx.delete(PROJECT_HOST_KEYS, { projectId, deviceId: row.deviceId });
20
+ await dropRow(tx, row);
14
21
  }
15
22
  }
16
- export async function dropMemberHostKeys(tx, projectId, memberId) {
23
+ export async function dropMemberHostKeys(tx, { projectId, memberId, removed }) {
17
24
  for (const row of await hostKeysOf(tx, projectId)) {
18
25
  const device = await tx.get(DEVICES, { id: row.deviceId });
19
- if (device?.memberId && b4a.equals(device.memberId, memberId)) {
20
- await tx.delete(PROJECT_HOST_KEYS, { projectId, deviceId: row.deviceId });
26
+ const hosted = !!device?.memberId && b4a.equals(device.memberId, memberId);
27
+ if (hosted || (removed && b4a.equals(row.sealedBy, memberId))) {
28
+ await dropRow(tx, row);
21
29
  }
22
30
  }
23
31
  }
24
32
  function hostKeysSealedTo(tx, deviceId) {
25
33
  return tx
26
- .find(PROJECT_HOST_KEYS_BY_DEVICE, { gte: { deviceId }, lte: { deviceId } })
34
+ .find(PROJECT_HOST_SEALS_BY_DEVICE, {
35
+ gte: { deviceId },
36
+ lte: { deviceId }
37
+ })
27
38
  .toArray();
28
39
  }
29
40
  export async function dropDeviceHostKeys(tx, deviceId) {
30
41
  for (const row of await hostKeysSealedTo(tx, deviceId)) {
31
- await tx.delete(PROJECT_HOST_KEYS, { projectId: row.projectId, deviceId });
42
+ await dropRow(tx, row);
32
43
  }
33
44
  }
34
45
  export async function pruneProjectHostKeys(tx, deviceId) {
35
46
  const agents = await liveAgentsOn(tx, deviceId);
36
47
  for (const row of await hostKeysSealedTo(tx, deviceId)) {
37
48
  if (!(await usesAny(tx, agents, row.projectId))) {
38
- await tx.delete(PROJECT_HOST_KEYS, { projectId: row.projectId, deviceId });
49
+ await dropRow(tx, row);
39
50
  }
40
51
  }
41
52
  }
@@ -1,11 +1,13 @@
1
1
  import type HyperDB from 'hyperdb';
2
2
  import type { Router } from '../../../spec/mesh/hyperdispatch/index.js';
3
- import type { QvacProject, QvacProjectKey, QvacProjectMember } from '../../../spec/mesh/hyperschema/types.d.ts';
3
+ import type { QvacProject, QvacProjectMemberSeal, QvacProjectMember } from '../../../spec/mesh/hyperschema/types.d.ts';
4
4
  import type { RouterContext } from './types.ts';
5
5
  type Signed = Pick<RouterContext, 'tx' | 'node'>;
6
6
  export declare const UNGROUPED = "@qvac/ungrouped";
7
7
  export declare function readProject(tx: HyperDB, id: Buffer): Promise<QvacProject | null>;
8
- export declare function readProjectKey(tx: HyperDB, projectId: Buffer, memberId: Buffer): Promise<QvacProjectKey | null>;
8
+ export type MemberSealSlot = Pick<QvacProjectMemberSeal, 'projectId' | 'memberId' | 'sealedBy'>;
9
+ export declare function readProjectKey(tx: HyperDB, { projectId, memberId, sealedBy }: MemberSealSlot): Promise<QvacProjectMemberSeal | null>;
10
+ export declare function projectKeys(tx: HyperDB, projectId: Buffer, memberId?: Buffer): Promise<QvacProjectMemberSeal[]>;
9
11
  export declare function dropMemberProjectKeys(tx: HyperDB, memberId: Buffer): Promise<void>;
10
12
  export declare function listsMember(project: Pick<QvacProject, 'memberIds'>, memberId: Buffer): boolean;
11
13
  export declare function listedProject(project: QvacProject, memberId: Buffer): boolean;