@qvac/core 0.3.3 → 0.4.0

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 (169) hide show
  1. package/README.md +19 -17
  2. package/dist-lib/lib/abort-controller-node.d.ts +4 -0
  3. package/dist-lib/lib/abort-controller-node.js +1 -0
  4. package/dist-lib/lib/api/agents.d.ts +3 -3
  5. package/dist-lib/lib/api/agents.js +31 -20
  6. package/dist-lib/lib/api/approval-rules.d.ts +2 -2
  7. package/dist-lib/lib/api/approval-rules.js +2 -2
  8. package/dist-lib/lib/api/attachments.d.ts +2 -2
  9. package/dist-lib/lib/api/attachments.js +14 -25
  10. package/dist-lib/lib/api/chat-groups.d.ts +2 -2
  11. package/dist-lib/lib/api/chat-groups.js +9 -9
  12. package/dist-lib/lib/api/chat.d.ts +4 -2
  13. package/dist-lib/lib/api/chat.js +18 -18
  14. package/dist-lib/lib/api/chunks.d.ts +2 -2
  15. package/dist-lib/lib/api/chunks.js +27 -36
  16. package/dist-lib/lib/api/current-mesh.d.ts +7 -15
  17. package/dist-lib/lib/api/current-mesh.js +7 -22
  18. package/dist-lib/lib/api/devices.d.ts +4 -3
  19. package/dist-lib/lib/api/devices.js +18 -18
  20. package/dist-lib/lib/api/execution.d.ts +2 -2
  21. package/dist-lib/lib/api/execution.js +6 -6
  22. package/dist-lib/lib/api/files.d.ts +2 -2
  23. package/dist-lib/lib/api/files.js +4 -4
  24. package/dist-lib/lib/api/index.d.ts +12 -11
  25. package/dist-lib/lib/api/index.js +15 -13
  26. package/dist-lib/lib/api/inference-calls.d.ts +1 -1
  27. package/dist-lib/lib/api/inference-calls.js +2 -2
  28. package/dist-lib/lib/api/knowledge.d.ts +2 -2
  29. package/dist-lib/lib/api/knowledge.js +22 -11
  30. package/dist-lib/lib/api/local-files.d.ts +3 -0
  31. package/dist-lib/lib/api/local-files.js +23 -0
  32. package/dist-lib/lib/api/members.d.ts +6 -2
  33. package/dist-lib/lib/api/members.js +24 -17
  34. package/dist-lib/lib/api/mesh.d.ts +4 -5
  35. package/dist-lib/lib/api/mesh.js +7 -9
  36. package/dist-lib/lib/api/notifications.d.ts +2 -13
  37. package/dist-lib/lib/api/notifications.js +20 -52
  38. package/dist-lib/lib/api/rag.d.ts +2 -2
  39. package/dist-lib/lib/api/rag.js +4 -4
  40. package/dist-lib/lib/api/range.d.ts +11 -0
  41. package/dist-lib/lib/api/range.js +10 -0
  42. package/dist-lib/lib/api/sync.d.ts +2 -2
  43. package/dist-lib/lib/api/sync.js +3 -3
  44. package/dist-lib/lib/api/tool-activity.d.ts +2 -2
  45. package/dist-lib/lib/api/tool-activity.js +11 -10
  46. package/dist-lib/lib/api/tool-configs.d.ts +2 -2
  47. package/dist-lib/lib/api/tool-configs.js +6 -7
  48. package/dist-lib/lib/api/upload-frames.d.ts +9 -0
  49. package/dist-lib/lib/api/upload-frames.js +17 -0
  50. package/dist-lib/lib/api/watchable.d.ts +1 -6
  51. package/dist-lib/lib/api/watchable.js +4 -13
  52. package/dist-lib/lib/assistant/history.d.ts +3 -3
  53. package/dist-lib/lib/assistant-client.js +3 -2
  54. package/dist-lib/lib/assistant-tree.js +1 -1
  55. package/dist-lib/lib/assistant.d.ts +16 -14
  56. package/dist-lib/lib/assistant.js +166 -137
  57. package/dist-lib/lib/collabora/convert.js +1 -1
  58. package/dist-lib/lib/core/agent.d.ts +5 -7
  59. package/dist-lib/lib/core/agent.js +26 -6
  60. package/dist-lib/lib/core/archived-mesh.js +1 -0
  61. package/dist-lib/lib/core/chat-order.d.ts +20 -0
  62. package/dist-lib/lib/core/chat-order.js +96 -0
  63. package/dist-lib/lib/core/chat.d.ts +0 -5
  64. package/dist-lib/lib/core/chat.js +1 -3
  65. package/dist-lib/lib/core/chunk.d.ts +7 -14
  66. package/dist-lib/lib/core/chunk.js +17 -35
  67. package/dist-lib/lib/core/contract/agents.js +8 -0
  68. package/dist-lib/lib/core/contract/compute.d.ts +1 -1
  69. package/dist-lib/lib/core/contract/compute.js +1 -0
  70. package/dist-lib/lib/core/contract/joined-meshes.js +4 -3
  71. package/dist-lib/lib/core/contract/members.js +4 -3
  72. package/dist-lib/lib/core/contract/writers.js +5 -4
  73. package/dist-lib/lib/core/departures.js +5 -4
  74. package/dist-lib/lib/core/device-invites.js +2 -1
  75. package/dist-lib/lib/core/files/attachments.d.ts +8 -1
  76. package/dist-lib/lib/core/files/index.d.ts +4 -4
  77. package/dist-lib/lib/core/files/index.js +18 -8
  78. package/dist-lib/lib/core/files/local-index.d.ts +16 -0
  79. package/dist-lib/lib/core/files/local-index.js +38 -0
  80. package/dist-lib/lib/core/files/processing/document.js +2 -1
  81. package/dist-lib/lib/core/files/processing/ownership.js +6 -3
  82. package/dist-lib/lib/core/files/records/chunks.js +1 -0
  83. package/dist-lib/lib/core/files/records/reclaim.js +1 -0
  84. package/dist-lib/lib/core/files/records/routing.js +2 -1
  85. package/dist-lib/lib/core/files/store/blob.d.ts +1 -1
  86. package/dist-lib/lib/core/files/store/blob.js +6 -3
  87. package/dist-lib/lib/core/files/store/index.d.ts +1 -1
  88. package/dist-lib/lib/core/files/store/reference.js +4 -3
  89. package/dist-lib/lib/core/files/store/server.d.ts +8 -4
  90. package/dist-lib/lib/core/files/store/server.js +16 -12
  91. package/dist-lib/lib/core/files/store/sweep.js +2 -1
  92. package/dist-lib/lib/core/files/upload.d.ts +1 -1
  93. package/dist-lib/lib/core/hosted-agents.js +1 -1
  94. package/dist-lib/lib/core/image/cache-sweep.d.ts +1 -0
  95. package/dist-lib/lib/core/image/cache-sweep.js +7 -3
  96. package/dist-lib/lib/core/local/files.d.ts +24 -0
  97. package/dist-lib/lib/core/local/files.js +154 -0
  98. package/dist-lib/lib/core/local/index.d.ts +21 -5
  99. package/dist-lib/lib/core/local/index.js +75 -45
  100. package/dist-lib/lib/core/membership.d.ts +4 -1
  101. package/dist-lib/lib/core/membership.js +17 -2
  102. package/dist-lib/lib/core/mesh.d.ts +5 -0
  103. package/dist-lib/lib/core/mesh.js +11 -5
  104. package/dist-lib/lib/core/migrations/agent-indexes.d.ts +2 -0
  105. package/dist-lib/lib/core/migrations/agent-indexes.js +30 -0
  106. package/dist-lib/lib/core/migrations/file-index.d.ts +2 -0
  107. package/dist-lib/lib/core/migrations/file-index.js +27 -0
  108. package/dist-lib/lib/core/migrations/index.js +3 -1
  109. package/dist-lib/lib/core/notification.js +5 -5
  110. package/dist-lib/lib/core/range.d.ts +18 -0
  111. package/dist-lib/lib/core/range.js +17 -0
  112. package/dist-lib/lib/core/sync/index.js +39 -32
  113. package/dist-lib/lib/core/sync-status.js +3 -2
  114. package/dist-lib/lib/core/tool-activity.d.ts +2 -5
  115. package/dist-lib/lib/core/tool-activity.js +4 -7
  116. package/dist-lib/lib/core.d.ts +48 -29
  117. package/dist-lib/lib/core.js +465 -359
  118. package/dist-lib/lib/knowledge-search.d.ts +2 -2
  119. package/dist-lib/lib/kv-cache.js +1 -1
  120. package/dist-lib/lib/log.js +2 -3
  121. package/dist-lib/lib/mesh-scoped-engine.d.ts +8 -1
  122. package/dist-lib/lib/mesh-scoped-engine.js +62 -15
  123. package/dist-lib/lib/model-download.d.ts +14 -5
  124. package/dist-lib/lib/model-download.js +25 -17
  125. package/dist-lib/lib/oauth/token-exchange-core.js +1 -1
  126. package/dist-lib/lib/run-transcript.d.ts +2 -2
  127. package/dist-lib/lib/run-transcript.js +3 -2
  128. package/dist-lib/lib/task.d.ts +1 -0
  129. package/dist-lib/lib/task.js +3 -2
  130. package/dist-lib/schema/build.js +4 -10
  131. package/dist-lib/schema/engine.js +261 -91
  132. package/dist-lib/schema/helpers.d.ts +4 -0
  133. package/dist-lib/schema/helpers.js +20 -0
  134. package/dist-lib/schema/local.d.ts +1 -1
  135. package/dist-lib/schema/local.js +70 -2
  136. package/dist-lib/schema/qvac.js +30 -0
  137. package/dist-lib/spec/bind.js +20 -3
  138. package/dist-lib/spec/calls.js +19 -15
  139. package/dist-lib/spec/capabilities.d.ts +42 -34
  140. package/dist-lib/spec/hrpc/hrpc.json +82 -30
  141. package/dist-lib/spec/hrpc/index.js +106 -57
  142. package/dist-lib/spec/hrpc/messages.js +5420 -5019
  143. package/dist-lib/spec/hyperschema/index.js +5424 -5023
  144. package/dist-lib/spec/hyperschema/schema.json +3202 -2666
  145. package/dist-lib/spec/hyperschema/types.d.ts +455 -319
  146. package/dist-lib/spec/local/hyperdb/db.json +119 -1
  147. package/dist-lib/spec/local/hyperdb/index.js +414 -2
  148. package/dist-lib/spec/local/hyperdb/messages.js +505 -124
  149. package/dist-lib/spec/local/hyperschema/index.js +203 -5
  150. package/dist-lib/spec/local/hyperschema/schema.json +136 -1
  151. package/dist-lib/spec/local/hyperschema/types.d.ts +32 -1
  152. package/dist-lib/spec/mesh/hyperdb/db.json +55 -3
  153. package/dist-lib/spec/mesh/hyperdb/index.js +119 -3
  154. package/dist-lib/spec/mesh/hyperdb/messages.js +244 -172
  155. package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +7 -1
  156. package/dist-lib/spec/mesh/hyperdispatch/index.js +18 -1
  157. package/dist-lib/spec/mesh/hyperdispatch/messages.js +74 -2
  158. package/dist-lib/spec/mesh/hyperschema/index.js +74 -2
  159. package/dist-lib/spec/mesh/hyperschema/schema.json +27 -1
  160. package/dist-lib/spec/mesh/hyperschema/types.d.ts +9 -1
  161. package/package.json +10 -6
  162. package/tui/app.mjs +376 -151
  163. package/tui/boot.mjs +2 -1
  164. package/tui/lib/busy-words.mjs +2 -1
  165. package/tui/lib/knowledge-command.mjs +11 -10
  166. package/tui/lib/knowledge-import.mjs +7 -5
  167. package/tui/lib/render.mjs +4 -3
  168. package/tui/lib/usage.mjs +2 -1
  169. package/tui/main.mjs +2 -1
@@ -1,7 +1,8 @@
1
1
  import CorestoreStorage from 'hypercore-storage';
2
2
  import HyperDB from 'hyperdb';
3
- import type { LocalAccountRecovery, LocalAppConfig, LocalCapabilityProfile, LocalChatNotify, LocalCustomModel, LocalIdentity, LocalMemberProof, LocalMeshDeparture, LocalMeshMembership, LocalModelDownload, LocalModelNotify } from '../../../spec/local/hyperschema/types.d.ts';
3
+ import type { LocalAccountRecovery, LocalAppConfig, LocalCapabilityProfile, LocalChatNotify, LocalChatOrder, LocalCustomModel, LocalIdentity, LocalMemberProof, LocalMeshDeparture, LocalMeshMembership, LocalModelDownload, LocalModelNotify } from '../../../spec/local/hyperschema/types.d.ts';
4
4
  import type { DeviceMetadata } from '../mesh.ts';
5
+ import { LocalFiles } from './files.ts';
5
6
  export interface ResolvedSession {
6
7
  readonly seed: Buffer;
7
8
  readonly baseKey: Buffer | undefined;
@@ -12,6 +13,7 @@ export declare function openLocalStore(storage: CorestoreStorage): LocalStore;
12
13
  declare class LocalStore {
13
14
  private readonly _db;
14
15
  private readonly _writeLock;
16
+ readonly files: LocalFiles;
15
17
  constructor(db: HyperDB);
16
18
  private _locked;
17
19
  get(key: string): Promise<unknown>;
@@ -27,6 +29,19 @@ declare class LocalStore {
27
29
  getChatNotify(chatId: string): Promise<LocalChatNotify | null>;
28
30
  ackChatNotify(chatId: string, seq: number): Promise<void>;
29
31
  clearChatNotify(chatId: string): Promise<void>;
32
+ writeChatOrder(meshKey: Buffer, rows: ReadonlyArray<{
33
+ chatId: string;
34
+ sortKey: Buffer;
35
+ }>, gone?: ReadonlyArray<string>): Promise<void>;
36
+ listChatOrder(meshKey: Buffer, { limit, before }?: {
37
+ limit?: number;
38
+ before?: {
39
+ sortKey: Buffer;
40
+ chatId: string;
41
+ };
42
+ }): Promise<LocalChatOrder[]>;
43
+ private _dropChatOrder;
44
+ private _chatOrderIn;
30
45
  beginModelTransfer(name: string, { transferred }?: {
31
46
  transferred?: boolean;
32
47
  }): Promise<LocalModelNotify>;
@@ -47,7 +62,7 @@ declare class LocalStore {
47
62
  renameDevice(publicKey: Buffer, name: string): Promise<void>;
48
63
  saveDeviceProfile(publicKey: Buffer, capabilityProfile: LocalCapabilityProfile, recommendedModelName: string, platform?: string): Promise<void>;
49
64
  readIdentity(): Promise<LocalIdentity | null>;
50
- saveDisplayName(displayName: string): Promise<LocalIdentity | null>;
65
+ saveDisplayName(displayName: string, displayNameAt?: number): Promise<LocalIdentity | null>;
51
66
  readMemberProof(meshKey: Buffer): Promise<LocalMemberProof | null>;
52
67
  recordMemberProof(link: LocalMemberProof): Promise<void>;
53
68
  readAccountRecovery(meshKey: Buffer): Promise<LocalAccountRecovery | null>;
@@ -72,21 +87,22 @@ declare class LocalStore {
72
87
  recordMesh(deviceId: Buffer, key: Buffer, session: ResolvedSession, { account }?: {
73
88
  account?: boolean;
74
89
  }): Promise<void>;
75
- setActiveMesh(deviceId: Buffer, key: Buffer): Promise<void>;
90
+ setAccountMesh(deviceId: Buffer, key: Buffer): Promise<void>;
76
91
  recordDeparture(key: Buffer, opts: {
77
92
  creator: boolean;
78
93
  }): Promise<void>;
79
94
  departures(): Promise<LocalMeshDeparture[]>;
80
95
  deleteDeparture(key: Buffer): Promise<void>;
81
96
  archiveMesh(key: Buffer): Promise<void>;
97
+ forgetMesh(key: Buffer): Promise<void>;
82
98
  markMeshMigrated(key: Buffer, migratedAt: number): Promise<void>;
83
- adoptHomeMesh(deviceId: Buffer, key: Buffer, { account }: {
99
+ adoptAccountMesh(deviceId: Buffer, key: Buffer, { account }: {
84
100
  account?: Buffer | null;
85
101
  }): Promise<void>;
86
102
  private _pointersAt;
87
103
  listMeshes(): Promise<LocalMeshMembership[]>;
88
104
  listArchivedMeshes(): Promise<LocalMeshMembership[]>;
89
- homeMesh(deviceId: Buffer): Promise<LocalMeshMembership | null>;
105
+ accountMesh(deviceId: Buffer): Promise<LocalMeshMembership | null>;
90
106
  readMeshSession(key: Buffer): Promise<ResolvedSession | null>;
91
107
  watch(listener: () => void): void;
92
108
  unwatch(listener: () => void): void;
@@ -5,6 +5,10 @@ import ScopeLock from 'scope-lock';
5
5
  import { MAX_DEVICE_NAME_CHARS, deviceNameWithinBounds } from '../../../schema/qvac.js';
6
6
  import { generateMnemonic, mintIdentity } from '../identity.js';
7
7
  import LocalDatabaseSpec from '../../../spec/local/hyperdb/index.js';
8
+ import b4a from 'b4a';
9
+ import { dropMeshFiles, LocalFiles } from './files.js';
10
+ const CHAT_ORDER = '@local/chat-order';
11
+ const CHAT_ORDER_BY_KEY = '@local/chat-order-by-key';
8
12
  const CONFIG = '@local/config';
9
13
  const ACTIVE_MESH = '@local/active-mesh';
10
14
  const MESH_MEMBERSHIP = '@local/mesh-membership';
@@ -25,7 +29,7 @@ const CHAT_NOTIFY = '@local/chat-notify';
25
29
  const MODEL_NOTIFY = '@local/model-notify';
26
30
  const APP_CONFIG_ID = 'default';
27
31
  function freshWriterSeed() {
28
- return `local-${crypto.randomBytes(8).toString('hex')}`;
32
+ return `local-${b4a.toHex(crypto.randomBytes(8))}`;
29
33
  }
30
34
  function sessionOf(row) {
31
35
  return {
@@ -42,8 +46,10 @@ export function openLocalStore(storage) {
42
46
  class LocalStore {
43
47
  _db;
44
48
  _writeLock = new ScopeLock();
49
+ files;
45
50
  constructor(db) {
46
51
  this._db = db;
52
+ this.files = new LocalFiles(db, (fn) => this._locked(fn));
47
53
  }
48
54
  async _locked(fn) {
49
55
  await this._writeLock.lock();
@@ -125,6 +131,41 @@ class LocalStore {
125
131
  await tx.flush();
126
132
  });
127
133
  }
134
+ writeChatOrder(meshKey, rows, gone = []) {
135
+ return this._locked(async () => {
136
+ const tx = this._db.transaction();
137
+ for (const row of rows) {
138
+ await tx.insert(CHAT_ORDER, { meshKey, ...row });
139
+ }
140
+ for (const chatId of gone)
141
+ await tx.delete(CHAT_ORDER, { meshKey, chatId });
142
+ await tx.flush();
143
+ });
144
+ }
145
+ listChatOrder(meshKey, { limit, before } = {}) {
146
+ return this._db
147
+ .find(CHAT_ORDER_BY_KEY, {
148
+ gte: { meshKey },
149
+ lt: before === undefined ? undefined : { meshKey, ...before },
150
+ lte: before === undefined ? { meshKey } : undefined,
151
+ reverse: true,
152
+ limit
153
+ })
154
+ .toArray();
155
+ }
156
+ async _dropChatOrder(tx, meshKey) {
157
+ for (const order of await this._chatOrderIn(tx, meshKey)) {
158
+ await tx.delete(CHAT_ORDER, { meshKey, chatId: order.chatId });
159
+ }
160
+ }
161
+ _chatOrderIn(db, meshKey) {
162
+ return db
163
+ .find(CHAT_ORDER_BY_KEY, {
164
+ gte: { meshKey },
165
+ lte: { meshKey }
166
+ })
167
+ .toArray();
168
+ }
128
169
  beginModelTransfer(name, { transferred = false } = {}) {
129
170
  return this._locked(async () => {
130
171
  const current = await this._db.get(MODEL_NOTIFY, { name });
@@ -133,7 +174,7 @@ class LocalStore {
133
174
  return current;
134
175
  const next = {
135
176
  name,
136
- transferId: pending ? current.transferId : crypto.randomBytes(16).toString('hex'),
177
+ transferId: pending ? current.transferId : b4a.toHex(crypto.randomBytes(16)),
137
178
  notificationId: current?.notificationId,
138
179
  completedAt: current?.completedAt,
139
180
  acknowledged: current?.acknowledged,
@@ -278,7 +319,7 @@ class LocalStore {
278
319
  readIdentity() {
279
320
  return this._db.get(IDENTITY, { id: IDENTITY_ID });
280
321
  }
281
- saveDisplayName(displayName) {
322
+ saveDisplayName(displayName, displayNameAt = Date.now()) {
282
323
  return this._locked(async () => {
283
324
  const tx = this._db.transaction();
284
325
  const existing = await tx.get(IDENTITY, { id: IDENTITY_ID });
@@ -286,7 +327,7 @@ class LocalStore {
286
327
  await tx.close();
287
328
  return null;
288
329
  }
289
- const identity = { ...existing, displayName };
330
+ const identity = { ...existing, displayName, displayNameAt };
290
331
  await tx.insert(IDENTITY, identity);
291
332
  await tx.flush();
292
333
  return identity;
@@ -322,6 +363,7 @@ class LocalStore {
322
363
  await tx.insert(IDENTITY, {
323
364
  id: IDENTITY_ID,
324
365
  displayName: existing?.displayName ?? null,
366
+ displayNameAt: existing?.displayNameAt,
325
367
  ...identity
326
368
  });
327
369
  await tx.flush();
@@ -348,8 +390,9 @@ class LocalStore {
348
390
  }
349
391
  async resolveSession(deviceId, opts = {}) {
350
392
  const current = await this._activeMembership(deviceId);
351
- const sameSeed = Boolean(opts.seed && current?.seed.equals(opts.seed));
352
- if (opts.seed && !sameSeed) {
393
+ const named = opts.baseKey ? await this._membership(opts.baseKey) : null;
394
+ const resumed = [current, named].find((row) => row && opts.seed?.equals(row.seed)) ?? null;
395
+ if (opts.seed && !resumed) {
353
396
  return {
354
397
  seed: opts.seed,
355
398
  baseKey: opts.baseKey,
@@ -357,8 +400,9 @@ class LocalStore {
357
400
  compute: opts.compute ?? false
358
401
  };
359
402
  }
360
- if (current && !opts.new)
361
- return sessionOf(current);
403
+ const session = resumed ?? current;
404
+ if (session && !opts.new)
405
+ return sessionOf(session);
362
406
  return {
363
407
  seed: crypto.randomBytes(32),
364
408
  baseKey: undefined,
@@ -378,8 +422,7 @@ class LocalStore {
378
422
  const pointer = await this._db.get(ACTIVE_MESH, { deviceId });
379
423
  if (!pointer)
380
424
  return this._migrateLegacySession(deviceId);
381
- return ((await this._membership(pointer.activeMeshKey)) ??
382
- (await this._membership(pointer.homeMeshKey)));
425
+ return this._membership(pointer.homeMeshKey);
383
426
  }
384
427
  async _membership(key) {
385
428
  if (!key)
@@ -397,11 +440,7 @@ class LocalStore {
397
440
  return legacy;
398
441
  const tx = this._db.transaction();
399
442
  await tx.insert(MESH_MEMBERSHIP, { ...legacy, key });
400
- await tx.insert(ACTIVE_MESH, {
401
- deviceId,
402
- activeMeshKey: key,
403
- homeMeshKey: key
404
- });
443
+ await tx.insert(ACTIVE_MESH, { deviceId, homeMeshKey: key });
405
444
  const shipped = await tx.get(MEMBERSHIP, { id: MEMBERSHIP_ID });
406
445
  if (shipped) {
407
446
  const { memberId, proof, attestedAt } = shipped;
@@ -430,7 +469,6 @@ class LocalStore {
430
469
  return this._locked(async () => {
431
470
  const existing = await this._db.get(MESH_MEMBERSHIP, { key });
432
471
  const minted = account && !existing && session.baseKey === undefined;
433
- const pointer = minted ? await this._db.get(ACTIVE_MESH, { deviceId }) : null;
434
472
  const tx = this._db.transaction();
435
473
  await tx.insert(MESH_MEMBERSHIP, {
436
474
  key,
@@ -439,24 +477,15 @@ class LocalStore {
439
477
  compute: session.compute
440
478
  });
441
479
  if (minted) {
442
- await tx.insert(ACTIVE_MESH, {
443
- deviceId,
444
- activeMeshKey: pointer?.activeMeshKey,
445
- homeMeshKey: key
446
- });
480
+ await tx.insert(ACTIVE_MESH, { deviceId, homeMeshKey: key });
447
481
  }
448
482
  await tx.flush();
449
483
  });
450
484
  }
451
- setActiveMesh(deviceId, key) {
485
+ setAccountMesh(deviceId, key) {
452
486
  return this._locked(async () => {
453
- const pointer = await this._db.get(ACTIVE_MESH, { deviceId });
454
487
  const tx = this._db.transaction();
455
- await tx.insert(ACTIVE_MESH, {
456
- deviceId,
457
- activeMeshKey: key,
458
- homeMeshKey: pointer?.homeMeshKey
459
- });
488
+ await tx.insert(ACTIVE_MESH, { deviceId, homeMeshKey: key });
460
489
  await tx.flush();
461
490
  });
462
491
  }
@@ -504,6 +533,16 @@ class LocalStore {
504
533
  }
505
534
  for (const pointer of cleared)
506
535
  await tx.insert(ACTIVE_MESH, pointer);
536
+ await this._dropChatOrder(tx, key);
537
+ await dropMeshFiles(tx, key);
538
+ await tx.flush();
539
+ });
540
+ }
541
+ forgetMesh(key) {
542
+ return this._locked(async () => {
543
+ const tx = this._db.transaction();
544
+ await this._dropChatOrder(tx, key);
545
+ await dropMeshFiles(tx, key);
507
546
  await tx.flush();
508
547
  });
509
548
  }
@@ -517,25 +556,22 @@ class LocalStore {
517
556
  await tx.flush();
518
557
  });
519
558
  }
520
- adoptHomeMesh(deviceId, key, { account }) {
559
+ adoptAccountMesh(deviceId, key, { account }) {
521
560
  return this._locked(async () => {
522
561
  const departed = [];
523
562
  for await (const row of this._db.find(MESH_MEMBERSHIP)) {
524
563
  if (!row.key.equals(key) && !row.leftAt)
525
564
  departed.push(row);
526
565
  }
527
- const pointer = await this._db.get(ACTIVE_MESH, { deviceId });
528
566
  const leftAt = Date.now();
529
567
  const tx = this._db.transaction();
530
568
  for (const row of departed) {
531
569
  const wasHome = !!account?.equals(row.key);
532
570
  await tx.insert(MESH_MEMBERSHIP, { ...row, leftAt, wasHome });
571
+ await this._dropChatOrder(tx, row.key);
572
+ await dropMeshFiles(tx, row.key);
533
573
  }
534
- await tx.insert(ACTIVE_MESH, {
535
- deviceId,
536
- activeMeshKey: pointer?.activeMeshKey,
537
- homeMeshKey: key
538
- });
574
+ await tx.insert(ACTIVE_MESH, { deviceId, homeMeshKey: key });
539
575
  await tx.flush();
540
576
  });
541
577
  }
@@ -543,16 +579,10 @@ class LocalStore {
543
579
  const cleared = [];
544
580
  let home = false;
545
581
  for await (const pointer of this._db.find(ACTIVE_MESH)) {
546
- const active = pointer.activeMeshKey?.equals(key);
547
- const aimed = pointer.homeMeshKey?.equals(key);
548
- if (!active && !aimed)
582
+ if (!pointer.homeMeshKey?.equals(key))
549
583
  continue;
550
- home ||= !!aimed;
551
- cleared.push({
552
- deviceId: pointer.deviceId,
553
- activeMeshKey: active ? null : pointer.activeMeshKey,
554
- homeMeshKey: aimed ? null : pointer.homeMeshKey
555
- });
584
+ home = true;
585
+ cleared.push({ deviceId: pointer.deviceId, homeMeshKey: null });
556
586
  }
557
587
  return { cleared, home };
558
588
  }
@@ -572,7 +602,7 @@ class LocalStore {
572
602
  }
573
603
  return rows;
574
604
  }
575
- async homeMesh(deviceId) {
605
+ async accountMesh(deviceId) {
576
606
  const pointer = await this._db.get(ACTIVE_MESH, { deviceId });
577
607
  return this._membership(pointer?.homeMeshKey);
578
608
  }
@@ -18,7 +18,10 @@ export declare function attestUnattestedDevices(mesh: Mesh, { deviceKeyPair, log
18
18
  export interface KnownMember {
19
19
  readonly id: Buffer;
20
20
  readonly name: string;
21
+ readonly updatedAt: number;
21
22
  readonly proof: Buffer;
22
23
  }
23
24
  export declare function claimOwnMembership(mesh: Mesh, { deviceKeyPair, log }: MembershipContext, known: KnownMember): Promise<void>;
24
- export declare function renameFoundedMember(mesh: Mesh, { deviceKeyPair, log }: MembershipContext, identity: LocalIdentity): Promise<void>;
25
+ export declare function namedAt(identity: Pick<LocalIdentity, 'displayNameAt'>): number;
26
+ export declare function legacyNameStamp(account: Mesh, { deviceKeyPair }: MembershipContext, identity: LocalIdentity): Promise<number | null>;
27
+ export declare function renameFoundedMember(mesh: Mesh, { deviceKeyPair, log }: MembershipContext, identity: LocalIdentity, adopt: (name: string, namedAt: number) => Promise<unknown>): Promise<void>;
@@ -62,7 +62,17 @@ export async function claimOwnMembership(mesh, { deviceKeyPair, log }, known) {
62
62
  await mesh.refreshOurMember();
63
63
  log?.info('membership', 'claimed our own member in a mesh a sibling device joined');
64
64
  }
65
- export async function renameFoundedMember(mesh, { deviceKeyPair, log }, identity) {
65
+ export function namedAt(identity) {
66
+ return identity.displayNameAt || 0;
67
+ }
68
+ export async function legacyNameStamp(account, { deviceKeyPair }, identity) {
69
+ const own = await account.view.get('@qvac/devices', { id: deviceKeyPair.publicKey });
70
+ if (!own?.memberId)
71
+ return null;
72
+ const member = await account.view.get('@qvac/members', { id: own.memberId });
73
+ return member && member.name === identity.displayName ? member.updatedAt : null;
74
+ }
75
+ export async function renameFoundedMember(mesh, { deviceKeyPair, log }, identity, adopt) {
66
76
  if (!identity.displayName)
67
77
  return;
68
78
  const own = await mesh.view.get('@qvac/devices', { id: deviceKeyPair.publicKey });
@@ -71,8 +81,13 @@ export async function renameFoundedMember(mesh, { deviceKeyPair, log }, identity
71
81
  const member = await mesh.view.get('@qvac/members', { id: own.memberId });
72
82
  if (!member || member.name === identity.displayName)
73
83
  return;
84
+ if (namedAt(identity) < member.updatedAt) {
85
+ await adopt(member.name, member.updatedAt);
86
+ log?.info('membership', 'another device of this member renamed it later, so this one follows');
87
+ return;
88
+ }
74
89
  await mesh.dispatch('update-member', {
75
- member: { ...member, name: identity.displayName, updatedAt: Date.now() }
90
+ member: { ...member, name: identity.displayName, updatedAt: namedAt(identity) }
76
91
  });
77
92
  log?.info('membership', 'the member this device founded carries its user name');
78
93
  }
@@ -11,6 +11,7 @@ import { Router } from '../../spec/mesh/hyperdispatch/index.js';
11
11
  import type { QvacCapabilityProfile, QvacMember } from '../../spec/mesh/hyperschema/types.d.ts';
12
12
  import type { BlindPeeringOpts } from './blind-peers.ts';
13
13
  import type { AttachmentUrl } from './attachment-link.ts';
14
+ import type { MeshFileIndex } from './files/local-index.ts';
14
15
  import { type DeviceAttestation } from './device-attestation.ts';
15
16
  import { type SyncActivity } from './sync-status.ts';
16
17
  export interface MeshView {
@@ -38,6 +39,7 @@ export interface MeshOptions {
38
39
  bootFrom?: AutobeeOplogRef | null;
39
40
  wakeup?: Wakeup;
40
41
  attachmentUrl?: AttachmentUrl | null;
42
+ fileIndex?: MeshFileIndex | null;
41
43
  }
42
44
  export declare function isOurTrustedWriter(snap: HyperDB, writerKey: Buffer, ourMemberId: Buffer | null): Promise<boolean>;
43
45
  export type BirthRow = {
@@ -79,6 +81,7 @@ export declare class Mesh extends ReadyResource {
79
81
  private readonly _bootFrom;
80
82
  private readonly _wakeup;
81
83
  readonly attachmentUrl: AttachmentUrl | null;
84
+ readonly fileIndex: MeshFileIndex | null;
82
85
  private readonly _scope;
83
86
  readonly router: Router;
84
87
  private readonly _keyPair;
@@ -142,6 +145,8 @@ export declare class Mesh extends ReadyResource {
142
145
  pendingUploads(): number;
143
146
  suspend(): Promise<void>;
144
147
  resume(): Promise<void>;
148
+ syncChatOrder: ((chatIds: Set<string>) => Promise<void>) | null;
149
+ chatOrderBuilt: Promise<void>;
145
150
  watch(listener: () => void): void;
146
151
  unwatch(listener: () => void): void;
147
152
  _autobeeIsTrusted(key: Buffer, reference: MeshView): Promise<boolean>;
@@ -19,6 +19,7 @@ import { assertComputeRoute } from './contract/compute.js';
19
19
  import { registerRoutes } from './contract/index.js';
20
20
  import { createSyncActivity } from './sync-status.js';
21
21
  import { buildHealthAssistant } from './health-assistant.js';
22
+ import b4a from 'b4a';
22
23
  const [MESH_KEYPAIR, MESH_ENCRYPTION, MESH_DEVICE] = crypto.namespace('qvac-assistant/mesh', 3);
23
24
  const LIVE_WRITERS_BY_MOST_RECENT = '@qvac/live-writers-by-most-recent';
24
25
  const WARMUP_COLLECTIONS = [
@@ -61,6 +62,7 @@ export class Mesh extends ReadyResource {
61
62
  _bootFrom;
62
63
  _wakeup;
63
64
  attachmentUrl;
65
+ fileIndex;
64
66
  _scope;
65
67
  router;
66
68
  _keyPair;
@@ -111,7 +113,7 @@ export class Mesh extends ReadyResource {
111
113
  if (!peering?.blindPeers)
112
114
  return [];
113
115
  return [...peering.blindPeers.values()].map((bp) => ({
114
- key: bp.remotePublicKey.toString('hex').slice(0, 16),
116
+ key: b4a.toHex(bp.remotePublicKey).slice(0, 16),
115
117
  connected: bp.connected,
116
118
  connects: bp.connects,
117
119
  uploaded: bp.uploaded
@@ -182,12 +184,13 @@ export class Mesh extends ReadyResource {
182
184
  this._deviceId = opts.deviceId ?? null;
183
185
  this._wakeup = opts.wakeup;
184
186
  this.attachmentUrl = opts.attachmentUrl ?? null;
187
+ this.fileIndex = opts.fileIndex ?? null;
185
188
  this.router = new Router();
186
189
  this._keyPair = crypto.keyPair(crypto.hash([MESH_KEYPAIR, this._seed]));
187
190
  this._deviceKeyPair = crypto.keyPair(crypto.hash([MESH_DEVICE, this._seed]));
188
191
  this._encryptionKey = crypto.hash([MESH_ENCRYPTION, this._seed]);
189
192
  this.discoveryKey = crypto.discoveryKey(this._keyPair.publicKey);
190
- this._scope = ['p2p', 'mesh', this._keyPair.publicKey.toString('hex').slice(0, 8)];
193
+ this._scope = ['p2p', 'mesh', b4a.toHex(this._keyPair.publicKey).slice(0, 8)];
191
194
  registerRoutes(this.router, { compute: this._compute });
192
195
  }
193
196
  async _open() {
@@ -211,7 +214,8 @@ export class Mesh extends ReadyResource {
211
214
  encryptionKey: this._encryptionKey,
212
215
  wakeupCapability: { key: this._keyPair.publicKey, discoveryKey: this.discoveryKey },
213
216
  wakeup: this._wakeup,
214
- fastForward: { boot: this._bootFrom ? { head: this._bootFrom } : null }
217
+ fastForward: { boot: this._bootFrom ? { head: this._bootFrom } : null },
218
+ strictReindex: true
215
219
  });
216
220
  this.autobee.on('error', (err) => this._log?.warn(this._scope, 'autobee error', err));
217
221
  this.autobee.on('move-to', (to, from) => this._log?.info(this._scope, 'fast-forwarded onto a head', {
@@ -406,6 +410,8 @@ export class Mesh extends ReadyResource {
406
410
  await this._blindPeering.resume();
407
411
  this._files?.resume();
408
412
  }
413
+ syncChatOrder = null;
414
+ chatOrderBuilt = Promise.resolve();
409
415
  watch(listener) {
410
416
  this.view.watch(listener);
411
417
  }
@@ -420,7 +426,7 @@ export class Mesh extends ReadyResource {
420
426
  const trusted = await isOurTrustedWriter(snap, key, this._ourMemberId);
421
427
  if (!trusted) {
422
428
  this._log?.debug(this._scope, 'writer is no device of ours', {
423
- writerKey: key.toString('hex').slice(0, 16)
429
+ writerKey: b4a.toHex(key).slice(0, 16)
424
430
  });
425
431
  }
426
432
  return trusted;
@@ -439,7 +445,7 @@ export class Mesh extends ReadyResource {
439
445
  })) {
440
446
  if (await isOurTrustedWriter(snap, writer.writerKey, this._ourMemberId)) {
441
447
  this._log?.debug(this._scope, 'vouching for a head', {
442
- writerKey: writer.writerKey.toString('hex').slice(0, 16),
448
+ writerKey: b4a.toHex(writer.writerKey).slice(0, 16),
443
449
  length: writer.length
444
450
  });
445
451
  return { key: writer.writerKey, length: writer.length };
@@ -0,0 +1,2 @@
1
+ import type { Mesh } from '../mesh.ts';
2
+ export declare function reindexAgents(mesh: Mesh): Promise<boolean>;
@@ -0,0 +1,30 @@
1
+ const ACTIVE_AGENTS = '@qvac/active-agents';
2
+ const AGENTS_BY_PROVIDER = '@qvac/agents-by-provider';
3
+ export async function reindexAgents(mesh) {
4
+ if (!mesh.writable)
5
+ return false;
6
+ const missing = await unindexedAgentIds(mesh);
7
+ if (missing.length === 0)
8
+ return false;
9
+ await mesh.dispatch('reindex-agents', { agentIds: missing });
10
+ return true;
11
+ }
12
+ async function unindexedAgentIds(mesh) {
13
+ const snap = mesh.view.snapshot();
14
+ try {
15
+ const indexed = new Set();
16
+ for await (const agent of snap.find(AGENTS_BY_PROVIDER)) {
17
+ indexed.add(agent.id);
18
+ }
19
+ const missing = [];
20
+ for await (const agent of snap.find(ACTIVE_AGENTS)) {
21
+ if (!indexed.has(agent.id)) {
22
+ missing.push(agent.id);
23
+ }
24
+ }
25
+ return missing;
26
+ }
27
+ finally {
28
+ await snap.close();
29
+ }
30
+ }
@@ -0,0 +1,2 @@
1
+ import type { Mesh } from '../mesh.ts';
2
+ export declare function backfillFileIndex(mesh: Mesh): Promise<void>;
@@ -0,0 +1,27 @@
1
+ const CHUNKS = '@qvac/chunks';
2
+ const BATCH = 256;
3
+ export async function backfillFileIndex(mesh) {
4
+ const index = mesh.fileIndex;
5
+ if (!index)
6
+ return;
7
+ const snapshot = mesh.view.snapshot();
8
+ try {
9
+ let batch = [];
10
+ for await (const chunk of snapshot.find(CHUNKS)) {
11
+ if (mesh.closing)
12
+ return;
13
+ if (chunk.type !== 'attachment')
14
+ continue;
15
+ batch.push(chunk);
16
+ if (batch.length < BATCH)
17
+ continue;
18
+ await index.record(batch);
19
+ batch = [];
20
+ }
21
+ if (batch.length > 0)
22
+ await index.record(batch);
23
+ }
24
+ finally {
25
+ await snapshot.close();
26
+ }
27
+ }
@@ -1,9 +1,11 @@
1
1
  import { migrateAgentCatalog } from './agent-catalog.js';
2
2
  import { migrateMembership } from './membership.js';
3
3
  import { sealDeviceRows } from './seal.js';
4
+ import { reindexAgents } from './agent-indexes.js';
4
5
  export async function runMigrations(mesh, context, { catalog = true } = {}) {
5
6
  const membership = await migrateMembership(mesh, context);
6
7
  const agents = catalog && (await migrateAgentCatalog(mesh, context));
7
8
  const seal = await sealDeviceRows(mesh, context);
8
- return membership || agents || seal;
9
+ const indexed = await reindexAgents(mesh);
10
+ return membership || agents || seal || indexed;
9
11
  }
@@ -18,11 +18,11 @@ export async function deriveNotifications(mesh, local, requestedLimit, deviceId)
18
18
  const [statuses, approvals, messages] = await Promise.all([
19
19
  (counter?.count ?? 0) <= ackedThrough
20
20
  ? []
21
- : findChunksOfType(snap, chat.id, 'run-status', { fromSeq: ackedThrough }),
21
+ : findChunksOfType(snap, chat.id, 'run-status', { gte: ackedThrough }),
22
22
  findChunksOfType(snap, chat.id, 'approval-request'),
23
23
  (counter?.count ?? 0) <= ackedThrough
24
24
  ? []
25
- : findChunksOfType(snap, chat.id, 'user-message', { fromSeq: ackedThrough })
25
+ : findChunksOfType(snap, chat.id, 'user-message', { gte: ackedThrough })
26
26
  ]);
27
27
  const terminalStatuses = statuses.filter((chunk) => {
28
28
  const state = chunk.runStatus?.state;
@@ -111,7 +111,7 @@ export async function deriveNotifications(mesh, local, requestedLimit, deviceId)
111
111
  }
112
112
  const contentByChat = new Map();
113
113
  for (const [chatId, terminalSeq] of terminalSeqByChat) {
114
- contentByChat.set(chatId, await findChunksOfType(snap, chatId, 'content', { toSeq: terminalSeq }));
114
+ contentByChat.set(chatId, await findChunksOfType(snap, chatId, 'content', { lte: terminalSeq }));
115
115
  }
116
116
  const agentNameById = new Map();
117
117
  for (const agentId of new Set(selected.map((candidate) => candidate.agentId))) {
@@ -138,8 +138,8 @@ export async function deriveNotifications(mesh, local, requestedLimit, deviceId)
138
138
  }
139
139
  async function diagramRepairRunIds(snap, chatId, toSeq) {
140
140
  const [users, requests] = await Promise.all([
141
- findChunksOfType(snap, chatId, 'user-message', { toSeq }),
142
- findChunksOfType(snap, chatId, 'run-request', { toSeq })
141
+ findChunksOfType(snap, chatId, 'user-message', { lte: toSeq }),
142
+ findChunksOfType(snap, chatId, 'run-request', { lte: toSeq })
143
143
  ]);
144
144
  const records = [...users, ...requests];
145
145
  const repairs = diagramRepairLinks(records, (chunk) => chunk.userMessage?.text ?? null);
@@ -0,0 +1,18 @@
1
+ export type Bounds = {
2
+ gt?: number;
3
+ gte?: number;
4
+ lt?: number;
5
+ lte?: number;
6
+ reverse?: boolean;
7
+ limit?: number;
8
+ };
9
+ type Range<Key> = {
10
+ gt?: Key;
11
+ gte?: Key;
12
+ lt?: Key;
13
+ lte?: Key;
14
+ reverse?: boolean;
15
+ limit?: number;
16
+ };
17
+ export declare function keyRange<Key extends object>(prefix: Partial<Key>, field: keyof Key & string, { gt, gte, lt, lte, reverse, limit }?: Bounds): Range<Partial<Key>>;
18
+ export {};
@@ -0,0 +1,17 @@
1
+ export function keyRange(prefix, field, { gt, gte, lt, lte, reverse, limit } = {}) {
2
+ const at = (value) => ({ ...prefix, [field]: value });
3
+ const range = { reverse: reverse === true, limit: limit || undefined };
4
+ if (gt !== undefined)
5
+ range.gt = at(gt);
6
+ if (gte !== undefined)
7
+ range.gte = at(gte);
8
+ if (lt !== undefined)
9
+ range.lt = at(lt);
10
+ if (lte !== undefined)
11
+ range.lte = at(lte);
12
+ if (range.gt === undefined && range.gte === undefined)
13
+ range.gte = { ...prefix };
14
+ if (range.lt === undefined && range.lte === undefined)
15
+ range.lte = { ...prefix };
16
+ return range;
17
+ }