@qvac/core 0.9.0 → 0.10.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 (203) hide show
  1. package/CHANGELOG.md +112 -1
  2. package/README.md +4 -4
  3. package/dist-lib/index.d.ts +0 -1
  4. package/dist-lib/index.js +0 -1
  5. package/dist-lib/lib/api/agents.d.ts +7 -1
  6. package/dist-lib/lib/api/agents.js +2 -2
  7. package/dist-lib/lib/api/app-config.d.ts +2 -1
  8. package/dist-lib/lib/api/app-config.js +2 -2
  9. package/dist-lib/lib/api/attachments.js +3 -2
  10. package/dist-lib/lib/api/chat-move.d.ts +2 -1
  11. package/dist-lib/lib/api/chat-move.js +2 -2
  12. package/dist-lib/lib/api/chat.d.ts +3 -1
  13. package/dist-lib/lib/api/chat.js +5 -5
  14. package/dist-lib/lib/api/chunks.d.ts +2 -1
  15. package/dist-lib/lib/api/chunks.js +3 -3
  16. package/dist-lib/lib/api/config.d.ts +2 -1
  17. package/dist-lib/lib/api/config.js +2 -2
  18. package/dist-lib/lib/api/current-mesh.d.ts +1 -3
  19. package/dist-lib/lib/api/current-mesh.js +4 -10
  20. package/dist-lib/lib/api/custom-models.d.ts +7 -1
  21. package/dist-lib/lib/api/custom-models.js +10 -8
  22. package/dist-lib/lib/api/device-routes.d.ts +0 -2
  23. package/dist-lib/lib/api/device-routes.js +4 -5
  24. package/dist-lib/lib/api/devices.d.ts +3 -1
  25. package/dist-lib/lib/api/devices.js +9 -8
  26. package/dist-lib/lib/api/diagnostics.d.ts +2 -1
  27. package/dist-lib/lib/api/diagnostics.js +2 -2
  28. package/dist-lib/lib/api/index.d.ts +2 -0
  29. package/dist-lib/lib/api/index.js +39 -21
  30. package/dist-lib/lib/api/instrument.d.ts +2 -2
  31. package/dist-lib/lib/api/instrument.js +10 -11
  32. package/dist-lib/lib/api/knowledge.d.ts +3 -1
  33. package/dist-lib/lib/api/knowledge.js +9 -7
  34. package/dist-lib/lib/api/logs.js +3 -3
  35. package/dist-lib/lib/api/members.d.ts +6 -1
  36. package/dist-lib/lib/api/members.js +11 -10
  37. package/dist-lib/lib/api/mesh.d.ts +2 -2
  38. package/dist-lib/lib/api/mesh.js +6 -6
  39. package/dist-lib/lib/api/model-download.d.ts +2 -1
  40. package/dist-lib/lib/api/model-download.js +2 -2
  41. package/dist-lib/lib/api/network.d.ts +3 -2
  42. package/dist-lib/lib/api/network.js +4 -4
  43. package/dist-lib/lib/api/notifications.d.ts +3 -1
  44. package/dist-lib/lib/api/notifications.js +5 -4
  45. package/dist-lib/lib/api/rag.d.ts +0 -1
  46. package/dist-lib/lib/api/rag.js +3 -4
  47. package/dist-lib/lib/api/sync.d.ts +2 -1
  48. package/dist-lib/lib/api/sync.js +2 -2
  49. package/dist-lib/lib/api/tool-activity.d.ts +2 -1
  50. package/dist-lib/lib/api/tool-activity.js +4 -3
  51. package/dist-lib/lib/api/tool-configs.d.ts +2 -1
  52. package/dist-lib/lib/api/tool-configs.js +4 -3
  53. package/dist-lib/lib/api/upload-frames.d.ts +0 -1
  54. package/dist-lib/lib/api/upload-frames.js +0 -1
  55. package/dist-lib/lib/api/watchable.d.ts +3 -2
  56. package/dist-lib/lib/api/watchable.js +2 -3
  57. package/dist-lib/lib/assistant/fake-models.d.ts +2 -4
  58. package/dist-lib/lib/assistant/harness.js +8 -6
  59. package/dist-lib/lib/assistant-client.d.ts +2 -0
  60. package/dist-lib/lib/assistant-client.js +4 -3
  61. package/dist-lib/lib/assistant-tree.d.ts +3 -3
  62. package/dist-lib/lib/assistant.d.ts +5 -3
  63. package/dist-lib/lib/assistant.js +50 -13
  64. package/dist-lib/lib/client.d.ts +2 -2
  65. package/dist-lib/lib/core/agent.js +5 -4
  66. package/dist-lib/lib/core/archived-mesh.d.ts +2 -3
  67. package/dist-lib/lib/core/archived-mesh.js +3 -3
  68. package/dist-lib/lib/core/chat-activity.js +5 -3
  69. package/dist-lib/lib/core/chat-copy.d.ts +0 -1
  70. package/dist-lib/lib/core/chat-copy.js +2 -2
  71. package/dist-lib/lib/core/chat-fork.d.ts +0 -6
  72. package/dist-lib/lib/core/chat-fork.js +11 -17
  73. package/dist-lib/lib/core/chat-group.js +8 -7
  74. package/dist-lib/lib/core/chat-move.d.ts +4 -6
  75. package/dist-lib/lib/core/chat-move.js +14 -15
  76. package/dist-lib/lib/core/chat-order.d.ts +2 -1
  77. package/dist-lib/lib/core/chat-order.js +15 -2
  78. package/dist-lib/lib/core/chat.js +8 -7
  79. package/dist-lib/lib/core/chunk.js +13 -8
  80. package/dist-lib/lib/core/collections.d.ts +1 -0
  81. package/dist-lib/lib/core/collections.js +1 -0
  82. package/dist-lib/lib/core/contract/compute.d.ts +0 -4
  83. package/dist-lib/lib/core/contract/compute.js +2 -9
  84. package/dist-lib/lib/core/contract/types.d.ts +2 -2
  85. package/dist-lib/lib/core/departures.d.ts +2 -2
  86. package/dist-lib/lib/core/device-control.d.ts +0 -2
  87. package/dist-lib/lib/core/device-control.js +5 -6
  88. package/dist-lib/lib/core/files/attachments.js +5 -4
  89. package/dist-lib/lib/core/files/index.d.ts +2 -1
  90. package/dist-lib/lib/core/files/index.js +20 -8
  91. package/dist-lib/lib/core/files/processing/claims.js +3 -3
  92. package/dist-lib/lib/core/files/processing/document.d.ts +2 -0
  93. package/dist-lib/lib/core/files/processing/document.js +3 -0
  94. package/dist-lib/lib/core/files/processing/index.js +4 -2
  95. package/dist-lib/lib/core/files/processing/source.d.ts +3 -0
  96. package/dist-lib/lib/core/files/processing/source.js +5 -3
  97. package/dist-lib/lib/core/files/processing/state.js +1 -0
  98. package/dist-lib/lib/core/files/rag/index.d.ts +3 -0
  99. package/dist-lib/lib/core/files/rag/index.js +3 -1
  100. package/dist-lib/lib/core/files/rag/load.js +6 -3
  101. package/dist-lib/lib/core/files/rag/produce.js +3 -3
  102. package/dist-lib/lib/core/files/rag/retrieval/full-text.js +1 -1
  103. package/dist-lib/lib/core/files/rag/similarity.d.ts +2 -2
  104. package/dist-lib/lib/core/files/rag/similarity.js +2 -3
  105. package/dist-lib/lib/core/files/records/chunks.js +1 -1
  106. package/dist-lib/lib/core/files/records/knowledge.js +2 -1
  107. package/dist-lib/lib/core/files/records/purge.js +9 -5
  108. package/dist-lib/lib/core/files/records/reclaim.js +20 -48
  109. package/dist-lib/lib/core/files/records/sweep.d.ts +3 -0
  110. package/dist-lib/lib/core/files/records/sweep.js +9 -3
  111. package/dist-lib/lib/core/files/store/blob.d.ts +2 -0
  112. package/dist-lib/lib/core/files/store/blob.js +2 -2
  113. package/dist-lib/lib/core/files/store/index.d.ts +1 -0
  114. package/dist-lib/lib/core/files/store/index.js +2 -0
  115. package/dist-lib/lib/core/files/store/reference.js +4 -2
  116. package/dist-lib/lib/core/files/upload.d.ts +1 -1
  117. package/dist-lib/lib/core/files/upload.js +10 -9
  118. package/dist-lib/lib/core/handoff-channel.d.ts +6 -4
  119. package/dist-lib/lib/core/handoff-channel.js +8 -9
  120. package/dist-lib/lib/core/harness-loan.d.ts +0 -2
  121. package/dist-lib/lib/core/harness-loan.js +3 -4
  122. package/dist-lib/lib/core/hosted-agents.d.ts +2 -2
  123. package/dist-lib/lib/core/inference-calls-handoff.d.ts +2 -2
  124. package/dist-lib/lib/core/inference-calls-handoff.js +6 -5
  125. package/dist-lib/lib/core/inference-calls-receiver.d.ts +0 -2
  126. package/dist-lib/lib/core/inference-calls-receiver.js +5 -7
  127. package/dist-lib/lib/core/local/index.d.ts +4 -1
  128. package/dist-lib/lib/core/local/index.js +23 -2
  129. package/dist-lib/lib/core/membership.d.ts +2 -2
  130. package/dist-lib/lib/core/mesh.d.ts +13 -7
  131. package/dist-lib/lib/core/mesh.js +55 -47
  132. package/dist-lib/lib/core/migrations/file-index.js +2 -4
  133. package/dist-lib/lib/core/next-frame.js +2 -1
  134. package/dist-lib/lib/core/pdf/rasterize.d.ts +2 -1
  135. package/dist-lib/lib/core/pdf/rasterize.js +5 -5
  136. package/dist-lib/lib/core/peer-duplex.d.ts +3 -3
  137. package/dist-lib/lib/core/peer-duplex.js +4 -4
  138. package/dist-lib/lib/core/secret-handoff.d.ts +2 -2
  139. package/dist-lib/lib/core/secret-handoff.js +7 -4
  140. package/dist-lib/lib/core/send.js +3 -2
  141. package/dist-lib/lib/core/skill-overlay-handoff.d.ts +2 -2
  142. package/dist-lib/lib/core/skill-overlay-handoff.js +6 -5
  143. package/dist-lib/lib/core/skill-overlay-receiver.js +5 -5
  144. package/dist-lib/lib/core/skill-secrets.d.ts +0 -5
  145. package/dist-lib/lib/core/skill-secrets.js +5 -9
  146. package/dist-lib/lib/core/sync/changed.d.ts +20 -0
  147. package/dist-lib/lib/core/sync/changed.js +7 -0
  148. package/dist-lib/lib/core/sync/devices.js +1 -1
  149. package/dist-lib/lib/core/sync/files.d.ts +1 -0
  150. package/dist-lib/lib/core/sync/files.js +50 -72
  151. package/dist-lib/lib/core/sync/index.d.ts +1 -2
  152. package/dist-lib/lib/core/sync/index.js +40 -62
  153. package/dist-lib/lib/core/sync/view-updates.d.ts +33 -0
  154. package/dist-lib/lib/core/sync/view-updates.js +133 -0
  155. package/dist-lib/lib/core/system-info.d.ts +2 -2
  156. package/dist-lib/lib/core/system-info.js +10 -11
  157. package/dist-lib/lib/core/tool-configs.js +6 -6
  158. package/dist-lib/lib/core.d.ts +1 -7
  159. package/dist-lib/lib/core.js +147 -131
  160. package/dist-lib/lib/custom-model-pusher.d.ts +2 -4
  161. package/dist-lib/lib/custom-model-task.d.ts +2 -1
  162. package/dist-lib/lib/custom-model-task.js +2 -2
  163. package/dist-lib/lib/downloads-allowed-pusher.d.ts +2 -4
  164. package/dist-lib/lib/errors.d.ts +603 -0
  165. package/dist-lib/lib/errors.js +552 -0
  166. package/dist-lib/lib/log.d.ts +7 -6
  167. package/dist-lib/lib/log.js +10 -6
  168. package/dist-lib/lib/logger-type.d.ts +2 -5
  169. package/dist-lib/lib/model-download-status.d.ts +2 -2
  170. package/dist-lib/lib/model-download.d.ts +2 -2
  171. package/dist-lib/lib/model-download.js +3 -2
  172. package/dist-lib/lib/model-idle-pusher.d.ts +2 -4
  173. package/dist-lib/lib/model-memory-cap.d.ts +2 -4
  174. package/dist-lib/lib/oauth/browser-connect.js +3 -2
  175. package/dist-lib/lib/oauth/flow-core.js +3 -3
  176. package/dist-lib/lib/oauth/loopback.js +10 -9
  177. package/dist-lib/lib/oauth/providers.js +4 -2
  178. package/dist-lib/lib/oauth/register.js +3 -2
  179. package/dist-lib/lib/oauth/token-exchange-core.js +5 -4
  180. package/dist-lib/lib/run-notices.d.ts +2 -0
  181. package/dist-lib/lib/run-notices.js +7 -2
  182. package/dist-lib/lib/run-transcript.d.ts +2 -2
  183. package/dist-lib/lib/task.js +2 -1
  184. package/dist-lib/schema/engine.js +6 -3
  185. package/dist-lib/schema/field-order.js +1 -3
  186. package/dist-lib/schema/local.js +11 -0
  187. package/dist-lib/spec/hrpc/messages.js +20 -9
  188. package/dist-lib/spec/hyperschema/index.js +20 -9
  189. package/dist-lib/spec/hyperschema/schema.json +15 -0
  190. package/dist-lib/spec/hyperschema/types.d.ts +3 -0
  191. package/dist-lib/spec/local/hyperdb/db.json +16 -1
  192. package/dist-lib/spec/local/hyperdb/index.js +71 -2
  193. package/dist-lib/spec/local/hyperdb/messages.js +300 -143
  194. package/dist-lib/spec/local/hyperschema/index.js +82 -2
  195. package/dist-lib/spec/local/hyperschema/schema.json +43 -1
  196. package/dist-lib/spec/local/hyperschema/types.d.ts +12 -1
  197. package/package.json +10 -3
  198. package/tui/app.mjs +1 -1
  199. package/worker/assistant-sidecar.mjs +7 -0
  200. package/dist-lib/lib/log-levels.d.ts +0 -7
  201. package/dist-lib/lib/log-levels.js +0 -1
  202. package/dist-lib/lib/logger.d.ts +0 -5
  203. package/dist-lib/lib/logger.js +0 -18
@@ -9,7 +9,8 @@ import ReadyResource from 'ready-resource';
9
9
  import { Files } from './files/index.js';
10
10
  import MeshDefinition from '../../spec/mesh/hyperdb/index.js';
11
11
  import { Router, encode } from '../../spec/mesh/hyperdispatch/index.js';
12
- import { syncChanges, syncSnapshot } from './sync/index.js';
12
+ import { syncChanges } from './sync/index.js';
13
+ import { ViewUpdates } from './sync/view-updates.js';
13
14
  import { buildDefaultSeed, isOwnKickedRow, isOwnRevokedRow } from './helpers/index.js';
14
15
  import { raceResource } from '../race-resource.js';
15
16
  import { signDeviceAttestation, signWriterClaim, verifyWriterClaim } from './device-attestation.js';
@@ -21,9 +22,9 @@ import { createSyncActivity } from './sync-status.js';
21
22
  import { buildHealthAssistant } from './health-assistant.js';
22
23
  import b4a from 'b4a';
23
24
  import { errorMessage, isRecord } from '../guards.js';
25
+ import { CoreError } from '../errors.js';
24
26
  import { ACTIVE_AGENTS, ACTIVE_CHATS, DEVICES, LIVE_WRITERS_BY_MOST_RECENT, MEMBERS, MESH_NAMES } from './collections.js';
25
27
  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';
27
28
  const SLOW_WRITE_MS = 5000;
28
29
  const SLOW_DRAIN_MS = 15_000;
29
30
  export function meshPublicKeyFromSeed(seed) {
@@ -77,6 +78,8 @@ export class Mesh extends ReadyResource {
77
78
  _deviceKeyPair;
78
79
  _encryptionKey;
79
80
  _blindPeering = null;
81
+ openedEmpty = false;
82
+ _updates;
80
83
  _openedAt = 0;
81
84
  _trustedDevice;
82
85
  _joined = false;
@@ -113,6 +116,9 @@ export class Mesh extends ReadyResource {
113
116
  get publicKey() {
114
117
  return this._keyPair.publicKey;
115
118
  }
119
+ get log() {
120
+ return this._log;
121
+ }
116
122
  get devicePublicKey() {
117
123
  return this._deviceKeyPair.publicKey;
118
124
  }
@@ -158,6 +164,11 @@ export class Mesh extends ReadyResource {
158
164
  get membership() {
159
165
  return this._membership;
160
166
  }
167
+ tallyMembership() {
168
+ this.refreshMembership()
169
+ .then(() => this.emit('membership-tallied'))
170
+ .catch((err) => this._log?.warn(this._scope, 'tallying membership failed', err));
171
+ }
161
172
  async refreshMembership() {
162
173
  const snap = this.view.snapshot();
163
174
  try {
@@ -192,7 +203,7 @@ export class Mesh extends ReadyResource {
192
203
  }
193
204
  async refreshOurMember() {
194
205
  if (!this._deviceId)
195
- return;
206
+ return null;
196
207
  const own = await this.view.get(DEVICES, { id: this._deviceId });
197
208
  this._ourMemberId = own?.memberId ?? null;
198
209
  if (own?.memberId && own.memberProof) {
@@ -203,6 +214,7 @@ export class Mesh extends ReadyResource {
203
214
  };
204
215
  this.emit('membership', membership);
205
216
  }
217
+ return own;
206
218
  }
207
219
  get name() {
208
220
  return this._name;
@@ -240,6 +252,7 @@ export class Mesh extends ReadyResource {
240
252
  this._wakeup = opts.wakeup;
241
253
  this.attachmentUrl = opts.attachmentUrl ?? null;
242
254
  this.fileIndex = opts.fileIndex ?? null;
255
+ this._updates = new ViewUpdates((update) => this._syncUpdate(update), opts.pendingUpdates);
243
256
  this.router = new Router();
244
257
  this._keyPair = crypto.keyPair(crypto.hash([MESH_KEYPAIR, this._seed]));
245
258
  this._deviceKeyPair = crypto.keyPair(crypto.hash([MESH_DEVICE, this._seed]));
@@ -291,7 +304,6 @@ export class Mesh extends ReadyResource {
291
304
  fastForwards: this.autobee.stats.fastForwards,
292
305
  sinceOpenMs: this._sinceOpen()
293
306
  }));
294
- this.autobee.on('move-to', () => void this._prefetchView().catch(noop));
295
307
  this.autobee.on('interrupt', (err) => this._log?.warn(this._scope, 'autobee interrupt', err));
296
308
  if (this._bootInterrupted) {
297
309
  void this.autobee.close().catch(noop);
@@ -303,6 +315,7 @@ export class Mesh extends ReadyResource {
303
315
  await blobs.close().catch(noop);
304
316
  throw err;
305
317
  }
318
+ this.openedEmpty = !this.view.db.head()?.length;
306
319
  this.autobee.local.on('upload', (index) => {
307
320
  this._localWriterUploadedLength = Math.max(this._localWriterUploadedLength, index + 1);
308
321
  });
@@ -316,6 +329,9 @@ export class Mesh extends ReadyResource {
316
329
  writers: this.writerCount,
317
330
  sinceOpenMs: this._sinceOpen()
318
331
  });
332
+ void this._updates
333
+ .start()
334
+ .catch((err) => this._log?.warn(this._scope, 'resuming the view sync failed', err));
319
335
  void this._sweepOrphanedBlobs(this.blobs.core.length);
320
336
  this.files.start({ deviceId: this._deviceId });
321
337
  }
@@ -384,6 +400,7 @@ export class Mesh extends ReadyResource {
384
400
  }
385
401
  }
386
402
  async _close() {
403
+ this._updates.close();
387
404
  this._log?.info(this._scope, 'closing', {
388
405
  sinceOpenMs: this._sinceOpen(),
389
406
  ...this._lengths(),
@@ -773,38 +790,49 @@ export class Mesh extends ReadyResource {
773
790
  this._log?.warn(this._scope, 'halting apply: the mesh moved past this build', err);
774
791
  return host.interrupt(err);
775
792
  }
776
- async _autobeeUpdate(view, changes) {
777
- if (this.closing)
778
- return;
793
+ async _autobeeUpdate(_view, changes) {
779
794
  const change = changes.get('view');
780
- if (!change) {
795
+ if (this.closing || !change) {
781
796
  return;
782
797
  }
783
798
  const shared = change.shared;
784
799
  const from = shared?.key ? { key: shared.key, length: shared.length } : change.from;
800
+ await this._updates.push({
801
+ from,
802
+ to: change.to?.length ?? 0,
803
+ incremental: !!from?.length && isIncremental(change)
804
+ });
805
+ }
806
+ synced() {
807
+ return this._updates.flush();
808
+ }
809
+ async _syncUpdate({ from, to, incremental }) {
810
+ if (this.closing)
811
+ return null;
785
812
  const started = Date.now();
813
+ let reached = null;
786
814
  try {
787
- if (!from?.length) {
788
- this._arrive();
789
- }
790
- else if (isIncremental(change)) {
791
- await syncChanges(this, view, from);
815
+ if (from && incremental) {
816
+ reached = await syncChanges(this, this.autobee.view, from);
792
817
  }
793
818
  else {
794
- await syncSnapshot(this, view, from);
819
+ this._arrive();
795
820
  }
796
821
  if (this._admitted && !this.autobee.writable) {
797
822
  await this.checkOwnAdmission();
798
823
  }
799
824
  }
800
825
  catch (err) {
801
- this._log?.warn(this._scope, 'change sync failed', err);
826
+ if (!this.closing) {
827
+ this._log?.warn(this._scope, 'change sync failed', err);
828
+ }
802
829
  }
803
830
  this._log?.debug(this._scope, 'view synced', {
804
831
  from: from?.length ?? 0,
805
- to: change.to?.length ?? 0,
832
+ to: reached?.length ?? to,
806
833
  ms: Date.now() - started
807
834
  });
835
+ return reached;
808
836
  }
809
837
  _arrive() {
810
838
  this._log?.info(this._scope, 'landed on the view', {
@@ -824,10 +852,17 @@ export class Mesh extends ReadyResource {
824
852
  }
825
853
  async _readLanding() {
826
854
  const started = Date.now();
827
- await Promise.all([this.refreshOurMember(), this.refreshMembership(), this.refreshName()]);
855
+ const [own] = await Promise.all([
856
+ this.refreshOurMember(),
857
+ this.refreshMembership(),
858
+ this.refreshName()
859
+ ]);
828
860
  if (this.closing) {
829
861
  return;
830
862
  }
863
+ if (own && isOwnKickedRow(this, own)) {
864
+ this.emit('kicked');
865
+ }
831
866
  if (this._membership.unattested > 0) {
832
867
  this.emit('unattested-device');
833
868
  }
@@ -836,16 +871,6 @@ export class Mesh extends ReadyResource {
836
871
  this.emit('mesh-named');
837
872
  this._log?.debug(this._scope, 'read the landed view', { ms: Date.now() - started });
838
873
  }
839
- async _prefetchView() {
840
- const { context } = this.autobee.bee;
841
- const cores = Array.from({ length: context.cores.length + 1 }, (_, i) => context.getCore(i));
842
- const started = Date.now();
843
- const fetched = await Promise.all(cores.map(prefetchCore));
844
- const blocks = fetched.reduce((total, count) => total + count, 0);
845
- if (blocks) {
846
- this._log?.info(this._scope, 'prefetched the view', { blocks, ms: Date.now() - started });
847
- }
848
- }
849
874
  async checkOwnAdmission() {
850
875
  if (this.autobee.writable) {
851
876
  this._admitted = true;
@@ -926,24 +951,6 @@ export class MeshBatch {
926
951
  }
927
952
  }
928
953
  function noop() { }
929
- async function prefetchCore(core) {
930
- await core.ready();
931
- if (!core.length) {
932
- await core.update({ wait: true });
933
- }
934
- const start = core.contiguousLength;
935
- if (start >= core.length) {
936
- return 0;
937
- }
938
- const range = core.download({ start, end: core.length });
939
- try {
940
- await range.done();
941
- }
942
- finally {
943
- range.destroy();
944
- }
945
- return core.length - start;
946
- }
947
954
  function viewCoreOf(head) {
948
955
  return isRecord(head) && b4a.isBuffer(head.key) ? b4a.toHex(head.key).slice(0, 8) : null;
949
956
  }
@@ -951,8 +958,9 @@ function isIncremental(change) {
951
958
  return isRecord(change) && change.incremental === true;
952
959
  }
953
960
  function assertJoined(autobee, opts) {
954
- if (!opts?.optimistic && !autobee.writable)
955
- throw new Error(MESH_JOINING);
961
+ if (!opts?.optimistic && !autobee.writable) {
962
+ throw new CoreError('MESH_STILL_JOINING');
963
+ }
956
964
  }
957
965
  function openedPart(part, name) {
958
966
  if (part === null)
@@ -1,4 +1,4 @@
1
- import { CHUNKS } from '../collections.js';
1
+ import { attachmentChunks } from '../files/records/sweep.js';
2
2
  const BATCH = 256;
3
3
  export async function backfillFileIndex(mesh) {
4
4
  const index = mesh.fileIndex;
@@ -8,11 +8,9 @@ export async function backfillFileIndex(mesh) {
8
8
  const snapshot = mesh.view.snapshot();
9
9
  try {
10
10
  let batch = [];
11
- for await (const chunk of snapshot.find(CHUNKS)) {
11
+ for await (const chunk of attachmentChunks(snapshot)) {
12
12
  if (mesh.closing)
13
13
  return;
14
- if (chunk.type !== 'attachment')
15
- continue;
16
14
  batch.push(chunk);
17
15
  if (batch.length < BATCH) {
18
16
  continue;
@@ -1,4 +1,5 @@
1
1
  import { AbortedError } from '@qvac/harness/race-abort';
2
+ import { CoreError } from '../errors.js';
2
3
  import { unref } from '../unref.js';
3
4
  export const UPLOAD_INACTIVITY_MS = 30_000;
4
5
  export function nextFrame(iterator, { signal, inactivityMs = UPLOAD_INACTIVITY_MS } = {}) {
@@ -6,7 +7,7 @@ export function nextFrame(iterator, { signal, inactivityMs = UPLOAD_INACTIVITY_M
6
7
  return Promise.reject(new AbortedError());
7
8
  }
8
9
  return new Promise((resolve, reject) => {
9
- const timer = setTimeout(() => finish(new Error('upload source timed out')), inactivityMs);
10
+ const timer = setTimeout(() => finish(new CoreError('UPLOAD_TIMEOUT')), inactivityMs);
10
11
  unref(timer);
11
12
  const abort = () => finish(new AbortedError());
12
13
  const finish = (error, result) => {
@@ -1,3 +1,4 @@
1
+ import type { LoggerInterface } from '@qvac/logging';
1
2
  export declare const PDF_RENDER_SCALE = 2;
2
3
  export declare const MAX_PDF_PAGE_IMAGES = 10;
3
4
  interface PageFlags {
@@ -14,5 +15,5 @@ export interface PdfDoc {
14
15
  export interface PdfRasterizer {
15
16
  open(bytes: Uint8Array): Promise<PdfDoc | null>;
16
17
  }
17
- export declare const pdfRasterizer: PdfRasterizer;
18
+ export declare const createPdfRasterizer: (log: LoggerInterface | null) => PdfRasterizer;
18
19
  export {};
@@ -5,7 +5,7 @@ export function renderScale(pageWidth) {
5
5
  return Math.min(PDF_RENDER_SCALE, MAX_IMAGE_WIDTH / pageWidth);
6
6
  }
7
7
  let pdfium;
8
- async function loadPdfium() {
8
+ async function loadPdfium(log) {
9
9
  if (pdfium !== undefined)
10
10
  return pdfium;
11
11
  try {
@@ -13,14 +13,14 @@ async function loadPdfium() {
13
13
  pdfium = mod.default ?? mod;
14
14
  }
15
15
  catch (err) {
16
- logger.warn('pdf', 'bare-pdfium did not load; PDF pages will not render', err);
16
+ log?.warn('pdf', 'bare-pdfium did not load; PDF pages will not render', err);
17
17
  pdfium = null;
18
18
  }
19
19
  return pdfium;
20
20
  }
21
- export const pdfRasterizer = {
21
+ export const createPdfRasterizer = (log) => ({
22
22
  async open(bytes) {
23
- const mod = await loadPdfium();
23
+ const mod = await loadPdfium(log);
24
24
  if (!mod) {
25
25
  return null;
26
26
  }
@@ -54,4 +54,4 @@ export const pdfRasterizer = {
54
54
  }
55
55
  };
56
56
  }
57
- };
57
+ });
@@ -8,8 +8,8 @@ export interface PeerStream {
8
8
  export interface PeerDuplexOptions {
9
9
  readonly protocol: string;
10
10
  readonly onaccept: (fromDeviceId: Uint8Array) => Promise<Duplex | null>;
11
- readonly unreachable: string;
12
- readonly revoked: string;
11
+ readonly unreachable: () => Error;
12
+ readonly revoked: () => Error;
13
13
  readonly onchange?: (deviceId: string) => void;
14
14
  }
15
15
  export declare class PeerDuplexChannel {
@@ -21,7 +21,7 @@ export declare class PeerDuplexChannel {
21
21
  addStream(stream: PeerStream): void;
22
22
  openIds(): string[];
23
23
  isOpen(deviceId: string): boolean;
24
- closeDialled(deviceId: string, reason: string): void;
24
+ closeDialled(deviceId: string, reason: () => Error): void;
25
25
  dial(deviceId: string): Duplex;
26
26
  revalidate(allowed: (deviceId: Uint8Array) => Promise<boolean>): Promise<void>;
27
27
  destroy(): void;
@@ -92,13 +92,13 @@ export class PeerDuplexChannel {
92
92
  }
93
93
  closeDialled(deviceId, reason) {
94
94
  for (const stream of [...(this._peers.get(deviceId)?.dialled.values() ?? [])]) {
95
- stream.destroy(new Error(reason));
95
+ stream.destroy(reason());
96
96
  }
97
97
  }
98
98
  dial(deviceId) {
99
99
  const peer = this._peers.get(deviceId);
100
100
  if (!peer) {
101
- throw new Error(this._opts.unreachable);
101
+ throw this._opts.unreachable();
102
102
  }
103
103
  const id = this._nextId;
104
104
  this._nextId += 1;
@@ -115,7 +115,7 @@ export class PeerDuplexChannel {
115
115
  for (const [id, stream] of [...peer.served]) {
116
116
  peer.served.delete(id);
117
117
  peer.close.send({ id, mine: false });
118
- stream.destroy(new Error(this._opts.revoked));
118
+ stream.destroy(this._opts.revoked());
119
119
  }
120
120
  }
121
121
  }
@@ -205,7 +205,7 @@ export class PeerDuplexChannel {
205
205
  this._peers.delete(deviceId);
206
206
  for (const held of [peer.dialled, peer.served]) {
207
207
  for (const stream of held.values()) {
208
- stream.destroy(new Error(this._opts.unreachable));
208
+ stream.destroy(this._opts.unreachable());
209
209
  }
210
210
  held.clear();
211
211
  }
@@ -1,6 +1,6 @@
1
- import { ACK_TIMEOUT_MS, HOST_SILENT, HOST_UNREACHABLE, type HandoffStream } from './handoff-channel.ts';
1
+ import { ACK_TIMEOUT_MS, type HandoffStream } from './handoff-channel.ts';
2
2
  import type { AssistantPushSkillSecretRequest, AssistantSkillSecretValue } from '../../spec/hyperschema/types.d.ts';
3
- export { ACK_TIMEOUT_MS, HOST_SILENT, HOST_UNREACHABLE, type HandoffStream };
3
+ export { ACK_TIMEOUT_MS, type HandoffStream };
4
4
  export type SecretValue = AssistantSkillSecretValue;
5
5
  export type SecretRequest = AssistantPushSkillSecretRequest;
6
6
  export type SecretRequestHandler = (request: SecretRequest, fromDeviceId: Uint8Array) => Promise<void>;
@@ -1,7 +1,7 @@
1
1
  import { getStruct } from '../../spec/hyperschema/index.js';
2
- import { ACK_TIMEOUT_MS, HandoffChannel, HOST_SILENT, HOST_UNREACHABLE } from './handoff-channel.js';
2
+ import { ACK_TIMEOUT_MS, HandoffChannel } from './handoff-channel.js';
3
3
  const PROTOCOL = 'qvac/secret-handoff';
4
- export { ACK_TIMEOUT_MS, HOST_SILENT, HOST_UNREACHABLE };
4
+ export { ACK_TIMEOUT_MS };
5
5
  const handoff = getStruct('@assistant/skill-secret-handoff');
6
6
  const handoffAck = getStruct('@assistant/skill-secret-handoff-ack');
7
7
  export class SecretHandoff {
@@ -12,8 +12,11 @@ export class SecretHandoff {
12
12
  requestEncoding: handoff,
13
13
  ackEncoding: handoffAck,
14
14
  onrequest: opts.onrequest,
15
- buildAck: (id, error) => ({ id, error }),
16
- readAck: (ack) => ({ error: ack.error ?? '', result: undefined }),
15
+ buildAck: (id, { message, code }) => ({ id, error: message, code }),
16
+ readAck: (ack) => ({
17
+ error: { message: ack.error ?? '', code: ack.code ?? 0 },
18
+ result: undefined
19
+ }),
17
20
  ackTimeoutMs: opts.ackTimeoutMs
18
21
  });
19
22
  }
@@ -1,3 +1,4 @@
1
+ import { CoreError } from '../errors.js';
1
2
  import { buildChat, enrichChat, mustGetActiveChat } from './chat.js';
2
3
  import { appendChunk, listChunks } from './chunk.js';
3
4
  import { randomId } from './id.js';
@@ -5,7 +6,7 @@ export async function sendMessage(mesh, input) {
5
6
  const existing = input.chatId ? await mustGetActiveChat(mesh, input.chatId) : null;
6
7
  const agentId = input.agentId ?? existing?.agentId;
7
8
  if (!agentId) {
8
- throw new Error('send-message: agentId is required to mint a chat');
9
+ throw new CoreError('SEND_AGENT_REQUIRED');
9
10
  }
10
11
  const chat = existing ?? buildChat({ agentId, title: input.title ?? '' });
11
12
  const chatId = chat.id;
@@ -46,7 +47,7 @@ export async function parentRequestId(mesh, chatId, parentRunId) {
46
47
  const requests = await listChunks(mesh, { chatId, type: 'run-request' });
47
48
  const parent = requests.find((chunk) => chunk.runId === parentRunId);
48
49
  if (!parent) {
49
- throw new Error(`run not found: ${parentRunId}`);
50
+ throw new CoreError('RUN_NOT_FOUND', [parentRunId]);
50
51
  }
51
52
  return parent.id;
52
53
  }
@@ -1,6 +1,6 @@
1
- import { ACK_TIMEOUT_MS, HOST_SILENT, HOST_UNREACHABLE, type HandoffStream } from './handoff-channel.ts';
1
+ import { ACK_TIMEOUT_MS, type HandoffStream } from './handoff-channel.ts';
2
2
  import type { AssistantManagedSkill, AssistantSkillOverlayOp } from '../../spec/hyperschema/types.d.ts';
3
- export { ACK_TIMEOUT_MS, HOST_SILENT, HOST_UNREACHABLE, type HandoffStream };
3
+ export { ACK_TIMEOUT_MS, type HandoffStream };
4
4
  export type SkillOverlayOp = AssistantSkillOverlayOp;
5
5
  export interface SkillOverlayResult {
6
6
  readonly skills?: ReadonlyArray<AssistantManagedSkill>;
@@ -1,7 +1,7 @@
1
1
  import { getStruct } from '../../spec/hyperschema/index.js';
2
- import { ACK_TIMEOUT_MS, HandoffChannel, HOST_SILENT, HOST_UNREACHABLE } from './handoff-channel.js';
2
+ import { ACK_TIMEOUT_MS, HandoffChannel } from './handoff-channel.js';
3
3
  const PROTOCOL = 'qvac/skill-overlay-handoff';
4
- export { ACK_TIMEOUT_MS, HOST_SILENT, HOST_UNREACHABLE };
4
+ export { ACK_TIMEOUT_MS };
5
5
  const handoff = getStruct('@assistant/skill-overlay-handoff');
6
6
  const handoffAck = getStruct('@assistant/skill-overlay-handoff-ack');
7
7
  export class SkillOverlayHandoff {
@@ -12,9 +12,10 @@ export class SkillOverlayHandoff {
12
12
  requestEncoding: handoff,
13
13
  ackEncoding: handoffAck,
14
14
  onrequest: opts.onrequest,
15
- buildAck: (id, error, result) => ({
15
+ buildAck: (id, { message, code }, result) => ({
16
16
  id,
17
- error,
17
+ error: message,
18
+ code,
18
19
  skills: result?.skills ? [...result.skills] : undefined,
19
20
  found: result?.found,
20
21
  content: result?.content ?? undefined,
@@ -24,7 +25,7 @@ export class SkillOverlayHandoff {
24
25
  removed: result?.removed
25
26
  }),
26
27
  readAck: (ack) => ({
27
- error: ack.error ?? '',
28
+ error: { message: ack.error ?? '', code: ack.code ?? 0 },
28
29
  result: {
29
30
  skills: ack.skills ?? undefined,
30
31
  found: ack.found,
@@ -1,22 +1,22 @@
1
1
  import b4a from 'b4a';
2
2
  import { getAgent } from './agent.js';
3
- import { NO_SUCH_AGENT, NOT_THE_HOST, SENDER_REVOKED } from './skill-secrets.js';
4
3
  import { DEVICES } from './collections.js';
4
+ import { CoreError } from '../errors.js';
5
5
  export function createSkillOverlayReceiver(opts) {
6
6
  return async (op, fromDeviceId) => {
7
7
  const sender = await opts.mesh.view.get(DEVICES, { id: fromDeviceId });
8
8
  if (!sender || sender.revoked) {
9
- throw new Error(SENDER_REVOKED);
9
+ throw new CoreError('PEER_SENDER_REVOKED');
10
10
  }
11
11
  if (!op.agentId) {
12
- throw new Error(NO_SUCH_AGENT);
12
+ throw new CoreError('AGENT_HOST_AGENT_UNKNOWN');
13
13
  }
14
14
  const agent = await getAgent(opts.mesh, op.agentId);
15
15
  if (!agent) {
16
- throw new Error(NO_SUCH_AGENT);
16
+ throw new CoreError('AGENT_HOST_AGENT_UNKNOWN');
17
17
  }
18
18
  if (!agent.providerDeviceId || !b4a.equals(agent.providerDeviceId, opts.deviceId)) {
19
- throw new Error(NOT_THE_HOST);
19
+ throw new CoreError('AGENT_NOT_HOSTED_HERE');
20
20
  }
21
21
  return opts.runLocal(op);
22
22
  };
@@ -3,11 +3,6 @@ import type { QvacAgent } from '../../spec/mesh/hyperschema/types.d.ts';
3
3
  import type { openLocalStore } from './local/index.ts';
4
4
  import type { Mesh } from './mesh.ts';
5
5
  import type { SecretRequestHandler } from './secret-handoff.ts';
6
- export declare const NOT_THE_HOST = "this device does not host that agent";
7
- export declare const SENDER_REVOKED = "the sending device is no longer in the mesh";
8
- export declare const HOST_REVOKED = "the device hosting this agent is no longer in the mesh";
9
- export declare const NO_SUCH_AGENT = "no agent with that id";
10
- export declare const NOT_A_SETUP_FIELD = "that skill declares no such setup field";
11
6
  export type SetupFields = (skillName: string) => Promise<ReadonlyMap<string, boolean> | null>;
12
7
  export interface SkillSecretsOptions {
13
8
  readonly mesh: Mesh;
@@ -5,27 +5,23 @@ import { settingConfigKey } from '../credential-pusher.js';
5
5
  import { scopedKey } from '../oauth/keys.js';
6
6
  import { getAgent } from './agent.js';
7
7
  import { DEVICES } from './collections.js';
8
- export const NOT_THE_HOST = 'this device does not host that agent';
9
- export const SENDER_REVOKED = 'the sending device is no longer in the mesh';
10
- export const HOST_REVOKED = 'the device hosting this agent is no longer in the mesh';
11
- export const NO_SUCH_AGENT = 'no agent with that id';
12
- export const NOT_A_SETUP_FIELD = 'that skill declares no such setup field';
8
+ import { CoreError } from '../errors.js';
13
9
  export function createSkillSecretReceiver(opts) {
14
10
  return async (request, fromDeviceId) => {
15
11
  const sender = await opts.mesh.view.get(DEVICES, { id: fromDeviceId });
16
12
  if (!sender || sender.revoked) {
17
- throw new Error(SENDER_REVOKED);
13
+ throw new CoreError('PEER_SENDER_REVOKED');
18
14
  }
19
15
  const agent = await getAgent(opts.mesh, request.agentId);
20
16
  if (!agent) {
21
- throw new Error(NO_SUCH_AGENT);
17
+ throw new CoreError('AGENT_HOST_AGENT_UNKNOWN');
22
18
  }
23
19
  if (!agent.providerDeviceId || !b4a.equals(agent.providerDeviceId, opts.deviceId)) {
24
- throw new Error(NOT_THE_HOST);
20
+ throw new CoreError('AGENT_NOT_HOSTED_HERE');
25
21
  }
26
22
  const fields = await opts.setupFields(request.skillName);
27
23
  if (fields && request.values.some(({ key }) => !fields.has(key))) {
28
- throw new Error(NOT_A_SETUP_FIELD);
24
+ throw new CoreError('SKILL_SETUP_FIELD_UNKNOWN');
29
25
  }
30
26
  const entries = request.values.map(({ key, value }) => ({
31
27
  key: fields?.get(key) === false
@@ -0,0 +1,20 @@
1
+ import type { ViewChange } from 'hyperdb';
2
+ import type { QvacAgent, QvacChat, QvacChunk, QvacChunkCounter, QvacDevice, QvacIndexingStatus, QvacKnowledge, QvacKnowledgeArtifact, QvacPurgedChat } from '../../../spec/mesh/hyperschema/types.d.ts';
3
+ import { AGENTS, CHATS, CHUNKS, CHUNK_COUNTERS, DEVICES, INDEXING_STATUS, KNOWLEDGE, KNOWLEDGE_ARTIFACTS, PURGED_CHATS } from '../collections.ts';
4
+ export interface ChangedRows {
5
+ [AGENTS]: QvacAgent;
6
+ [DEVICES]: QvacDevice;
7
+ [CHATS]: QvacChat;
8
+ [CHUNK_COUNTERS]: QvacChunkCounter;
9
+ [CHUNKS]: QvacChunk;
10
+ [INDEXING_STATUS]: QvacIndexingStatus;
11
+ [KNOWLEDGE]: QvacKnowledge;
12
+ [KNOWLEDGE_ARTIFACTS]: QvacKnowledgeArtifact;
13
+ [PURGED_CHATS]: QvacPurgedChat;
14
+ }
15
+ export type ChangedRow<C extends keyof ChangedRows> = ViewChange & {
16
+ readonly collection: C;
17
+ readonly value: ChangedRows[C];
18
+ };
19
+ export declare function changed<C extends keyof ChangedRows>(change: ViewChange, collection: C): change is ChangedRow<C>;
20
+ export declare function liveRow<C extends keyof ChangedRows>(change: ChangedRow<C>): ChangedRows[C] | null;
@@ -0,0 +1,7 @@
1
+ import { AGENTS, CHATS, CHUNKS, CHUNK_COUNTERS, DEVICES, INDEXING_STATUS, KNOWLEDGE, KNOWLEDGE_ARTIFACTS, PURGED_CHATS } from '../collections.js';
2
+ export function changed(change, collection) {
3
+ return change.collection === collection;
4
+ }
5
+ export function liveRow(change) {
6
+ return change.type === 'delete' ? null : change.value;
7
+ }
@@ -9,6 +9,6 @@ export function syncOwnMember(mesh, device) {
9
9
  export function syncDeviceRevocation(mesh, device) {
10
10
  if (!isOwnKickedRow(mesh, device))
11
11
  return;
12
- logger.info('sync', 'our writer was revoked (kicked)');
12
+ mesh.log?.info('sync', 'our writer was revoked (kicked)');
13
13
  mesh.emit('kicked');
14
14
  }
@@ -10,5 +10,6 @@ export declare class FileChanges {
10
10
  private readonly _devices;
11
11
  constructor(snap: HyperDB, from: ViewHead);
12
12
  add(change: ViewChange): Promise<void>;
13
+ private _was;
13
14
  ids(): Promise<Set<string>>;
14
15
  }