@qvac/core 0.6.4 → 0.6.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 (87) hide show
  1. package/CHANGELOG.md +78 -1
  2. package/README.md +20 -4
  3. package/dist-lib/lib/api/chat-move.d.ts +3 -0
  4. package/dist-lib/lib/api/chat-move.js +7 -0
  5. package/dist-lib/lib/api/chat.d.ts +1 -1
  6. package/dist-lib/lib/api/chat.js +2 -1
  7. package/dist-lib/lib/api/chunk-delta.d.ts +7 -0
  8. package/dist-lib/lib/api/chunk-delta.js +33 -0
  9. package/dist-lib/lib/api/chunks.d.ts +1 -1
  10. package/dist-lib/lib/api/chunks.js +6 -3
  11. package/dist-lib/lib/api/index.d.ts +2 -0
  12. package/dist-lib/lib/api/index.js +2 -0
  13. package/dist-lib/lib/api/mesh.d.ts +2 -1
  14. package/dist-lib/lib/api/mesh.js +4 -0
  15. package/dist-lib/lib/assistant/harness.d.ts +8 -10
  16. package/dist-lib/lib/assistant.js +1 -1
  17. package/dist-lib/lib/constants.d.ts +1 -0
  18. package/dist-lib/lib/constants.js +1 -0
  19. package/dist-lib/lib/core/archived-mesh.js +3 -42
  20. package/dist-lib/lib/core/chat-activity.js +23 -10
  21. package/dist-lib/lib/core/chat-copy.d.ts +24 -0
  22. package/dist-lib/lib/core/chat-copy.js +115 -0
  23. package/dist-lib/lib/core/chat-move.d.ts +65 -0
  24. package/dist-lib/lib/core/chat-move.js +525 -0
  25. package/dist-lib/lib/core/chat.d.ts +6 -0
  26. package/dist-lib/lib/core/chat.js +11 -3
  27. package/dist-lib/lib/core/collections.d.ts +2 -0
  28. package/dist-lib/lib/core/collections.js +2 -0
  29. package/dist-lib/lib/core/contract/compute.d.ts +1 -1
  30. package/dist-lib/lib/core/contract/compute.js +2 -1
  31. package/dist-lib/lib/core/contract/index.js +2 -0
  32. package/dist-lib/lib/core/contract/invites.d.ts +1 -0
  33. package/dist-lib/lib/core/contract/invites.js +1 -1
  34. package/dist-lib/lib/core/contract/mesh-name.d.ts +3 -0
  35. package/dist-lib/lib/core/contract/mesh-name.js +26 -0
  36. package/dist-lib/lib/core/contract/writers.js +10 -3
  37. package/dist-lib/lib/core/files/index.d.ts +1 -1
  38. package/dist-lib/lib/core/files/index.js +1 -1
  39. package/dist-lib/lib/core/files/preview/index.js +4 -1
  40. package/dist-lib/lib/core/files/processing/source.js +4 -1
  41. package/dist-lib/lib/core/files/store/index.d.ts +2 -1
  42. package/dist-lib/lib/core/files/store/index.js +2 -1
  43. package/dist-lib/lib/core/files/store/reference.d.ts +3 -0
  44. package/dist-lib/lib/core/files/store/reference.js +8 -0
  45. package/dist-lib/lib/core/files/store/sweep.js +3 -1
  46. package/dist-lib/lib/core/local/index.d.ts +4 -1
  47. package/dist-lib/lib/core/local/index.js +14 -1
  48. package/dist-lib/lib/core/mesh-seed-handoff.d.ts +3 -1
  49. package/dist-lib/lib/core/mesh-seed-handoff.js +1 -0
  50. package/dist-lib/lib/core/mesh.d.ts +5 -1
  51. package/dist-lib/lib/core/mesh.js +16 -1
  52. package/dist-lib/lib/core/sync/index.js +9 -1
  53. package/dist-lib/lib/core.d.ts +9 -0
  54. package/dist-lib/lib/core.js +156 -30
  55. package/dist-lib/lib/custom-model-pusher.js +9 -5
  56. package/dist-lib/lib/mesh-scoped-engine.js +5 -0
  57. package/dist-lib/schema/engine.js +80 -1
  58. package/dist-lib/schema/local.js +17 -0
  59. package/dist-lib/schema/qvac.d.ts +2 -0
  60. package/dist-lib/schema/qvac.js +16 -0
  61. package/dist-lib/spec/bind.js +25 -0
  62. package/dist-lib/spec/calls.js +5 -0
  63. package/dist-lib/spec/capabilities.d.ts +10 -0
  64. package/dist-lib/spec/hrpc/hrpc.json +65 -0
  65. package/dist-lib/spec/hrpc/index.js +66 -4
  66. package/dist-lib/spec/hrpc/messages.js +1398 -1154
  67. package/dist-lib/spec/hyperschema/index.js +1398 -1154
  68. package/dist-lib/spec/hyperschema/schema.json +204 -0
  69. package/dist-lib/spec/hyperschema/types.d.ts +47 -0
  70. package/dist-lib/spec/local/hyperdb/db.json +17 -1
  71. package/dist-lib/spec/local/hyperdb/index.js +83 -2
  72. package/dist-lib/spec/local/hyperdb/messages.js +340 -136
  73. package/dist-lib/spec/local/hyperschema/index.js +107 -2
  74. package/dist-lib/spec/local/hyperschema/schema.json +78 -1
  75. package/dist-lib/spec/local/hyperschema/types.d.ts +18 -1
  76. package/dist-lib/spec/mesh/hyperdb/db.json +16 -1
  77. package/dist-lib/spec/mesh/hyperdb/index.js +71 -2
  78. package/dist-lib/spec/mesh/hyperdb/messages.js +454 -228
  79. package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +7 -1
  80. package/dist-lib/spec/mesh/hyperdispatch/index.js +18 -1
  81. package/dist-lib/spec/mesh/hyperdispatch/messages.js +150 -2
  82. package/dist-lib/spec/mesh/hyperschema/index.js +150 -2
  83. package/dist-lib/spec/mesh/hyperschema/schema.json +76 -1
  84. package/dist-lib/spec/mesh/hyperschema/types.d.ts +21 -1
  85. package/package.json +5 -3
  86. package/tui/app.mjs +89 -3
  87. package/tui/lib/move-command.mjs +17 -0
@@ -12,7 +12,8 @@ import ReadyGuard from 'ready-guard';
12
12
  import ReadyResource from 'ready-resource';
13
13
  import ScopeLock from 'scope-lock';
14
14
  import { createApi } from './api/index.js';
15
- import { NO_SUCH_MESH } from './api/current-mesh.js';
15
+ import { NO_SUCH_MESH, requireMesh } from './api/current-mesh.js';
16
+ import { ChatMoves } from './core/chat-move.js';
16
17
  import { asEncoded, watchModels } from './models-watch.js';
17
18
  import { BLIND_PEER_KEYS, BLIND_RELAY_KEYS } from './core/blind-peers.js';
18
19
  import { CoreBlobServer, MemoryVectorIndex } from './core/files/index.js';
@@ -23,9 +24,10 @@ import { sweepDriveStaging } from './fs-drive-bridge.js';
23
24
  import { decodeMeshInvite, encodeMeshInvite } from './core/invite.js';
24
25
  import { fileKeyOf, knownFile, referencedFile } from './core/files/local-index.js';
25
26
  import { grantKeyPair, inviteGrantTermsRefusal } from './core/invite-grants.js';
26
- import { belongsToAnotherMember, inviteStanding, memberStanding } from './core/contract/invites.js';
27
+ import { belongsToAnotherMember, inviteStanding, memberStanding, removedBy } from './core/contract/invites.js';
27
28
  import { syncStatusSnapshot } from './core/sync-status.js';
28
29
  import { openLocalStore } from './core/local/index.js';
30
+ import { MAX_MESH_NAME_CHARS, meshNameWithinBounds } from '../schema/qvac.js';
29
31
  import { getAgent } from './core/agent.js';
30
32
  import { countContents, migrateContents, withArchivedMesh } from './core/archived-mesh.js';
31
33
  import { HostedAgents } from './core/hosted-agents.js';
@@ -33,7 +35,7 @@ import { HarnessLoan } from './core/harness-loan.js';
33
35
  import { duplexPair } from '@qvac/harness/transport';
34
36
  import { signMemberWriterClaim, signWriterClaim, verifyMemberWriterClaim, verifyWriterClaim } from './core/device-attestation.js';
35
37
  import { HOST_SILENT, HOST_UNREACHABLE } from './core/handoff-channel.js';
36
- import { MeshSeedHandoff, SEED_REFUSED, SEED_ASKER_UNSEEN } from './core/mesh-seed-handoff.js';
38
+ import { MeshSeedHandoff, SEED_REFUSED, ADMISSION_DEFERRED, SEED_ASKER_UNSEEN } from './core/mesh-seed-handoff.js';
37
39
  import { SecretHandoff } from './core/secret-handoff.js';
38
40
  import { InferenceCallsHandoff } from './core/inference-calls-handoff.js';
39
41
  import { createInferenceCallsReceiver, NO_RECORD_STORE } from './core/inference-calls-receiver.js';
@@ -70,7 +72,6 @@ const MESH_JOIN_CANCELED = 'Mesh join canceled';
70
72
  const GRANT_JOIN_TIMED_OUT = 'Join did not complete in time - the invite may already be spent, expired or revoked';
71
73
  const MESH_ID = /^[0-9a-f]{64}$/;
72
74
  const MESH_BELOW_FLOOR = 'Build is below the mesh floor';
73
- const LEAVE_HOME_REFUSED = 'Your account cannot be left while other meshes remain';
74
75
  const MESH_WITHOUT_ACCOUNT = 'Recording a mesh needs the account mesh';
75
76
  export const ACCOUNT_MEMBER_UNKNOWN = 'This device is not part of its account yet';
76
77
  export const AWAITING_ADMISSION = 'awaiting-admission';
@@ -85,6 +86,7 @@ const RECOVERY_DEVICE_REMOVED = 'This device was removed from the account, so it
85
86
  const RECOVERY_WAIT_MS = 30_000;
86
87
  export const MEMBER_INVITE_HOME_REFUSED = 'An account mesh cannot be invited into';
87
88
  export const DEVICE_INVITE_MESH_REFUSED = 'Only an account mesh can take a device invite';
89
+ export const MESH_NAME_ACCOUNT_REFUSED = 'An account mesh has no shared name';
88
90
  export const INVITE_ACCOUNT_UNREADABLE = 'This device cannot reach its account to check';
89
91
  export const MESH_HELD_BY_SIBLING = 'Another of your devices holds this mesh: bring that device online, or invite from it instead';
90
92
  export const INVITE_GRANT_REVOKED = 'This invite was revoked';
@@ -108,6 +110,7 @@ const DIAGNOSTICS_SAMPLE_MS = 2000;
108
110
  const ACCOUNT_WRITABLE_MS = 15_000;
109
111
  const GRANT_WRITABLE_MS = 60_000;
110
112
  const INVITER_RETRY_MS = 1_000;
113
+ const ADMISSION_RETRIES = 60;
111
114
  export class Core extends ReadyResource {
112
115
  _storagePath;
113
116
  store;
@@ -185,6 +188,7 @@ export class Core extends ReadyResource {
185
188
  _rpcs = new Set();
186
189
  _stream;
187
190
  _api;
191
+ _chatMoves = null;
188
192
  _clients;
189
193
  _deviceKeyPair;
190
194
  _identityPhrase = null;
@@ -198,6 +202,8 @@ export class Core extends ReadyResource {
198
202
  _harnessLoan;
199
203
  _serveLoanedHarness = null;
200
204
  _following = new Map();
205
+ _admissionRetries = new Map();
206
+ _admissionRetryPending = new Set();
201
207
  _revokingInMeshes = null;
202
208
  _revokeInMeshesAgain = false;
203
209
  _sweepUnknownEverywhere = false;
@@ -401,6 +407,12 @@ export class Core extends ReadyResource {
401
407
  await this._openBootMesh();
402
408
  this._systemInfo = createSystemInfoSampler();
403
409
  const meshStatus = this._meshStatusSource();
410
+ const deviceId = this.devicePublicKey;
411
+ this._chatMoves = deviceId
412
+ ? new ChatMoves((meshId) => requireMesh(this._meshPool(), meshId), this.local, {
413
+ deviceId
414
+ })
415
+ : null;
404
416
  this._api = createApi({
405
417
  state: this.local,
406
418
  mesh: this._meshPool(),
@@ -412,6 +424,7 @@ export class Core extends ReadyResource {
412
424
  forget: async (chatId) => void (await this._inferenceCalls?.forget(chatId))
413
425
  },
414
426
  logs: this._logSource(),
427
+ chatMoves: this._chatMoves,
415
428
  deviceId: this.devicePublicKey,
416
429
  lifecycle: this._apiLifecycle,
417
430
  ragIndex: (meshId) => this._meshes.get(meshId)?.vectorIndex ?? null,
@@ -542,6 +555,9 @@ export class Core extends ReadyResource {
542
555
  this._rpcs.add(rpc);
543
556
  this._offlineBoot.exit();
544
557
  void this._pushIdentityPhrase();
558
+ this._chatMoves
559
+ ?.resume()
560
+ .catch((err) => logger.warn('chat-move', 'unfinished moves not resumed', { err }));
545
561
  return;
546
562
  }
547
563
  catch (err) {
@@ -919,6 +935,8 @@ export class Core extends ReadyResource {
919
935
  await this._stopIndexing();
920
936
  await this._blobServer?.close();
921
937
  this._blobServer = null;
938
+ await this._chatMoves?.close();
939
+ this._chatMoves = null;
922
940
  for (const entry of this._meshes.values()) {
923
941
  this._unwireMesh(entry);
924
942
  await entry.mesh.close();
@@ -2423,6 +2441,7 @@ export class Core extends ReadyResource {
2423
2441
  });
2424
2442
  mesh.on('mesh-kind', () => void this._refuseSealedReader(mesh));
2425
2443
  mesh.on('membership-tallied', () => this._notifyStatus());
2444
+ mesh.on('mesh-named', () => this._notifyStatus());
2426
2445
  mesh.on('membership', (membership) => {
2427
2446
  void this._recordMemberProof(mesh, membership);
2428
2447
  void this._followJoinedMeshes(mesh);
@@ -2661,6 +2680,10 @@ export class Core extends ReadyResource {
2661
2680
  void mesh.refreshMembership().catch((error) => {
2662
2681
  this._log?.warn(['p2p', 'mesh'], 'tallying membership failed', error);
2663
2682
  });
2683
+ void mesh
2684
+ .refreshName()
2685
+ .then(() => this._notifyStatus())
2686
+ .catch((error) => this._log?.warn(['p2p', 'mesh'], 'reading the mesh name failed', error));
2664
2687
  mesh.on('chat-tombstone', (chatId, deletedAt) => {
2665
2688
  void this.local
2666
2689
  .clearChatNotify(chatId)
@@ -2784,14 +2807,18 @@ export class Core extends ReadyResource {
2784
2807
  throw new Error('Mesh transition already in progress');
2785
2808
  }
2786
2809
  const departing = await this._openPooledMesh(meshId);
2787
- const others = (await this.local.listMeshes()).filter((row) => !b4a.equals(row.key, departing.key));
2788
2810
  const leavingAccount = await this._isStoredAccount(departing);
2789
- if (others.length > 0 && leavingAccount) {
2790
- throw new Error(LEAVE_HOME_REFUSED);
2791
- }
2792
2811
  this._leaving = meshId;
2793
2812
  this._notifyStatus();
2794
2813
  try {
2814
+ if (leavingAccount) {
2815
+ await Promise.allSettled([...this._following.values()]);
2816
+ const others = (await this.local.listMeshes()).filter((row) => !b4a.equals(row.key, departing.key));
2817
+ await Promise.all(others.map((row) => this._departOnLogout(row.key)));
2818
+ if (this.closing) {
2819
+ return;
2820
+ }
2821
+ }
2795
2822
  if (await this._departureHasAudience(departing)) {
2796
2823
  await this.local.recordDeparture(departing.key, { creator: departing.minted });
2797
2824
  }
@@ -2815,6 +2842,31 @@ export class Core extends ReadyResource {
2815
2842
  this._notifyStatus();
2816
2843
  }
2817
2844
  }
2845
+ async _departOnLogout(key) {
2846
+ try {
2847
+ const mesh = await this._openPooledMesh(b4a.toHex(key));
2848
+ if (!(await this.local.awaitsAdmission(key))) {
2849
+ await mesh.waitForWritable(this._accountWritableMs);
2850
+ }
2851
+ if (await this._departureHasAudience(mesh)) {
2852
+ await this.local.recordDeparture(mesh.key, { creator: mesh.minted });
2853
+ }
2854
+ await this._announceDeparture(mesh);
2855
+ await this._archiveMembership(mesh);
2856
+ await this._retirePooledMesh(mesh);
2857
+ }
2858
+ catch (error) {
2859
+ this._log?.warn(['p2p', 'mesh'], 'departing a mesh on logout failed', error);
2860
+ if (this.closing) {
2861
+ return;
2862
+ }
2863
+ const held = this._pooled(b4a.toHex(key));
2864
+ if (held) {
2865
+ await this._retirePooledMesh(held).catch(noop);
2866
+ }
2867
+ await this.local.archiveMesh(key).catch(noop);
2868
+ }
2869
+ }
2818
2870
  async _isStoredAccount(mesh) {
2819
2871
  const key = await this._storedAccountKey();
2820
2872
  return key !== undefined && b4a.equals(key, mesh.key);
@@ -2963,6 +3015,19 @@ export class Core extends ReadyResource {
2963
3015
  });
2964
3016
  this._notifyStatus();
2965
3017
  }
3018
+ async setMeshName(meshId, name) {
3019
+ if (!this.opened)
3020
+ await this.ready();
3021
+ const trimmed = name.trim();
3022
+ if (!meshNameWithinBounds(trimmed)) {
3023
+ throw new Error(`mesh name must be at most ${MAX_MESH_NAME_CHARS} characters`);
3024
+ }
3025
+ const mesh = await requireMesh(this._meshPool(), meshId);
3026
+ if (this._isAccount(mesh)) {
3027
+ throw new Error(MESH_NAME_ACCOUNT_REFUSED);
3028
+ }
3029
+ await mesh.setName(trimmed);
3030
+ }
2966
3031
  async createMesh({ shared = false, compute = false } = {}) {
2967
3032
  if (!this.opened)
2968
3033
  await this.ready();
@@ -3179,6 +3244,7 @@ export class Core extends ReadyResource {
3179
3244
  open: mesh !== null,
3180
3245
  account,
3181
3246
  label: joined?.label || undefined,
3247
+ name: mesh?.name?.name || undefined,
3182
3248
  members: mesh?.membership.members ?? 0,
3183
3249
  shared: joined?.shared ?? false,
3184
3250
  sharedAt: joined?.shared ? joined.sharedAt : undefined,
@@ -3229,7 +3295,9 @@ export class Core extends ReadyResource {
3229
3295
  if (!target) {
3230
3296
  throw new Error('this device holds no account mesh');
3231
3297
  }
3232
- await target.dispatch('remove-writer', { writerKey, deviceId, revokedAt: Date.now() });
3298
+ const paired = await this._accountMesh()?.view.get(DEVICES, { id: deviceId });
3299
+ const revokedAt = Math.max(Date.now(), (paired?.joinedAt ?? 0) + 1);
3300
+ await target.dispatch('remove-writer', { writerKey, deviceId, revokedAt });
3233
3301
  await target.autobee.update();
3234
3302
  if (target === this._accountMesh()) {
3235
3303
  this._sweepUnknownEverywhere = true;
@@ -3583,8 +3651,11 @@ export class Core extends ReadyResource {
3583
3651
  async _followMeshRows(memberMesh, ours) {
3584
3652
  let pending = false;
3585
3653
  for await (const joined of memberMesh.view.find(JOINED_MESHES)) {
3586
- if (this.closing || !this._isPooled(memberMesh))
3654
+ if (this.closing ||
3655
+ !this._isPooled(memberMesh) ||
3656
+ this._leaving === this._meshIdOf(memberMesh)) {
3587
3657
  return pending;
3658
+ }
3588
3659
  if (!b4a.equals(joined.memberId, ours))
3589
3660
  continue;
3590
3661
  const meshId = b4a.toHex(joined.meshKey);
@@ -3755,7 +3826,8 @@ export class Core extends ReadyResource {
3755
3826
  return;
3756
3827
  }
3757
3828
  const moving = this._joinCancellation ? this._joinCancellation.account : this._minting;
3758
- if (moving || !this._isAccount(memberMesh)) {
3829
+ const leaving = this._leaving === this._meshIdOf(memberMesh);
3830
+ if (moving || leaving || !this._isAccount(memberMesh)) {
3759
3831
  return;
3760
3832
  }
3761
3833
  const session = this.local.createCandidateSession(seed, meshKey);
@@ -3801,7 +3873,7 @@ export class Core extends ReadyResource {
3801
3873
  }
3802
3874
  const { publicKey: deviceId, secretKey } = this._requireDeviceKeyPair();
3803
3875
  const own = await mesh.view.get(DEVICES, { id: deviceId });
3804
- if (own && isKickedRow(own)) {
3876
+ if (own && (await this._removedFor(mesh, own))) {
3805
3877
  return this.local.markAwaitingAdmission(mesh.key, { awaitingAdmission: false });
3806
3878
  }
3807
3879
  const binding = {
@@ -3817,24 +3889,60 @@ export class Core extends ReadyResource {
3817
3889
  memberSignature: signMemberWriterClaim(secretKey, { ...binding, memberId: ours })
3818
3890
  };
3819
3891
  const known = await mesh.bornKind();
3820
- for (const sibling of known === null ? [] : await this._liveSiblings(memberMesh, ours)) {
3821
- if (this.closing)
3822
- return;
3823
- if (mesh.writable)
3824
- break;
3825
- if (!(await this._meshSeedHandoff.admit(sibling, mesh.key, claim).catch(noop))) {
3826
- continue;
3827
- }
3828
- await mesh.waitForWritable(this._accountWritableMs);
3892
+ const deferred = known !== null && (await this._askSiblingsToAdmit(memberMesh, mesh, { ours, claim }));
3893
+ if (this.closing) {
3894
+ return;
3829
3895
  }
3830
3896
  if (!mesh.writable && (await mesh.waitForGenesis(this._accountWritableMs)) === null) {
3831
3897
  await mesh.appendBirthOp(this._requireDeviceKeyPair(), this._localDevice ?? undefined);
3832
3898
  await mesh.waitForWritable(this._accountWritableMs);
3833
3899
  }
3900
+ const meshId = this._meshIdOf(mesh);
3901
+ if (deferred && !mesh.writable) {
3902
+ this._retryAdmission(memberMesh, meshId);
3903
+ return;
3904
+ }
3905
+ this._admissionRetries.delete(meshId);
3834
3906
  if (mesh.writable) {
3835
3907
  await this.local.markAwaitingAdmission(mesh.key, { awaitingAdmission: false });
3836
3908
  }
3837
3909
  }
3910
+ async _askSiblingsToAdmit(memberMesh, mesh, { ours, claim }) {
3911
+ let deferred = false;
3912
+ for (const sibling of await this._liveSiblings(memberMesh, ours)) {
3913
+ if (this.closing || mesh.writable)
3914
+ break;
3915
+ const admitted = await this._meshSeedHandoff
3916
+ .admit(sibling, mesh.key, claim)
3917
+ .catch((error) => {
3918
+ deferred ||= error.message === ADMISSION_DEFERRED;
3919
+ return false;
3920
+ });
3921
+ if (admitted) {
3922
+ await mesh.waitForWritable(this._accountWritableMs);
3923
+ }
3924
+ }
3925
+ return deferred && !mesh.writable;
3926
+ }
3927
+ _retryAdmission(memberMesh, meshId) {
3928
+ const tries = this._admissionRetries.get(meshId) ?? 0;
3929
+ if (tries >= ADMISSION_RETRIES) {
3930
+ return;
3931
+ }
3932
+ this._admissionRetries.set(meshId, tries + 1);
3933
+ const memberId = this._meshIdOf(memberMesh);
3934
+ if (this._admissionRetryPending.has(memberId)) {
3935
+ return;
3936
+ }
3937
+ this._admissionRetryPending.add(memberId);
3938
+ Promise.race([after(INVITER_RETRY_MS), this._closeSignal])
3939
+ .then(() => {
3940
+ this._admissionRetryPending.delete(memberId);
3941
+ const current = this.closing ? null : this._pooled(memberId);
3942
+ return current ? this._followJoinedMeshes(current) : undefined;
3943
+ })
3944
+ .catch(noop);
3945
+ }
3838
3946
  async _liveSiblings(memberMesh, ours) {
3839
3947
  const self = this._deviceKeyPair?.publicKey;
3840
3948
  const siblings = [];
@@ -3925,7 +4033,7 @@ export class Core extends ReadyResource {
3925
4033
  }
3926
4034
  const mesh = this._pooled(b4a.toHex(request.meshKey));
3927
4035
  const row = mesh ? await mesh.view.get(DEVICES, { id: asker }) : null;
3928
- if (row?.revoked) {
4036
+ if (mesh && row && (await this._removedFor(mesh, row))) {
3929
4037
  this._log?.warn(['p2p', 'mesh'], 'seed request refused: removed from this mesh');
3930
4038
  return null;
3931
4039
  }
@@ -3953,8 +4061,9 @@ export class Core extends ReadyResource {
3953
4061
  return null;
3954
4062
  }
3955
4063
  async _admitIntoMesh(request, asker) {
3956
- if (this._meshTransitionLock.locked)
3957
- return null;
4064
+ if (this._meshTransitionLock.locked) {
4065
+ throw new Error(ADMISSION_DEFERRED);
4066
+ }
3958
4067
  await this._meshTransitionLock.lock();
3959
4068
  try {
3960
4069
  return await this._admitIntoMeshLocked(request, asker);
@@ -3964,8 +4073,11 @@ export class Core extends ReadyResource {
3964
4073
  }
3965
4074
  }
3966
4075
  async _admitIntoMeshLocked(request, asker) {
3967
- if (this._leaving || this._minting)
4076
+ if (this._leaving)
3968
4077
  return null;
4078
+ if (this._minting) {
4079
+ throw new Error(ADMISSION_DEFERRED);
4080
+ }
3969
4081
  const account = await this._accountMesh();
3970
4082
  const ours = account?.ourMemberId;
3971
4083
  const row = ours ? await account.view.get(DEVICES, { id: asker }) : null;
@@ -4001,7 +4113,7 @@ export class Core extends ReadyResource {
4001
4113
  const mesh = await this._openPooledMesh(b4a.toHex(request.meshKey));
4002
4114
  await mesh.autobee.update();
4003
4115
  if (!mesh.writable) {
4004
- return null;
4116
+ throw new Error(ADMISSION_DEFERRED);
4005
4117
  }
4006
4118
  if ((await mesh.bornKind()) !== 'shared') {
4007
4119
  this._log?.warn(['p2p', 'mesh'], 'admission refused: a sibling joins this mesh by its seed');
@@ -4017,14 +4129,17 @@ export class Core extends ReadyResource {
4017
4129
  return null;
4018
4130
  }
4019
4131
  const current = await mesh.view.get(DEVICES, { id: asker });
4020
- if (current && isKickedRow(current)) {
4132
+ if (current && (await this._removedFor(mesh, current))) {
4021
4133
  this._log?.warn(['p2p', 'mesh'], 'admission refused: removed from this mesh');
4022
4134
  return null;
4023
4135
  }
4024
4136
  if (current && !current.revoked) {
4025
- return current.writerKey && b4a.equals(current.writerKey, writerKey)
4026
- ? { admitted: true }
4027
- : null;
4137
+ if (current.writerKey && b4a.equals(current.writerKey, writerKey)) {
4138
+ return { admitted: true };
4139
+ }
4140
+ if (!(row.joinedAt > current.joinedAt)) {
4141
+ throw new Error(ADMISSION_DEFERRED);
4142
+ }
4028
4143
  }
4029
4144
  await mesh.dispatch('vouched-join', {
4030
4145
  signature: mesh.sign(writerKey),
@@ -4056,6 +4171,16 @@ export class Core extends ReadyResource {
4056
4171
  }
4057
4172
  return false;
4058
4173
  }
4174
+ async _removedFor(mesh, row) {
4175
+ if (!row.revoked)
4176
+ return false;
4177
+ const by = await removedBy(mesh.view, row);
4178
+ if (by !== 'own-member') {
4179
+ return by === 'another-member';
4180
+ }
4181
+ const paired = await this._accountMesh()?.view.get(DEVICES, { id: row.id });
4182
+ return !(paired && !paired.revoked && row.revokedAt && paired.joinedAt > row.revokedAt);
4183
+ }
4059
4184
  async _askerStanding(deviceId) {
4060
4185
  const account = this._accountMesh();
4061
4186
  const ours = account?.ourMemberId;
@@ -4329,6 +4454,7 @@ export class Core extends ReadyResource {
4329
4454
  open: (meshId) => this.openMesh(meshId),
4330
4455
  close: (meshId) => this.closeMesh(meshId),
4331
4456
  label: (meshId, label) => this.setMeshLabel(meshId, label),
4457
+ name: (meshId, name) => this.setMeshName(meshId, name),
4332
4458
  createMesh: (opts) => this.createMesh(opts),
4333
4459
  listArchived: () => this.listArchivedMeshes(),
4334
4460
  archivedContents: (meshId) => this.archivedMeshContents(meshId),
@@ -1,15 +1,14 @@
1
1
  import { errorMessage } from './guards.js';
2
+ import { snapshotEquals } from './api/watchable.js';
2
3
  export function startCustomModelPusher(harness, state, opts = {}) {
3
4
  const { logger, onPushed } = opts;
4
5
  let disposed = false;
5
6
  let tail = Promise.resolve();
6
7
  let queued = false;
8
+ let pushed = null;
7
9
  const push = async () => {
8
10
  const rows = await state.getCustomModelRows();
9
- if (disposed) {
10
- return;
11
- }
12
- await harness.setCustomModels?.(rows.map((row) => ({
11
+ const entries = rows.map((row) => ({
13
12
  name: row.name,
14
13
  src: row.src,
15
14
  projectionSrc: row.mmprojSrc ?? undefined,
@@ -17,7 +16,12 @@ export function startCustomModelPusher(harness, state, opts = {}) {
17
16
  expectedSize: row.expectedSize || undefined,
18
17
  label: row.label || undefined,
19
18
  contextLength: row.contextLength || undefined
20
- })));
19
+ }));
20
+ if (disposed || snapshotEquals(entries, pushed)) {
21
+ return;
22
+ }
23
+ await harness.setCustomModels?.(entries);
24
+ pushed = entries;
21
25
  if (!disposed) {
22
26
  onPushed?.();
23
27
  }
@@ -12,10 +12,14 @@ export const MESH_SCOPED = new Set([
12
12
  'getChat',
13
13
  'renameChat',
14
14
  'markChatRead',
15
+ 'setChatAgent',
16
+ 'moveChatToMesh',
17
+ 'chatMoveWatch',
15
18
  'setChatRagEnabled',
16
19
  'setChatWebSearchEnabled',
17
20
  'deleteChat',
18
21
  'chatsWatch',
22
+ 'setMeshName',
19
23
  'listChatActivity',
20
24
  'chatActivityWatch',
21
25
  'searchChats',
@@ -32,6 +36,7 @@ export const MESH_SCOPED = new Set([
32
36
  'resolveApproval',
33
37
  'listChunks',
34
38
  'chunksWatch',
39
+ 'chunksDeltaWatch',
35
40
  'listAttachmentsByKind',
36
41
  'runRequestsWatch',
37
42
  'uploadAttachment',
@@ -323,6 +323,43 @@ export function registerEngineTypes(ns) {
323
323
  { name: 'config', type: '@assistant/chat-config', required: true }
324
324
  ]
325
325
  });
326
+ ns.register({
327
+ name: 'chat-move-state',
328
+ enum: ['carrying', 'leaving', 'complete', 'failed'],
329
+ strings: true
330
+ });
331
+ ns.register({
332
+ name: 'move-chat-to-mesh-request',
333
+ fields: [
334
+ { name: 'chatId', type: 'string', required: true },
335
+ { name: 'meshId', type: 'string', required: true },
336
+ { name: 'targetMeshId', type: 'string', required: true },
337
+ { name: 'agentId', type: 'string' }
338
+ ]
339
+ });
340
+ ns.register({
341
+ name: 'chat-move',
342
+ fields: [
343
+ { name: 'meshId', type: 'string', required: true },
344
+ { name: 'chatId', type: 'string', required: true },
345
+ { name: 'targetMeshId', type: 'string', required: true },
346
+ { name: 'targetChatId', type: 'string', required: true },
347
+ { name: 'state', type: '@assistant/chat-move-state', required: true },
348
+ { name: 'files', type: 'uint', required: true },
349
+ { name: 'carried', type: 'uint', required: true },
350
+ { name: 'waiting', type: 'string', array: true },
351
+ { name: 'restarted', type: 'bool', required: true },
352
+ { name: 'failure', type: 'string' }
353
+ ]
354
+ });
355
+ ns.register({
356
+ name: 'set-chat-agent-request',
357
+ fields: [
358
+ { name: 'chatId', type: 'string', required: true },
359
+ { name: 'agentId', type: 'string', required: true },
360
+ { name: 'meshId', type: 'string', required: true }
361
+ ]
362
+ });
326
363
  ns.register({
327
364
  name: 'set-chat-flag-request',
328
365
  fields: [
@@ -1760,7 +1797,8 @@ export function registerEngineTypes(ns) {
1760
1797
  { name: 'sharedAt', type: 'uint' },
1761
1798
  { name: 'compute', type: 'bool' },
1762
1799
  { name: 'reason', type: 'string' },
1763
- { name: 'awaitingFrom', type: 'string' }
1800
+ { name: 'awaitingFrom', type: 'string' },
1801
+ { name: 'name', type: 'string' }
1764
1802
  ]
1765
1803
  });
1766
1804
  ns.register({
@@ -1770,6 +1808,13 @@ export function registerEngineTypes(ns) {
1770
1808
  { name: 'label', type: 'string' }
1771
1809
  ]
1772
1810
  });
1811
+ ns.register({
1812
+ name: 'mesh-name-request',
1813
+ fields: [
1814
+ { name: 'meshId', type: 'string', required: true },
1815
+ { name: 'name', type: 'string' }
1816
+ ]
1817
+ });
1773
1818
  ns.register({
1774
1819
  name: 'mesh-list',
1775
1820
  fields: [{ name: 'meshes', type: '@assistant/mesh-entry', array: true }]
@@ -2020,6 +2065,15 @@ export function registerEngineTypes(ns) {
2020
2065
  name: 'model-users',
2021
2066
  fields: [{ name: 'agents', type: '@assistant/model-user', array: true }]
2022
2067
  });
2068
+ ns.register({
2069
+ name: 'chunk-delta',
2070
+ fields: [
2071
+ { name: 'reset', type: 'bool', required: true },
2072
+ { name: 'chunks', type: '@assistant/chunk', array: true },
2073
+ { name: 'removed', type: 'string', array: true },
2074
+ { name: 'count', type: 'uint', required: true }
2075
+ ]
2076
+ });
2023
2077
  }
2024
2078
  export function registerEngineApi(api) {
2025
2079
  api.register({
@@ -2782,4 +2836,29 @@ export function registerEngineApi(api) {
2782
2836
  request: { name: '@assistant/list-models-request', stream: false },
2783
2837
  response: { name: '@assistant/model-info-list', stream: true }
2784
2838
  });
2839
+ api.register({
2840
+ name: 'set-mesh-name',
2841
+ request: { name: '@assistant/mesh-name-request', stream: false },
2842
+ response: { name: '@assistant/ok-response', stream: false }
2843
+ });
2844
+ api.register({
2845
+ name: 'set-chat-agent',
2846
+ request: { name: '@assistant/set-chat-agent-request', stream: false },
2847
+ response: { name: '@assistant/chat', stream: false }
2848
+ });
2849
+ api.register({
2850
+ name: 'move-chat-to-mesh',
2851
+ request: { name: '@assistant/move-chat-to-mesh-request', stream: false },
2852
+ response: { name: '@assistant/chat-move', stream: false }
2853
+ });
2854
+ api.register({
2855
+ name: 'chat-move-watch',
2856
+ request: { name: '@assistant/chat-id', stream: false },
2857
+ response: { name: '@assistant/chat-move', stream: true }
2858
+ });
2859
+ api.register({
2860
+ name: 'chunks-delta-watch',
2861
+ request: { name: '@assistant/chunks-by-chat-request', stream: false },
2862
+ response: { name: '@assistant/chunk-delta', stream: true }
2863
+ });
2785
2864
  }
@@ -148,6 +148,18 @@ export function registerLocalTypes(ns) {
148
148
  { name: 'cleared', type: 'bool', required: true },
149
149
  { name: 'deletedAt', type: 'uint' }
150
150
  ]);
151
+ versioned('chat-move', [
152
+ { name: 'meshKey', type: 'fixed32', required: true },
153
+ { name: 'chatId', type: 'string', required: true },
154
+ { name: 'targetMeshKey', type: 'fixed32', required: true },
155
+ { name: 'targetChatId', type: 'string', required: true },
156
+ { name: 'agentId', type: 'string', required: true },
157
+ { name: 'carried', type: 'uint', required: true },
158
+ { name: 'restartRunId', type: 'string' },
159
+ { name: 'startedAt', type: 'uint', required: true },
160
+ { name: 'completedAt', type: 'uint' },
161
+ { name: 'failure', type: 'string' }
162
+ ]);
151
163
  ns.register({ name: 'capability-profile', enum: CAPABILITY_PROFILES, strings: true });
152
164
  }
153
165
  export function registerLocalCollections(dbns, helpersPath) {
@@ -250,4 +262,9 @@ export function registerLocalCollections(dbns, helpersPath) {
250
262
  map: 'liveFilesMap'
251
263
  }
252
264
  });
265
+ dbns.collections.register({
266
+ name: 'chat-moves',
267
+ schema: '@local/chat-move',
268
+ key: ['meshKey', 'chatId']
269
+ });
253
270
  }
@@ -23,6 +23,8 @@ export declare function capToolActivityArguments(args: unknown): unknown;
23
23
  export declare const MAX_DEVICE_NAME_CHARS = 100;
24
24
  export declare function deviceNameWithinBounds(name: string): boolean;
25
25
  export declare function capDeviceName(name: string): string;
26
+ export declare const MAX_MESH_NAME_CHARS = 100;
27
+ export declare function meshNameWithinBounds(name: string): boolean;
26
28
  export declare function registerQvacTypes(ns: Registry): void;
27
29
  export declare function registerQvacCollections(dbns: CollectionRegistry, helpersPath: string): void;
28
30
  export declare function registerQvacDispatch(ds: Registry): void;
@@ -47,6 +47,10 @@ export function deviceNameWithinBounds(name) {
47
47
  export function capDeviceName(name) {
48
48
  return name.length <= MAX_DEVICE_NAME_CHARS ? name : name.slice(0, MAX_DEVICE_NAME_CHARS);
49
49
  }
50
+ export const MAX_MESH_NAME_CHARS = 100;
51
+ export function meshNameWithinBounds(name) {
52
+ return name.length <= MAX_MESH_NAME_CHARS;
53
+ }
50
54
  export function registerQvacTypes(ns) {
51
55
  const versioned = versionedRegistrar(ns, 'qvac');
52
56
  registerContentTypes(ns, 'qvac');
@@ -548,6 +552,16 @@ export function registerQvacTypes(ns) {
548
552
  { name: 'chatId', type: 'string', required: true },
549
553
  { name: 'at', type: 'uint', required: true }
550
554
  ]);
555
+ versioned('mesh-name', [
556
+ { name: 'id', type: 'string', required: true },
557
+ { name: 'name', type: 'string', required: true },
558
+ { name: 'namedAt', type: 'uint', required: true },
559
+ { name: 'namedBy', type: 'fixed32' }
560
+ ]);
561
+ versioned('name-mesh-operation', [
562
+ { name: 'name', type: 'string', required: true },
563
+ { name: 'namedAt', type: 'uint', required: true }
564
+ ]);
551
565
  }
552
566
  export function registerQvacCollections(dbns, helpersPath) {
553
567
  dbns.require(helpersPath);
@@ -877,6 +891,7 @@ export function registerQvacCollections(dbns, helpersPath) {
877
891
  });
878
892
  dbns.collections.register({ name: 'mesh-founders', schema: '@qvac/mesh-founder', key: ['id'] });
879
893
  dbns.collections.register({ name: 'purged-chats', schema: '@qvac/purged-chat', key: ['chatId'] });
894
+ dbns.collections.register({ name: 'mesh-names', schema: '@qvac/mesh-name', key: ['id'] });
880
895
  }
881
896
  export function registerQvacDispatch(ds) {
882
897
  ds.register({ name: 'append-chunk', requestType: '@qvac/append-chunk-operation' });
@@ -942,4 +957,5 @@ export function registerQvacDispatch(ds) {
942
957
  ds.register({ name: 'reindex-devices', requestType: '@qvac/reindex-devices-operation' });
943
958
  ds.register({ name: 'recover-device', requestType: '@qvac/recovery-claim' });
944
959
  ds.register({ name: 'purge-chat', requestType: '@qvac/purge-chat-operation' });
960
+ ds.register({ name: 'name-mesh', requestType: '@qvac/name-mesh-operation' });
945
961
  }