@qvac/core 0.6.7 → 0.7.1

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 (146) hide show
  1. package/CHANGELOG.md +211 -1
  2. package/README.md +2 -0
  3. package/dist-lib/lib/api/app-config.d.ts +3 -1
  4. package/dist-lib/lib/api/app-config.js +15 -4
  5. package/dist-lib/lib/api/chat.d.ts +1 -1
  6. package/dist-lib/lib/api/chat.js +9 -0
  7. package/dist-lib/lib/api/index.d.ts +2 -1
  8. package/dist-lib/lib/api/index.js +2 -1
  9. package/dist-lib/lib/api/network.d.ts +8 -1
  10. package/dist-lib/lib/api/network.js +6 -0
  11. package/dist-lib/lib/assistant/fake-models.d.ts +22 -0
  12. package/dist-lib/lib/assistant/fake-models.js +112 -0
  13. package/dist-lib/lib/assistant/harness.d.ts +12 -0
  14. package/dist-lib/lib/assistant/harness.js +26 -0
  15. package/dist-lib/lib/assistant/history.d.ts +5 -1
  16. package/dist-lib/lib/assistant/history.js +14 -2
  17. package/dist-lib/lib/assistant-tree.d.ts +4 -0
  18. package/dist-lib/lib/assistant-tree.js +45 -7
  19. package/dist-lib/lib/assistant.d.ts +15 -2
  20. package/dist-lib/lib/assistant.js +181 -42
  21. package/dist-lib/lib/constants.d.ts +6 -0
  22. package/dist-lib/lib/constants.js +6 -0
  23. package/dist-lib/lib/core/agent.d.ts +2 -0
  24. package/dist-lib/lib/core/agent.js +16 -5
  25. package/dist-lib/lib/core/blind-peers.d.ts +4 -0
  26. package/dist-lib/lib/core/blind-peers.js +1 -0
  27. package/dist-lib/lib/core/chat-activity.d.ts +1 -1
  28. package/dist-lib/lib/core/chat-activity.js +3 -0
  29. package/dist-lib/lib/core/chat-copy.d.ts +4 -0
  30. package/dist-lib/lib/core/chat-copy.js +1 -1
  31. package/dist-lib/lib/core/chat-fork.d.ts +21 -0
  32. package/dist-lib/lib/core/chat-fork.js +245 -0
  33. package/dist-lib/lib/core/chat-move.js +1 -4
  34. package/dist-lib/lib/core/chat.d.ts +1 -0
  35. package/dist-lib/lib/core/chat.js +1 -1
  36. package/dist-lib/lib/core/collections.d.ts +1 -0
  37. package/dist-lib/lib/core/collections.js +1 -0
  38. package/dist-lib/lib/core/contract/compute.d.ts +1 -1
  39. package/dist-lib/lib/core/contract/compute.js +2 -1
  40. package/dist-lib/lib/core/contract/index.js +6 -1
  41. package/dist-lib/lib/core/contract/invites.d.ts +1 -11
  42. package/dist-lib/lib/core/contract/invites.js +3 -3
  43. package/dist-lib/lib/core/contract/mesh-seeds.d.ts +2 -0
  44. package/dist-lib/lib/core/contract/mesh-seeds.js +26 -0
  45. package/dist-lib/lib/core/contract/open-join.d.ts +2 -0
  46. package/dist-lib/lib/core/contract/open-join.js +57 -0
  47. package/dist-lib/lib/core/departures.d.ts +8 -1
  48. package/dist-lib/lib/core/departures.js +59 -2
  49. package/dist-lib/lib/core/fs-drives.d.ts +8 -1
  50. package/dist-lib/lib/core/fs-drives.js +47 -15
  51. package/dist-lib/lib/core/harness-loan.d.ts +4 -20
  52. package/dist-lib/lib/core/harness-loan.js +7 -184
  53. package/dist-lib/lib/core/id.d.ts +1 -0
  54. package/dist-lib/lib/core/id.js +3 -0
  55. package/dist-lib/lib/core/identity.d.ts +1 -1
  56. package/dist-lib/lib/core/identity.js +6 -4
  57. package/dist-lib/lib/core/invite.d.ts +2 -0
  58. package/dist-lib/lib/core/invite.js +6 -2
  59. package/dist-lib/lib/core/membership.d.ts +1 -12
  60. package/dist-lib/lib/core/membership.js +1 -94
  61. package/dist-lib/lib/core/mesh.d.ts +11 -15
  62. package/dist-lib/lib/core/mesh.js +52 -88
  63. package/dist-lib/lib/core/migrations/agent-tools-scope.d.ts +2 -0
  64. package/dist-lib/lib/core/migrations/agent-tools-scope.js +24 -0
  65. package/dist-lib/lib/core/migrations/index.js +3 -1
  66. package/dist-lib/lib/core/peer-duplex.d.ts +32 -0
  67. package/dist-lib/lib/core/peer-duplex.js +207 -0
  68. package/dist-lib/lib/core/sync/index.js +2 -16
  69. package/dist-lib/lib/core.d.ts +47 -50
  70. package/dist-lib/lib/core.js +524 -878
  71. package/dist-lib/lib/diagnostics.d.ts +7 -0
  72. package/dist-lib/lib/diagnostics.js +22 -0
  73. package/dist-lib/lib/doctor/checks.d.ts +15 -0
  74. package/dist-lib/lib/doctor/checks.js +272 -0
  75. package/dist-lib/lib/doctor/redact.d.ts +4 -0
  76. package/dist-lib/lib/doctor/redact.js +31 -0
  77. package/dist-lib/lib/doctor/render.d.ts +8 -0
  78. package/dist-lib/lib/doctor/render.js +83 -0
  79. package/dist-lib/lib/doctor/tree.d.ts +5 -0
  80. package/dist-lib/lib/doctor/tree.js +167 -0
  81. package/dist-lib/lib/downloads-allowed-pusher.d.ts +10 -0
  82. package/dist-lib/lib/downloads-allowed-pusher.js +37 -0
  83. package/dist-lib/lib/guards.d.ts +1 -0
  84. package/dist-lib/lib/guards.js +4 -0
  85. package/dist-lib/lib/inference-calls.js +1 -5
  86. package/dist-lib/lib/kv-cache-reclaim.d.ts +2 -1
  87. package/dist-lib/lib/kv-cache-reclaim.js +24 -0
  88. package/dist-lib/lib/mesh-scoped-engine.js +4 -0
  89. package/dist-lib/lib/model-download-status.d.ts +1 -1
  90. package/dist-lib/lib/model-download.d.ts +9 -10
  91. package/dist-lib/lib/model-download.js +49 -16
  92. package/dist-lib/lib/model-memory-cap.d.ts +29 -0
  93. package/dist-lib/lib/model-memory-cap.js +69 -0
  94. package/dist-lib/lib/models-watch.d.ts +1 -1
  95. package/dist-lib/lib/models-watch.js +4 -4
  96. package/dist-lib/lib/oauth/flow.js +1 -1
  97. package/dist-lib/lib/oauth/loopback.js +16 -2
  98. package/dist-lib/lib/oauth/redirect-types.d.ts +1 -0
  99. package/dist-lib/lib/run-replay.d.ts +26 -0
  100. package/dist-lib/lib/run-replay.js +107 -0
  101. package/dist-lib/schema/config.d.ts +1 -1
  102. package/dist-lib/schema/config.js +1 -1
  103. package/dist-lib/schema/content.js +9 -1
  104. package/dist-lib/schema/engine.js +75 -6
  105. package/dist-lib/schema/field-order.js +1 -1
  106. package/dist-lib/schema/local.js +3 -2
  107. package/dist-lib/schema/qvac.js +28 -2
  108. package/dist-lib/spec/bind.js +17 -0
  109. package/dist-lib/spec/calls.js +4 -0
  110. package/dist-lib/spec/capabilities.d.ts +8 -0
  111. package/dist-lib/spec/config/hyperconf/index.js +1 -1
  112. package/dist-lib/spec/hrpc/hrpc.json +52 -0
  113. package/dist-lib/spec/hrpc/index.js +53 -4
  114. package/dist-lib/spec/hrpc/messages.js +1498 -1278
  115. package/dist-lib/spec/hyperschema/index.js +1498 -1278
  116. package/dist-lib/spec/hyperschema/schema.json +182 -0
  117. package/dist-lib/spec/hyperschema/types.d.ts +49 -3
  118. package/dist-lib/spec/local/hyperdb/messages.js +22 -10
  119. package/dist-lib/spec/local/hyperschema/index.js +16 -7
  120. package/dist-lib/spec/local/hyperschema/schema.json +10 -1
  121. package/dist-lib/spec/local/hyperschema/types.d.ts +3 -2
  122. package/dist-lib/spec/mesh/hyperdb/db.json +16 -1
  123. package/dist-lib/spec/mesh/hyperdb/index.js +71 -2
  124. package/dist-lib/spec/mesh/hyperdb/messages.js +530 -246
  125. package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +13 -1
  126. package/dist-lib/spec/mesh/hyperdispatch/index.js +35 -1
  127. package/dist-lib/spec/mesh/hyperdispatch/messages.js +223 -10
  128. package/dist-lib/spec/mesh/hyperschema/index.js +223 -10
  129. package/dist-lib/spec/mesh/hyperschema/schema.json +128 -1
  130. package/dist-lib/spec/mesh/hyperschema/types.d.ts +34 -2
  131. package/package.json +14 -19
  132. package/tui/README.md +7 -1
  133. package/tui/app.mjs +366 -78
  134. package/tui/boot.mjs +1 -0
  135. package/tui/index.mjs +1 -0
  136. package/tui/lib/fork-command.mjs +43 -0
  137. package/tui/lib/render.mjs +25 -1
  138. package/worker/assistant-sidecar.mjs +5 -2
  139. package/dist-lib/lib/core/device-invites.d.ts +0 -11
  140. package/dist-lib/lib/core/device-invites.js +0 -63
  141. package/dist-lib/lib/core/grant-expiry.d.ts +0 -11
  142. package/dist-lib/lib/core/grant-expiry.js +0 -66
  143. package/dist-lib/lib/core/mesh-seed-handoff.d.ts +0 -27
  144. package/dist-lib/lib/core/mesh-seed-handoff.js +0 -53
  145. package/dist-lib/lib/oauth/loopback.stub.d.ts +0 -3
  146. package/dist-lib/lib/oauth/loopback.stub.js +0 -6
@@ -13,9 +13,6 @@ import { syncChanges } from './sync/index.js';
13
13
  import { buildDefaultSeed, isOwnKickedRow, isOwnRevokedRow } from './helpers/index.js';
14
14
  import { raceResource } from '../race-resource.js';
15
15
  import { signDeviceAttestation, signWriterClaim } from './device-attestation.js';
16
- import { attestMemberClaim, claimAttestation, deviceInviteToken } from './identity.js';
17
- import { GrantExpiry } from './grant-expiry.js';
18
- import { grantKeyPair, signInviteClaim } from './invite-grants.js';
19
16
  import { liveDeviceForWriter } from './contract/activity.js';
20
17
  import { assertComputeRoute } from './contract/compute.js';
21
18
  import { MESH_NAME_ROW } from './contract/mesh-name.js';
@@ -24,12 +21,12 @@ import { createSyncActivity } from './sync-status.js';
24
21
  import { buildHealthAssistant } from './health-assistant.js';
25
22
  import b4a from 'b4a';
26
23
  import { errorMessage, isRecord } from '../guards.js';
27
- import { ACTIVE_AGENTS, ACTIVE_CHATS, DEVICES, LIVE_WRITERS_BY_MOST_RECENT, MEMBERS, MESH_KINDS, MESH_NAMES } from './collections.js';
24
+ import { ACTIVE_AGENTS, ACTIVE_CHATS, DEVICES, LIVE_WRITERS_BY_MOST_RECENT, MEMBERS, MESH_NAMES } from './collections.js';
28
25
  const [MESH_KEYPAIR, MESH_ENCRYPTION, MESH_DEVICE] = crypto.namespace('qvac-assistant/mesh', 3);
26
+ export const MESH_JOINING = 'This mesh is still joining: it accepts writes once it is joined';
29
27
  export function meshPublicKeyFromSeed(seed) {
30
28
  return crypto.keyPair(crypto.hash([MESH_KEYPAIR, seed])).publicKey;
31
29
  }
32
- const GENESIS_POLL_MS = 250;
33
30
  const WARMUP_COLLECTIONS = [
34
31
  '@qvac/devices',
35
32
  '@qvac/active-agents',
@@ -91,8 +88,8 @@ export class Mesh extends ReadyResource {
91
88
  revoked: 0
92
89
  };
93
90
  _deviceId;
94
- _grantExpiry;
95
91
  _admitted = false;
92
+ _birth = null;
96
93
  _autobee = null;
97
94
  _blobs = null;
98
95
  _syncActivity = null;
@@ -210,28 +207,6 @@ export class Mesh extends ReadyResource {
210
207
  namedAt: Math.max(Date.now(), (current?.namedAt ?? 0) + 1)
211
208
  });
212
209
  }
213
- refreshGrantExpiry() {
214
- return this._grantExpiry.refresh();
215
- }
216
- async bornKind() {
217
- for await (const _device of this.view.find(DEVICES)) {
218
- return (await this.view.get(MESH_KINDS, { id: 'mesh' }))?.kind ?? null;
219
- }
220
- return undefined;
221
- }
222
- async waitForGenesis(timeout) {
223
- const deadline = Date.now() + timeout;
224
- let kind = await this.bornKind();
225
- while (kind === undefined && Date.now() < deadline) {
226
- await new Promise((resolve) => setTimeout(resolve, GENESIS_POLL_MS));
227
- if (this.closing) {
228
- return undefined;
229
- }
230
- await this.autobee.update();
231
- kind = await this.bornKind();
232
- }
233
- return kind;
234
- }
235
210
  sign(message) {
236
211
  return crypto.sign(message, this._keyPair.secretKey);
237
212
  }
@@ -260,7 +235,6 @@ export class Mesh extends ReadyResource {
260
235
  this.discoveryKey = crypto.discoveryKey(this._keyPair.publicKey);
261
236
  this._scope = ['p2p', 'mesh', b4a.toHex(this._keyPair.publicKey).slice(0, 8)];
262
237
  registerRoutes(this.router, { compute: this._compute });
263
- this._grantExpiry = new GrantExpiry(this, (err) => this._log?.warn(this._scope, 'revoking expired invite grants failed', err));
264
238
  }
265
239
  async _open() {
266
240
  this._log?.info(this._scope, 'opening');
@@ -296,7 +270,6 @@ export class Mesh extends ReadyResource {
296
270
  fastForwards: this.autobee.stats.fastForwards
297
271
  }));
298
272
  this.autobee.on('interrupt', (err) => this._log?.warn(this._scope, 'autobee interrupt', err));
299
- this.autobee.on('writable', () => void this._grantExpiry.refresh());
300
273
  if (this._bootInterrupted) {
301
274
  void this.autobee.close().catch(noop);
302
275
  }
@@ -312,7 +285,6 @@ export class Mesh extends ReadyResource {
312
285
  });
313
286
  this._syncActivity = createSyncActivity(this);
314
287
  void this.refreshOurMember().catch((err) => this._log?.warn(this._scope, 'reading our member failed', err));
315
- void this._grantExpiry.refresh();
316
288
  this._log?.info(this._scope, 'autobee opened', {
317
289
  writable: this.autobee.writable,
318
290
  creator: this._key === null
@@ -320,12 +292,13 @@ export class Mesh extends ReadyResource {
320
292
  void this._sweepOrphanedBlobs(this.blobs.core.length);
321
293
  this.files.start({ deviceId: this._deviceId });
322
294
  }
323
- startBlindPeering(dht) {
295
+ startBlindPeering(dht, { suspended = false } = {}) {
324
296
  if (this.closing || this._blindPeering || !this._autobee || !this._blobs) {
325
297
  return;
326
298
  }
327
299
  this._blindPeering = new BlindPeering(dht, this._store, {
328
300
  pick: 2,
301
+ suspended,
329
302
  wakeup: this._wakeup,
330
303
  ...this._blindPeeringOpts
331
304
  });
@@ -339,6 +312,15 @@ export class Mesh extends ReadyResource {
339
312
  });
340
313
  this._log?.info(this._scope, 'blind peering started');
341
314
  }
315
+ async stopBlindPeering() {
316
+ const peering = this._blindPeering;
317
+ if (!peering) {
318
+ return;
319
+ }
320
+ this._blindPeering = null;
321
+ await peering.close();
322
+ this._log?.info(this._scope, 'blind peering stopped');
323
+ }
342
324
  async openBlobs(coreKey) {
343
325
  const core = this._store.get({ key: coreKey, encryptionKey: this._encryptionKey });
344
326
  const blobs = new Hyperblobs(core);
@@ -353,7 +335,6 @@ export class Mesh extends ReadyResource {
353
335
  }
354
336
  async _close() {
355
337
  this._log?.info(this._scope, 'closing');
356
- this._grantExpiry.close();
357
338
  await this._files?.close();
358
339
  await Promise.all([this.autobee.close(), this.blobs.close(), this._blindPeering?.close()]);
359
340
  this._blindPeering = null;
@@ -398,14 +379,30 @@ export class Mesh extends ReadyResource {
398
379
  get localLength() {
399
380
  return this.autobee.local.length;
400
381
  }
382
+ get joining() {
383
+ return this._birth !== null;
384
+ }
401
385
  async dispatch(name, payload, opts) {
402
386
  assertComputeRoute(this._compute, name);
387
+ assertJoined(this.autobee, opts);
403
388
  await this.autobee.append(encode(`@qvac/${name}`, payload), opts);
404
389
  }
405
390
  batch() {
406
- return new MeshBatch(this.autobee, { compute: this._compute });
391
+ return new MeshBatch(this.autobee, { compute: this._compute, joined: true });
392
+ }
393
+ appendBirthOp(identity, device, opts = {}) {
394
+ this._birth ??= (async () => {
395
+ await this._appendBirthOp(identity, device, typeof opts === 'function' ? await opts() : opts);
396
+ })().finally(() => {
397
+ this._birth = null;
398
+ });
399
+ return this._birth.then(() => this._settleBirth());
407
400
  }
408
- async appendBirthOp(identity, device, { member, claim, sharing = false, grantSecret, recovery, vouched = false } = {}) {
401
+ async _settleBirth() {
402
+ await this.autobee.update();
403
+ this.throwIfInterrupted();
404
+ }
405
+ async _appendBirthOp(identity, device, { member, sharing = false } = {}) {
409
406
  if (this.autobee.local.length > 0)
410
407
  return;
411
408
  const { publicKey: deviceId, secretKey } = 'secretKey' in identity ? identity : { publicKey: identity, secretKey: null };
@@ -415,7 +412,7 @@ export class Mesh extends ReadyResource {
415
412
  : undefined;
416
413
  const deviceSignature = secretKey && row ? this.signDeviceClaim(secretKey, deviceId, row.joinedAt) : undefined;
417
414
  if (this._key === null) {
418
- const birth = this.batch();
415
+ const birth = new MeshBatch(this.autobee, { compute: this._compute });
419
416
  birth.dispatch('add-writer', { writerKey: localKey, device: row, deviceSignature });
420
417
  if (sharing) {
421
418
  birth.dispatch('mint-mesh-kind', { id: 'mesh', kind: 'shared' });
@@ -432,63 +429,21 @@ export class Mesh extends ReadyResource {
432
429
  }
433
430
  await birth.flush();
434
431
  }
435
- else if (!this.autobee.writable && !vouched && (grantSecret || !sharing)) {
436
- const signature = this.sign(localKey);
437
- const device = row ? claimableDevice(row) : undefined;
438
- const join = this.batch();
439
- if (grantSecret) {
440
- const pair = grantKeyPair(grantSecret);
441
- join.dispatch('claim-invite', {
442
- signature,
443
- deviceId,
444
- device,
445
- deviceSignature,
446
- grantKey: pair.publicKey,
447
- inviteSignature: signInviteClaim(pair.secretKey, {
448
- meshPublicKey: this.publicKey,
449
- grantKey: pair.publicKey,
450
- deviceId,
451
- writerKey: localKey
452
- }),
453
- member: member?.member,
454
- memberClaim: member?.proof
455
- });
456
- }
457
- else if (recovery) {
458
- if (!device || !secretKey) {
459
- throw new Error('A recovery needs this device row and its key');
460
- }
461
- join.dispatch('recover-device', {
462
- signature,
463
- deviceId,
464
- device,
465
- deviceSignature,
466
- memberId: recovery.memberId,
467
- proof: await attestMemberClaim(claimAttestation(this.publicKey, deviceId, localKey), { publicKey: deviceId, secretKey }, recovery.proof)
468
- });
469
- }
470
- else {
471
- join.dispatch('join', {
472
- signature,
473
- deviceId,
474
- device,
475
- claimedMemberId: claim?.memberId,
476
- inviteToken: claim?.inviteNonce
477
- ? deviceInviteToken(claim.inviteNonce, deviceId)
478
- : undefined,
479
- deviceSignature
480
- });
481
- }
432
+ else if (!this.autobee.writable) {
433
+ const join = new MeshBatch(this.autobee, { compute: this._compute });
434
+ join.dispatch('open-join', {
435
+ signature: this.sign(localKey),
436
+ deviceId,
437
+ device: row ? claimableDevice(row) : undefined,
438
+ deviceSignature,
439
+ member: member?.member,
440
+ memberClaim: member?.proof
441
+ });
482
442
  if (row) {
483
443
  join.dispatch('attest-device', this.attestDevice(deviceId));
484
444
  }
485
- if (row && member && !grantSecret) {
486
- join.dispatch('join-member', member);
487
- }
488
445
  await join.flush({ optimistic: true });
489
446
  }
490
- await this.autobee.update();
491
- this.throwIfInterrupted();
492
447
  }
493
448
  get interrupted() {
494
449
  return this.autobee.interrupted;
@@ -707,10 +662,12 @@ export class Mesh extends ReadyResource {
707
662
  export class MeshBatch {
708
663
  _autobee;
709
664
  _compute;
665
+ _joined;
710
666
  _ops = [];
711
667
  constructor(autobee, opts = {}) {
712
668
  this._autobee = autobee;
713
669
  this._compute = opts.compute ?? false;
670
+ this._joined = opts.joined ?? false;
714
671
  }
715
672
  get size() {
716
673
  return this._ops.length;
@@ -722,12 +679,19 @@ export class MeshBatch {
722
679
  async flush(opts) {
723
680
  if (this._ops.length === 0)
724
681
  return;
682
+ if (this._joined) {
683
+ assertJoined(this._autobee, opts);
684
+ }
725
685
  const ops = this._ops;
726
686
  this._ops = [];
727
687
  await this._autobee.append(ops, opts);
728
688
  }
729
689
  }
730
690
  function noop() { }
691
+ function assertJoined(autobee, opts) {
692
+ if (!opts?.optimistic && !autobee.writable)
693
+ throw new Error(MESH_JOINING);
694
+ }
731
695
  function openedPart(part, name) {
732
696
  if (part === null)
733
697
  throw new Error(`mesh ${name} read before open`);
@@ -0,0 +1,2 @@
1
+ import type { Mesh } from '../mesh.ts';
2
+ export declare function migrateAgentToolsScope(mesh: Mesh): Promise<boolean>;
@@ -0,0 +1,24 @@
1
+ import { allTools } from '../agent.js';
2
+ import { AGENTS } from '../collections.js';
3
+ export async function migrateAgentToolsScope(mesh) {
4
+ const scoped = await selectedScopeAgents(mesh);
5
+ for (const agent of scoped) {
6
+ await mesh.dispatch('put-agent', { agent: { ...agent, ...allTools() } });
7
+ }
8
+ return scoped.length > 0;
9
+ }
10
+ async function selectedScopeAgents(mesh) {
11
+ const snap = mesh.view.snapshot();
12
+ try {
13
+ const scoped = [];
14
+ for await (const agent of snap.find(AGENTS)) {
15
+ if (!agent.deletedAt && agent.toolsScope === 'selected') {
16
+ scoped.push(agent);
17
+ }
18
+ }
19
+ return scoped;
20
+ }
21
+ finally {
22
+ await snap.close();
23
+ }
24
+ }
@@ -1,4 +1,5 @@
1
1
  import { migrateAgentCatalog } from './agent-catalog.js';
2
+ import { migrateAgentToolsScope } from './agent-tools-scope.js';
2
3
  import { migrateMembership } from './membership.js';
3
4
  import { sealDeviceRows } from './seal.js';
4
5
  import { reindexAgents } from './agent-indexes.js';
@@ -9,5 +10,6 @@ export async function runMigrations(mesh, context, { catalog = true } = {}) {
9
10
  const agents = catalog && (await migrateAgentCatalog(mesh, context));
10
11
  const seal = await sealDeviceRows(mesh, context);
11
12
  const indexed = await reindexAgents(mesh);
12
- return devices || membership || agents || seal || indexed;
13
+ const tools = await migrateAgentToolsScope(mesh);
14
+ return devices || membership || agents || seal || indexed || tools;
13
15
  }
@@ -0,0 +1,32 @@
1
+ import { Duplex } from 'streamx';
2
+ export interface PeerStream {
3
+ readonly remotePublicKey: Uint8Array;
4
+ readonly noiseStream: {
5
+ userData: unknown;
6
+ };
7
+ }
8
+ export interface PeerDuplexOptions {
9
+ readonly protocol: string;
10
+ readonly onaccept: (fromDeviceId: Uint8Array) => Promise<Duplex | null>;
11
+ readonly unreachable: string;
12
+ readonly revoked: string;
13
+ readonly onchange?: (deviceId: string) => void;
14
+ }
15
+ export declare class PeerDuplexChannel {
16
+ private readonly _opts;
17
+ private readonly _peers;
18
+ private _nextId;
19
+ private readonly _pairing;
20
+ constructor(opts: PeerDuplexOptions);
21
+ addStream(stream: PeerStream): void;
22
+ isOpen(deviceId: string): boolean;
23
+ dial(deviceId: string): Duplex;
24
+ revalidate(allowed: (deviceId: Uint8Array) => Promise<boolean>): Promise<void>;
25
+ destroy(): void;
26
+ private _opened;
27
+ private _attach;
28
+ private _serve;
29
+ private _deliver;
30
+ private _end;
31
+ private _drop;
32
+ }
@@ -0,0 +1,207 @@
1
+ import b4a from 'b4a';
2
+ import { raw, uint } from 'compact-encoding';
3
+ import { Duplex } from 'streamx';
4
+ import { createProtocolPairing } from './protocol-pairing.js';
5
+ function noop() { }
6
+ const bytes = raw;
7
+ const frame = {
8
+ preencode(state, f) {
9
+ uint.preencode(state, f.id);
10
+ uint.preencode(state, f.mine ? 1 : 0);
11
+ bytes.preencode(state, f.data);
12
+ },
13
+ encode(state, f) {
14
+ uint.encode(state, f.id);
15
+ uint.encode(state, f.mine ? 1 : 0);
16
+ bytes.encode(state, f.data);
17
+ },
18
+ decode(state) {
19
+ const id = uint.decode(state);
20
+ const mine = uint.decode(state) === 1;
21
+ return { id, mine, data: bytes.decode(state) };
22
+ }
23
+ };
24
+ const ends = {
25
+ preencode(state, e) {
26
+ uint.preencode(state, e.id);
27
+ uint.preencode(state, e.mine ? 1 : 0);
28
+ },
29
+ encode(state, e) {
30
+ uint.encode(state, e.id);
31
+ uint.encode(state, e.mine ? 1 : 0);
32
+ },
33
+ decode(state) {
34
+ return { id: uint.decode(state), mine: uint.decode(state) === 1 };
35
+ }
36
+ };
37
+ export class PeerDuplexChannel {
38
+ _opts;
39
+ _peers;
40
+ _nextId;
41
+ _pairing;
42
+ constructor(opts) {
43
+ this._opts = opts;
44
+ this._peers = new Map();
45
+ this._nextId = 1;
46
+ this._pairing = createProtocolPairing(opts.protocol);
47
+ }
48
+ addStream(stream) {
49
+ const deviceId = b4a.toHex(stream.remotePublicKey);
50
+ if (this._peers.has(deviceId)) {
51
+ return;
52
+ }
53
+ const channel = this._pairing
54
+ .mux(stream, () => this.addStream(stream))
55
+ .createChannel({
56
+ protocol: this._opts.protocol,
57
+ onopen: () => this._opened(deviceId),
58
+ onclose: () => this._drop(deviceId)
59
+ });
60
+ if (!channel) {
61
+ return;
62
+ }
63
+ const open = channel.addMessage({
64
+ encoding: uint,
65
+ onmessage: (id) => void this._serve(deviceId, id)
66
+ });
67
+ const data = channel.addMessage({
68
+ encoding: frame,
69
+ onmessage: (f) => this._deliver(deviceId, f)
70
+ });
71
+ const close = channel.addMessage({
72
+ encoding: ends,
73
+ onmessage: (e) => this._end(deviceId, e)
74
+ });
75
+ this._peers.set(deviceId, {
76
+ channel,
77
+ remoteKey: stream.remotePublicKey,
78
+ open,
79
+ data,
80
+ close,
81
+ dialled: new Map(),
82
+ served: new Map(),
83
+ opened: false
84
+ });
85
+ channel.open();
86
+ }
87
+ isOpen(deviceId) {
88
+ return this._peers.get(deviceId)?.opened === true;
89
+ }
90
+ dial(deviceId) {
91
+ const peer = this._peers.get(deviceId);
92
+ if (!peer) {
93
+ throw new Error(this._opts.unreachable);
94
+ }
95
+ const id = this._nextId;
96
+ this._nextId += 1;
97
+ const stream = this._attach(peer, id, { mine: true });
98
+ peer.open.send(id);
99
+ return stream;
100
+ }
101
+ async revalidate(allowed) {
102
+ for (const peer of [...this._peers.values()]) {
103
+ if (peer.served.size === 0)
104
+ continue;
105
+ if (await allowed(peer.remoteKey).catch(() => false))
106
+ continue;
107
+ for (const [id, stream] of [...peer.served]) {
108
+ peer.served.delete(id);
109
+ peer.close.send({ id, mine: false });
110
+ stream.destroy(new Error(this._opts.revoked));
111
+ }
112
+ }
113
+ }
114
+ destroy() {
115
+ this._pairing.destroy();
116
+ for (const [deviceId, peer] of [...this._peers]) {
117
+ try {
118
+ peer.channel.close();
119
+ }
120
+ catch { }
121
+ this._drop(deviceId);
122
+ }
123
+ }
124
+ _opened(deviceId) {
125
+ const peer = this._peers.get(deviceId);
126
+ if (!peer) {
127
+ return;
128
+ }
129
+ peer.opened = true;
130
+ this._opts.onchange?.(deviceId);
131
+ }
132
+ _attach(peer, id, opts) {
133
+ const { mine } = opts;
134
+ const held = mine ? peer.dialled : peer.served;
135
+ const stream = new Duplex({
136
+ write: (chunk, cb) => {
137
+ peer.data.send({ id, mine, data: chunk });
138
+ cb(null);
139
+ },
140
+ final: (cb) => {
141
+ peer.close.send({ id, mine });
142
+ cb(null);
143
+ },
144
+ destroy: (cb) => {
145
+ if (held.delete(id)) {
146
+ peer.close.send({ id, mine });
147
+ }
148
+ cb(null);
149
+ }
150
+ });
151
+ held.set(id, stream);
152
+ return stream;
153
+ }
154
+ async _serve(deviceId, id) {
155
+ const peer = this._peers.get(deviceId);
156
+ if (!peer || peer.served.has(id)) {
157
+ return;
158
+ }
159
+ const stream = this._attach(peer, id, { mine: false });
160
+ const served = await this._opts.onaccept(peer.remoteKey).catch(noop);
161
+ if (!served) {
162
+ stream.destroy();
163
+ return;
164
+ }
165
+ stream.on('error', noop);
166
+ served.on('error', noop);
167
+ stream.pipe(served).pipe(stream);
168
+ }
169
+ _deliver(deviceId, f) {
170
+ const peer = this._peers.get(deviceId);
171
+ if (!peer) {
172
+ return;
173
+ }
174
+ const held = f.mine ? peer.served : peer.dialled;
175
+ held.get(f.id)?.push(f.data);
176
+ }
177
+ _end(deviceId, e) {
178
+ const peer = this._peers.get(deviceId);
179
+ if (!peer) {
180
+ return;
181
+ }
182
+ const held = e.mine ? peer.served : peer.dialled;
183
+ const stream = held.get(e.id);
184
+ if (!stream) {
185
+ return;
186
+ }
187
+ held.delete(e.id);
188
+ stream.push(null);
189
+ stream.destroy();
190
+ }
191
+ _drop(deviceId) {
192
+ const peer = this._peers.get(deviceId);
193
+ if (!peer) {
194
+ return;
195
+ }
196
+ this._peers.delete(deviceId);
197
+ for (const held of [peer.dialled, peer.served]) {
198
+ for (const stream of held.values()) {
199
+ stream.destroy(new Error(this._opts.unreachable));
200
+ }
201
+ held.clear();
202
+ }
203
+ if (peer.opened) {
204
+ this._opts.onchange?.(deviceId);
205
+ }
206
+ }
207
+ }
@@ -2,7 +2,7 @@ import { FileChanges } from './files.js';
2
2
  import { syncDeviceRevocation, syncOwnMember } from './devices.js';
3
3
  import { syncChatTombstone } from './chats.js';
4
4
  import { syncNotificationChunk } from './chunks.js';
5
- import { AGENTS, CHATS, CHUNKS, CHUNK_COUNTERS, DEVICES, KNOWLEDGE, KNOWLEDGE_ARTIFACTS, MEMBERS, MESH_NAMES, PURGED_CHATS } from '../collections.js';
5
+ import { AGENTS, CHATS, CHUNKS, CHUNK_COUNTERS, DEVICES, KNOWLEDGE, KNOWLEDGE_ARTIFACTS, MEMBERS, MESH_NAMES, MESH_SEEDS, PURGED_CHATS } from '../collections.js';
6
6
  export async function syncChanges(mesh, view, from) {
7
7
  if (mesh.closing)
8
8
  return;
@@ -11,10 +11,8 @@ export async function syncChanges(mesh, view, from) {
11
11
  let membershipMoved = false;
12
12
  let agentsDirty = false;
13
13
  let meshesChanged = false;
14
- let grantsChanged = false;
15
14
  const orderDirty = new Set();
16
15
  const purged = new Map();
17
- let kindChanged = false;
18
16
  let nameChanged = false;
19
17
  const snap = view.db.snapshot();
20
18
  const files = new FileChanges(snap, from);
@@ -58,15 +56,9 @@ export async function syncChanges(mesh, view, from) {
58
56
  else if (changed(change, PURGED_CHATS)) {
59
57
  purged.set(change.value.chatId, change.value.at);
60
58
  }
61
- else if (change.collection === '@qvac/joined-meshes') {
59
+ else if (change.collection === '@qvac/joined-meshes' || change.collection === MESH_SEEDS) {
62
60
  meshesChanged = true;
63
61
  }
64
- else if (change.collection === '@qvac/mesh-invite-grants') {
65
- grantsChanged = true;
66
- }
67
- else if (change.collection === '@qvac/mesh-kinds') {
68
- kindChanged = true;
69
- }
70
62
  else if (change.collection === MESH_NAMES) {
71
63
  nameChanged = true;
72
64
  }
@@ -100,12 +92,6 @@ export async function syncChanges(mesh, view, from) {
100
92
  await mesh.syncChatOrder(orderDirty);
101
93
  }
102
94
  mesh.emit('meshes-dirty', meshesChanged);
103
- if (grantsChanged) {
104
- void mesh.refreshGrantExpiry();
105
- }
106
- if (kindChanged) {
107
- mesh.emit('mesh-kind');
108
- }
109
95
  if (nameChanged) {
110
96
  await mesh.refreshName();
111
97
  mesh.emit('mesh-named');