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

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 (213) hide show
  1. package/CHANGELOG.md +238 -12
  2. package/dist-lib/index.d.ts +3 -1
  3. package/dist-lib/index.js +1 -0
  4. package/dist-lib/lib/api/approval-rules.d.ts +1 -1
  5. package/dist-lib/lib/api/approval-rules.js +5 -1
  6. package/dist-lib/lib/api/chunk-delta.d.ts +7 -2
  7. package/dist-lib/lib/api/chunk-delta.js +89 -7
  8. package/dist-lib/lib/api/chunks.js +14 -8
  9. package/dist-lib/lib/api/config.d.ts +10 -3
  10. package/dist-lib/lib/api/config.js +51 -6
  11. package/dist-lib/lib/api/custom-models.js +3 -5
  12. package/dist-lib/lib/api/devices.js +3 -1
  13. package/dist-lib/lib/api/files.d.ts +1 -1
  14. package/dist-lib/lib/api/files.js +65 -8
  15. package/dist-lib/lib/api/index.d.ts +12 -2
  16. package/dist-lib/lib/api/index.js +6 -1
  17. package/dist-lib/lib/api/instrument.js +12 -4
  18. package/dist-lib/lib/api/members.js +7 -13
  19. package/dist-lib/lib/api/mesh.d.ts +1 -3
  20. package/dist-lib/lib/api/mesh.js +2 -2
  21. package/dist-lib/lib/api/notifications.js +8 -4
  22. package/dist-lib/lib/api/projects.js +5 -1
  23. package/dist-lib/lib/api/rag.js +32 -19
  24. package/dist-lib/lib/api/served.js +2 -1
  25. package/dist-lib/lib/api/workspace.js +0 -1
  26. package/dist-lib/lib/assistant/harness.d.ts +8 -2
  27. package/dist-lib/lib/assistant/harness.js +17 -4
  28. package/dist-lib/lib/assistant/history.d.ts +1 -1
  29. package/dist-lib/lib/assistant/history.js +19 -13
  30. package/dist-lib/lib/assistant/message.d.ts +6 -1
  31. package/dist-lib/lib/assistant/message.js +22 -2
  32. package/dist-lib/lib/assistant/model-round.d.ts +5 -0
  33. package/dist-lib/lib/assistant/model-round.js +47 -19
  34. package/dist-lib/lib/assistant/pdf-budget.d.ts +10 -2
  35. package/dist-lib/lib/assistant/pdf-budget.js +10 -11
  36. package/dist-lib/lib/assistant/pdf-pages.d.ts +25 -0
  37. package/dist-lib/lib/assistant/pdf-pages.js +75 -0
  38. package/dist-lib/lib/assistant/round-host.js +5 -3
  39. package/dist-lib/lib/assistant-client.d.ts +2 -1
  40. package/dist-lib/lib/assistant-client.js +24 -6
  41. package/dist-lib/lib/assistant-tree.d.ts +2 -0
  42. package/dist-lib/lib/assistant-tree.js +8 -5
  43. package/dist-lib/lib/assistant.d.ts +21 -7
  44. package/dist-lib/lib/assistant.js +481 -244
  45. package/dist-lib/lib/client.js +10 -1
  46. package/dist-lib/lib/compaction.d.ts +3 -1
  47. package/dist-lib/lib/compaction.js +18 -4
  48. package/dist-lib/lib/constants.d.ts +3 -0
  49. package/dist-lib/lib/constants.js +3 -0
  50. package/dist-lib/lib/core/agent-copies.js +7 -1
  51. package/dist-lib/lib/core/agent.js +1 -0
  52. package/dist-lib/lib/core/approval-rules.d.ts +5 -1
  53. package/dist-lib/lib/core/approval-rules.js +16 -1
  54. package/dist-lib/lib/core/chat-activity.js +13 -16
  55. package/dist-lib/lib/core/chat-copy.js +1 -1
  56. package/dist-lib/lib/core/chat-fork.js +2 -1
  57. package/dist-lib/lib/core/chat-move.d.ts +4 -0
  58. package/dist-lib/lib/core/chat-move.js +42 -11
  59. package/dist-lib/lib/core/chat.d.ts +0 -1
  60. package/dist-lib/lib/core/chat.js +1 -1
  61. package/dist-lib/lib/core/chunk.d.ts +14 -3
  62. package/dist-lib/lib/core/chunk.js +116 -25
  63. package/dist-lib/lib/core/collections.d.ts +12 -1
  64. package/dist-lib/lib/core/collections.js +12 -1
  65. package/dist-lib/lib/core/config-value.d.ts +7 -0
  66. package/dist-lib/lib/core/config-value.js +15 -0
  67. package/dist-lib/lib/core/contract/activity.d.ts +3 -2
  68. package/dist-lib/lib/core/contract/activity.js +39 -21
  69. package/dist-lib/lib/core/contract/agent-join.js +15 -19
  70. package/dist-lib/lib/core/contract/agents.js +8 -7
  71. package/dist-lib/lib/core/contract/chat.d.ts +3 -0
  72. package/dist-lib/lib/core/contract/chat.js +12 -1
  73. package/dist-lib/lib/core/contract/chunks.js +16 -6
  74. package/dist-lib/lib/core/contract/devices.js +6 -4
  75. package/dist-lib/lib/core/contract/index.js +2 -1
  76. package/dist-lib/lib/core/contract/invites.js +12 -31
  77. package/dist-lib/lib/core/contract/joined-meshes.js +2 -1
  78. package/dist-lib/lib/core/contract/mesh-rules.js +4 -0
  79. package/dist-lib/lib/core/contract/model-requests.js +7 -6
  80. package/dist-lib/lib/core/contract/project-device-keys.js +15 -6
  81. package/dist-lib/lib/core/contract/project-host-keys.js +5 -4
  82. package/dist-lib/lib/core/contract/projects.js +12 -13
  83. package/dist-lib/lib/core/contract/public-seeds.js +5 -5
  84. package/dist-lib/lib/core/contract/roles.js +2 -1
  85. package/dist-lib/lib/core/contract/run-requests.d.ts +1 -1
  86. package/dist-lib/lib/core/contract/run-requests.js +19 -13
  87. package/dist-lib/lib/core/contract/shipped-rows.d.ts +2 -0
  88. package/dist-lib/lib/core/contract/shipped-rows.js +22 -0
  89. package/dist-lib/lib/core/contract/workspace.js +6 -12
  90. package/dist-lib/lib/core/contract/writers.js +20 -6
  91. package/dist-lib/lib/core/deleted-agent-runs.d.ts +7 -0
  92. package/dist-lib/lib/core/deleted-agent-runs.js +46 -0
  93. package/dist-lib/lib/core/devices.d.ts +2 -1
  94. package/dist-lib/lib/core/devices.js +4 -2
  95. package/dist-lib/lib/core/document-attachment.d.ts +6 -3
  96. package/dist-lib/lib/core/document-attachment.js +10 -5
  97. package/dist-lib/lib/core/files/attachments.js +3 -3
  98. package/dist-lib/lib/core/files/index.d.ts +8 -5
  99. package/dist-lib/lib/core/files/index.js +111 -18
  100. package/dist-lib/lib/core/files/processing/index.d.ts +1 -1
  101. package/dist-lib/lib/core/files/processing/index.js +8 -2
  102. package/dist-lib/lib/core/files/processing/source.d.ts +1 -0
  103. package/dist-lib/lib/core/files/processing/source.js +3 -0
  104. package/dist-lib/lib/core/files/processing/state.d.ts +1 -0
  105. package/dist-lib/lib/core/files/processing/state.js +7 -0
  106. package/dist-lib/lib/core/files/rag/build.d.ts +1 -0
  107. package/dist-lib/lib/core/files/rag/build.js +14 -1
  108. package/dist-lib/lib/core/files/rag/index.d.ts +1 -1
  109. package/dist-lib/lib/core/files/rag/index.js +1 -1
  110. package/dist-lib/lib/core/files/rag/produce.js +1 -0
  111. package/dist-lib/lib/core/files/rag/retrieval/full-text.d.ts +2 -0
  112. package/dist-lib/lib/core/files/rag/retrieval/full-text.js +7 -1
  113. package/dist-lib/lib/core/first-event.d.ts +6 -0
  114. package/dist-lib/lib/core/first-event.js +13 -0
  115. package/dist-lib/lib/core/invite.d.ts +2 -2
  116. package/dist-lib/lib/core/invite.js +5 -4
  117. package/dist-lib/lib/core/local/index.d.ts +16 -1
  118. package/dist-lib/lib/core/local/index.js +119 -10
  119. package/dist-lib/lib/core/mesh.d.ts +14 -3
  120. package/dist-lib/lib/core/mesh.js +127 -54
  121. package/dist-lib/lib/core/migrations/chat-chunks.d.ts +4 -0
  122. package/dist-lib/lib/core/migrations/chat-chunks.js +47 -0
  123. package/dist-lib/lib/core/migrations/file-index.d.ts +1 -1
  124. package/dist-lib/lib/core/migrations/file-index.js +6 -2
  125. package/dist-lib/lib/core/migrations/index.d.ts +2 -1
  126. package/dist-lib/lib/core/migrations/index.js +66 -9
  127. package/dist-lib/lib/core/notification.js +96 -23
  128. package/dist-lib/lib/core/pdf/figure.d.ts +22 -0
  129. package/dist-lib/lib/core/pdf/figure.js +64 -0
  130. package/dist-lib/lib/core/pdf/rasterize.d.ts +10 -1
  131. package/dist-lib/lib/core/pdf/rasterize.js +72 -22
  132. package/dist-lib/lib/core/peer-duplex.js +57 -5
  133. package/dist-lib/lib/core/public-mesh.js +8 -2
  134. package/dist-lib/lib/core/run-requests.js +2 -3
  135. package/dist-lib/lib/core/senders.d.ts +7 -6
  136. package/dist-lib/lib/core/senders.js +23 -30
  137. package/dist-lib/lib/core/shared-agents.d.ts +1 -0
  138. package/dist-lib/lib/core/shared-agents.js +47 -19
  139. package/dist-lib/lib/core/skill-secrets.d.ts +3 -2
  140. package/dist-lib/lib/core/skill-secrets.js +4 -1
  141. package/dist-lib/lib/core/sync/background.d.ts +3 -15
  142. package/dist-lib/lib/core/sync/background.js +25 -102
  143. package/dist-lib/lib/core/sync/devices.js +6 -3
  144. package/dist-lib/lib/core/sync/index.js +25 -3
  145. package/dist-lib/lib/core/sync-status.js +3 -3
  146. package/dist-lib/lib/core/vault-managed-skills.d.ts +18 -0
  147. package/dist-lib/lib/core/vault-managed-skills.js +105 -0
  148. package/dist-lib/lib/core.d.ts +66 -10
  149. package/dist-lib/lib/core.js +768 -179
  150. package/dist-lib/lib/credential-pusher.d.ts +11 -7
  151. package/dist-lib/lib/credential-pusher.js +40 -8
  152. package/dist-lib/lib/device-tree.d.ts +18 -0
  153. package/dist-lib/lib/device-tree.js +32 -0
  154. package/dist-lib/lib/doctor/redact.js +1 -1
  155. package/dist-lib/lib/errors.d.ts +12 -0
  156. package/dist-lib/lib/errors.js +12 -0
  157. package/dist-lib/lib/fs-drive-bridge.d.ts +7 -0
  158. package/dist-lib/lib/fs-drive-bridge.js +308 -30
  159. package/dist-lib/lib/integrations/setup-routes.js +7 -5
  160. package/dist-lib/lib/knowledge-search.d.ts +1 -1
  161. package/dist-lib/lib/knowledge-search.js +5 -46
  162. package/dist-lib/lib/kv-cache-retention.d.ts +35 -0
  163. package/dist-lib/lib/kv-cache-retention.js +98 -0
  164. package/dist-lib/lib/latest-frame.d.ts +10 -0
  165. package/dist-lib/lib/latest-frame.js +48 -0
  166. package/dist-lib/lib/mesh-scoped-engine.js +1 -0
  167. package/dist-lib/lib/model-download.d.ts +6 -1
  168. package/dist-lib/lib/model-download.js +32 -17
  169. package/dist-lib/lib/oauth/resolve.js +13 -5
  170. package/dist-lib/lib/reconnecting-watch.js +19 -11
  171. package/dist-lib/lib/run-notices.d.ts +2 -0
  172. package/dist-lib/lib/run-notices.js +5 -0
  173. package/dist-lib/lib/run-replay.d.ts +13 -1
  174. package/dist-lib/lib/run-replay.js +58 -21
  175. package/dist-lib/schema/build.js +66 -57
  176. package/dist-lib/schema/config.d.ts +1 -1
  177. package/dist-lib/schema/config.js +1 -1
  178. package/dist-lib/schema/content.js +2 -1
  179. package/dist-lib/schema/engine.js +70 -6
  180. package/dist-lib/schema/field-order.js +7 -1
  181. package/dist-lib/schema/indexes.d.ts +4 -0
  182. package/dist-lib/schema/indexes.js +18 -0
  183. package/dist-lib/schema/local.js +9 -2
  184. package/dist-lib/schema/qvac.js +116 -6
  185. package/dist-lib/spec/bind.js +15 -0
  186. package/dist-lib/spec/calls.js +5 -0
  187. package/dist-lib/spec/capabilities.d.ts +10 -0
  188. package/dist-lib/spec/config/hyperconf/index.js +1 -1
  189. package/dist-lib/spec/hrpc/hrpc.json +65 -0
  190. package/dist-lib/spec/hrpc/index.js +63 -3
  191. package/dist-lib/spec/hrpc/messages.js +1537 -1395
  192. package/dist-lib/spec/hyperschema/index.js +1537 -1395
  193. package/dist-lib/spec/hyperschema/schema.json +126 -1
  194. package/dist-lib/spec/hyperschema/types.d.ts +33 -1
  195. package/dist-lib/spec/local/hyperdb/messages.js +110 -54
  196. package/dist-lib/spec/local/hyperschema/index.js +56 -28
  197. package/dist-lib/spec/local/hyperschema/schema.json +55 -20
  198. package/dist-lib/spec/local/hyperschema/types.d.ts +8 -1
  199. package/dist-lib/spec/mesh/hyperdb/db.json +255 -51
  200. package/dist-lib/spec/mesh/hyperdb/index.js +1063 -404
  201. package/dist-lib/spec/mesh/hyperdb/messages.js +1673 -1202
  202. package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +62 -56
  203. package/dist-lib/spec/mesh/hyperdispatch/index.js +156 -139
  204. package/dist-lib/spec/mesh/hyperdispatch/messages.js +809 -632
  205. package/dist-lib/spec/mesh/hyperschema/index.js +809 -632
  206. package/dist-lib/spec/mesh/hyperschema/schema.json +293 -174
  207. package/dist-lib/spec/mesh/hyperschema/types.d.ts +31 -1
  208. package/package.json +32 -26
  209. package/worker/assistant-sidecar.mjs +3 -0
  210. package/dist-lib/lib/core/files/processing/settle.d.ts +0 -13
  211. package/dist-lib/lib/core/files/processing/settle.js +0 -86
  212. package/dist-lib/lib/core/migrations/device-indexes.d.ts +0 -2
  213. package/dist-lib/lib/core/migrations/device-indexes.js +0 -7
@@ -10,6 +10,7 @@ import b4a from 'b4a';
10
10
  import { dropMeshFileCursors, LocalFiles } from './files.js';
11
11
  import { LocalRunLogs } from './run-logs.js';
12
12
  import { LOCAL_ACCOUNT_RECOVERY, LOCAL_ACTIVE_MESH, LOCAL_APP_CONFIG, LOCAL_CHAT_MOVES, LOCAL_MESH_UPDATES, LOCAL_MESH_PURGES, LOCAL_MESH_NAMES, LOCAL_CHAT_NOTIFY, LOCAL_CHAT_ORDER, LOCAL_CHAT_ORDER_BY_KEY, LOCAL_CONFIG, LOCAL_CUSTOM_MODEL, LOCAL_FILES, LOCAL_DEVICE, LOCAL_IDENTITY, LOCAL_MEMBERSHIP, LOCAL_MEMBER_PROOF, LOCAL_MESH_DEPARTURE, LOCAL_MESH_HISTORY, LOCAL_MESH_MEMBERSHIP, LOCAL_MESH_SESSION, LOCAL_MODEL_DOWNLOAD, LOCAL_MODEL_NOTIFY, LOCAL_RUN_LOGS, LOCAL_RUN_LOG_STEPS } from '../collections.js';
13
+ import { configValue, readConfigValue } from '../config-value.js';
13
14
  export const MESH_LOCAL_COLLECTIONS = [
14
15
  { collection: LOCAL_MESH_MEMBERSHIP, field: 'key' },
15
16
  { collection: LOCAL_MESH_DEPARTURE, field: 'key' },
@@ -36,7 +37,8 @@ function sessionOf(row) {
36
37
  baseKey: row.key ?? undefined,
37
38
  writerSeed: row.writerSeed ?? undefined,
38
39
  role: meshRole(row.role),
39
- workspaceKey: row.workspaceKey ?? undefined
40
+ workspaceKey: row.workspaceKey ?? undefined,
41
+ bootFrom: row.bootKey ? { key: row.bootKey, length: row.bootLength ?? 0 } : null
40
42
  };
41
43
  }
42
44
  function meshRole(role) {
@@ -70,12 +72,12 @@ class LocalStore {
70
72
  }
71
73
  async get(key) {
72
74
  const row = await this._db.get(LOCAL_CONFIG, { key });
73
- return row === null ? null : row.value;
75
+ return row === null ? null : readConfigValue(row);
74
76
  }
75
77
  set(key, value) {
76
78
  return this._locked(async () => {
77
79
  const tx = this._db.transaction();
78
- await tx.insert(LOCAL_CONFIG, { key, value });
80
+ await tx.insert(LOCAL_CONFIG, { key, ...configValue(value) });
79
81
  await tx.flush();
80
82
  });
81
83
  }
@@ -83,7 +85,7 @@ class LocalStore {
83
85
  return this._locked(async () => {
84
86
  const tx = this._db.transaction();
85
87
  for (const { key, value } of entries) {
86
- await tx.insert(LOCAL_CONFIG, { key, value });
88
+ await tx.insert(LOCAL_CONFIG, { key, ...configValue(value) });
87
89
  }
88
90
  await tx.flush();
89
91
  });
@@ -95,10 +97,10 @@ class LocalStore {
95
97
  return null;
96
98
  }
97
99
  const tx = this._db.transaction();
98
- await tx.insert(LOCAL_CONFIG, { key: toKey, value: row.value });
100
+ await tx.insert(LOCAL_CONFIG, { ...row, key: toKey });
99
101
  await tx.delete(LOCAL_CONFIG, { key: fromKey });
100
102
  await tx.flush();
101
- return row.value;
103
+ return readConfigValue(row);
102
104
  });
103
105
  }
104
106
  deleteAll(keys) {
@@ -110,6 +112,20 @@ class LocalStore {
110
112
  await tx.flush();
111
113
  });
112
114
  }
115
+ change(plan) {
116
+ return this._locked(async () => {
117
+ const { result, set, delete: drop } = await plan((key) => this.get(key));
118
+ const tx = this._db.transaction();
119
+ for (const { key, value } of set) {
120
+ await tx.insert(LOCAL_CONFIG, { key, ...configValue(value) });
121
+ }
122
+ for (const key of drop) {
123
+ await tx.delete(LOCAL_CONFIG, { key });
124
+ }
125
+ await tx.flush();
126
+ return result;
127
+ });
128
+ }
113
129
  deletePrefix(prefix) {
114
130
  return this._locked(async () => {
115
131
  const tx = this._db.transaction();
@@ -285,11 +301,20 @@ class LocalStore {
285
301
  }
286
302
  return rows;
287
303
  }
288
- putCustomModelRow(row) {
304
+ putCustomModelRowIfLabelAvailable(row) {
289
305
  return this._locked(async () => {
306
+ const rows = [];
307
+ for await (const current of this._db.find(LOCAL_CUSTOM_MODEL)) {
308
+ rows.push(current);
309
+ }
310
+ const taken = rows.some((current) => (current.label || current.name) === row.label && current.name !== row.name);
311
+ if (taken) {
312
+ return false;
313
+ }
290
314
  const tx = this._db.transaction();
291
315
  await tx.insert(LOCAL_CUSTOM_MODEL, row);
292
316
  await tx.flush();
317
+ return true;
293
318
  });
294
319
  }
295
320
  deleteCustomModelRow(name) {
@@ -427,6 +452,13 @@ class LocalStore {
427
452
  await tx.flush();
428
453
  });
429
454
  }
455
+ dropChatMove({ meshKey, chatId }) {
456
+ return this._locked(async () => {
457
+ const tx = this._db.transaction();
458
+ await tx.delete(LOCAL_CHAT_MOVES, { meshKey, chatId });
459
+ await tx.flush();
460
+ });
461
+ }
430
462
  recordAccountRecovery(row) {
431
463
  return this._locked(async () => {
432
464
  const tx = this._db.transaction();
@@ -563,8 +595,11 @@ class LocalStore {
563
595
  seed: session.seed,
564
596
  writerSeed: session.writerSeed,
565
597
  awaitingAdmission: existing?.awaitingAdmission,
598
+ bootKey: session.bootFrom?.key,
599
+ bootLength: session.bootFrom?.length,
566
600
  role: session.role ?? existing?.role,
567
- workspaceKey: session.workspaceKey ?? existing?.workspaceKey
601
+ workspaceKey: session.workspaceKey ?? existing?.workspaceKey,
602
+ joinedAt: existing?.joinedAt
568
603
  });
569
604
  if (minted) {
570
605
  await tx.insert(LOCAL_ACTIVE_MESH, { deviceId, homeMeshKey: key });
@@ -640,6 +675,64 @@ class LocalStore {
640
675
  await tx.flush();
641
676
  });
642
677
  }
678
+ markRetired(keys, accountKey) {
679
+ return this._locked(async () => {
680
+ const tx = this._db.transaction();
681
+ for (const key of keys) {
682
+ const existing = await tx.get(LOCAL_MESH_MEMBERSHIP, { key });
683
+ if (existing) {
684
+ await tx.insert(LOCAL_MESH_MEMBERSHIP, {
685
+ ...existing,
686
+ retiredUnder: accountKey
687
+ });
688
+ }
689
+ }
690
+ await tx.flush();
691
+ });
692
+ }
693
+ unretire(accountKey) {
694
+ return this._locked(async () => {
695
+ const keys = [];
696
+ const tx = this._db.transaction();
697
+ for await (const row of this._db.find(LOCAL_MESH_MEMBERSHIP)) {
698
+ if (row.retiredUnder && b4a.equals(row.retiredUnder, accountKey)) {
699
+ await tx.insert(LOCAL_MESH_MEMBERSHIP, {
700
+ ...row,
701
+ retiredUnder: undefined
702
+ });
703
+ keys.push(row.key);
704
+ }
705
+ }
706
+ await tx.flush();
707
+ return keys;
708
+ });
709
+ }
710
+ markKicked(key) {
711
+ return this._locked(async () => {
712
+ const existing = await this._db.get(LOCAL_MESH_MEMBERSHIP, { key });
713
+ if (!existing) {
714
+ return;
715
+ }
716
+ const tx = this._db.transaction();
717
+ await tx.insert(LOCAL_MESH_MEMBERSHIP, { ...existing, kicked: true });
718
+ await tx.flush();
719
+ });
720
+ }
721
+ restoreMesh(key) {
722
+ return this._locked(async () => {
723
+ const existing = await this._db.get(LOCAL_MESH_MEMBERSHIP, { key });
724
+ if (!existing) {
725
+ return;
726
+ }
727
+ const tx = this._db.transaction();
728
+ await tx.insert(LOCAL_MESH_MEMBERSHIP, {
729
+ ...existing,
730
+ leftAt: undefined,
731
+ wasHome: undefined
732
+ });
733
+ await tx.flush();
734
+ });
735
+ }
643
736
  readPurge(key) {
644
737
  return this._db.get(LOCAL_MESH_PURGES, { key });
645
738
  }
@@ -716,17 +809,33 @@ class LocalStore {
716
809
  const { awaitingAdmission } = opts;
717
810
  return this._locked(async () => {
718
811
  const existing = await this._db.get(LOCAL_MESH_MEMBERSHIP, { key });
719
- if (!existing || !!existing.awaitingAdmission === awaitingAdmission) {
812
+ const settled = awaitingAdmission || !existing?.bootKey;
813
+ if (!existing || (!!existing.awaitingAdmission === awaitingAdmission && settled)) {
720
814
  return;
721
815
  }
816
+ const { bootKey, bootLength, ...rest } = existing;
722
817
  const tx = this._db.transaction();
723
818
  await tx.insert(LOCAL_MESH_MEMBERSHIP, {
724
- ...existing,
819
+ ...(awaitingAdmission ? existing : rest),
725
820
  awaitingAdmission
726
821
  });
727
822
  await tx.flush();
728
823
  });
729
824
  }
825
+ markJoinedUnder(key, joinedAt) {
826
+ return this._locked(async () => {
827
+ const existing = await this._db.get(LOCAL_MESH_MEMBERSHIP, { key });
828
+ if (!existing || existing.joinedAt === joinedAt) {
829
+ return;
830
+ }
831
+ const tx = this._db.transaction();
832
+ await tx.insert(LOCAL_MESH_MEMBERSHIP, { ...existing, joinedAt });
833
+ await tx.flush();
834
+ });
835
+ }
836
+ async joinedUnder(key) {
837
+ return (await this._membership(key))?.joinedAt || null;
838
+ }
730
839
  setMeshRole(key, role) {
731
840
  return this._locked(async () => {
732
841
  const existing = await this._db.get(LOCAL_MESH_MEMBERSHIP, { key });
@@ -49,11 +49,13 @@ export interface MeshOptions {
49
49
  pendingUpdates?: PendingUpdates | null;
50
50
  slowWriteMs?: number;
51
51
  slowDrainMs?: number;
52
- syncBatch?: number;
53
- syncPauseMs?: number;
54
52
  }
53
+ export declare const SCHEMA_VERSION: number;
55
54
  export declare function meshPublicKeyFromSeed(seed: Buffer): Buffer;
56
- export declare function isOurTrustedWriter(snap: HyperDB, writerKey: Buffer, ourMemberId: Buffer | null): Promise<boolean>;
55
+ export interface TrustScope {
56
+ readonly ourMemberId: Buffer | null;
57
+ }
58
+ export declare function isTrustedWriter(snap: HyperDB, writerKey: Buffer, { ourMemberId }: TrustScope): Promise<boolean>;
57
59
  export type BirthRow = {
58
60
  readonly id: Buffer;
59
61
  readonly joinedAt: number;
@@ -141,6 +143,8 @@ export declare class Mesh extends ReadyResource {
141
143
  private readonly _slowDrainMs;
142
144
  private _stallReported;
143
145
  private _admitted;
146
+ private _refused;
147
+ private _kickSeen;
144
148
  private _birth;
145
149
  private _rulesRaised;
146
150
  private _autobee;
@@ -200,6 +204,8 @@ export declare class Mesh extends ReadyResource {
200
204
  private _watchStall;
201
205
  private _endStall;
202
206
  private _reportStall;
207
+ private _fastForwardHeld;
208
+ private _writersHeld;
203
209
  private _coresNotHeld;
204
210
  private _coreHeld;
205
211
  batch(): MeshBatch;
@@ -238,8 +244,13 @@ export declare class Mesh extends ReadyResource {
238
244
  private _arrive;
239
245
  private _readLanding;
240
246
  checkOwnAdmission(): Promise<void>;
247
+ kickDirty(): Promise<void>;
248
+ private _kickState;
249
+ readKicked(): Promise<boolean>;
250
+ checkResumedRefusal(): Promise<boolean>;
241
251
  checkResumedRevocation(): Promise<'kicked' | 'left' | null>;
242
252
  ownWriterRefused(): Promise<boolean>;
253
+ private _ownRow;
243
254
  }
244
255
  export interface MeshDispatcher {
245
256
  dispatch(name: string, payload: unknown): void | Promise<void>;
@@ -9,14 +9,14 @@ import HypercoreId from 'hypercore-id-encoding';
9
9
  import ReadyResource from 'ready-resource';
10
10
  import { Files } from './files/index.js';
11
11
  import MeshDefinition from '../../spec/mesh/hyperdb/index.js';
12
- import { Router, encode } from '../../spec/mesh/hyperdispatch/index.js';
12
+ import { Router, encode, version } from '../../spec/mesh/hyperdispatch/index.js';
13
13
  import { syncChanges } from './sync/index.js';
14
14
  import { ViewUpdates } from './sync/view-updates.js';
15
15
  import { BackgroundSync } from './sync/background.js';
16
16
  import { buildDefaultSeed, isOwnKickedRow, isOwnRevokedRow } from './helpers/index.js';
17
17
  import { raceResource } from '../race-resource.js';
18
18
  import { signDeviceAttestation, signWriterClaim, verifyWriterClaim } from './device-attestation.js';
19
- import { devicesByWriterKey, liveDeviceForWriter } from './contract/activity.js';
19
+ import { devicesByWriterKey, devicesOfMember, liveDeviceForWriter } from './contract/activity.js';
20
20
  import { MESH_NAME_ROW } from './contract/mesh-name.js';
21
21
  import { versionedOp } from './contract/oldest-version.js';
22
22
  import { KNOWN_RULES, rulesVersion } from './contract/mesh-rules.js';
@@ -31,10 +31,9 @@ import { CoreError, isCoreError } from '../errors.js';
31
31
  import { ACTIVE_AGENTS, ACTIVE_CHATS, DEVICES, LIVE_WRITERS_BY_MOST_RECENT, MEMBERS, MESH_NAMES } from './collections.js';
32
32
  const [MESH_KEYPAIR, MESH_ENCRYPTION, MESH_DEVICE] = crypto.namespace('qvac-assistant/mesh', 3);
33
33
  const DEFAULT_NAMESPACE = b4a.alloc(32);
34
+ export const SCHEMA_VERSION = version;
34
35
  const SLOW_WRITE_MS = 5000;
35
36
  const SLOW_DRAIN_MS = 15_000;
36
- const SYNC_BATCH = 64;
37
- const SYNC_PAUSE_MS = 100;
38
37
  const BLOB_CORE = 'qvac-blobs';
39
38
  export function meshPublicKeyFromSeed(seed) {
40
39
  return crypto.keyPair(crypto.hash([MESH_KEYPAIR, seed])).publicKey;
@@ -58,7 +57,7 @@ const WARMUP_COLLECTIONS = [
58
57
  '@qvac/members',
59
58
  '@qvac/active-chats'
60
59
  ];
61
- export async function isOurTrustedWriter(snap, writerKey, ourMemberId) {
60
+ export async function isTrustedWriter(snap, writerKey, { ourMemberId }) {
62
61
  const device = await liveDeviceForWriter(snap, writerKey);
63
62
  if (!device) {
64
63
  return false;
@@ -66,7 +65,7 @@ export async function isOurTrustedWriter(snap, writerKey, ourMemberId) {
66
65
  if (!ourMemberId) {
67
66
  return true;
68
67
  }
69
- return !!device.memberId && b4a.equals(device.memberId, ourMemberId);
68
+ return !!device.memberId;
70
69
  }
71
70
  export function claimableDevice(row) {
72
71
  return {
@@ -124,6 +123,8 @@ export class Mesh extends ReadyResource {
124
123
  _slowDrainMs;
125
124
  _stallReported = false;
126
125
  _admitted = false;
126
+ _refused = false;
127
+ _kickSeen = null;
127
128
  _birth = null;
128
129
  _rulesRaised = null;
129
130
  _autobee = null;
@@ -288,8 +289,6 @@ export class Mesh extends ReadyResource {
288
289
  this._scope = ['p2p', 'mesh', b4a.toHex(this._keyPair.publicKey).slice(0, 8)];
289
290
  registerRoutes(this.router, { role: this._role });
290
291
  this._background = new BackgroundSync(this, {
291
- batch: opts.syncBatch ?? SYNC_BATCH,
292
- pauseMs: opts.syncPauseMs ?? SYNC_PAUSE_MS,
293
292
  logger: this._log,
294
293
  scope: this._scope
295
294
  });
@@ -297,6 +296,7 @@ export class Mesh extends ReadyResource {
297
296
  async _open() {
298
297
  this._openedAt = Date.now();
299
298
  this._log?.info(this._scope, 'opening', {
299
+ schema: SCHEMA_VERSION,
300
300
  creator: this._key === null,
301
301
  role: this._role,
302
302
  bootFrom: this._bootFrom?.length ?? null,
@@ -528,25 +528,33 @@ export class Mesh extends ReadyResource {
528
528
  }
529
529
  async _watchStall(pending, watch) {
530
530
  const started = Date.now();
531
- let reported = false;
532
- const timer = setTimeout(() => {
533
- if (this._stallReported) {
534
- return;
535
- }
536
- this._stallReported = true;
537
- reported = true;
538
- this._reportStall(watch, started).catch(noop);
539
- }, watch.ms);
531
+ const clock = { reports: 0, timer: null };
532
+ const report = (wait) => {
533
+ clock.timer = setTimeout(() => {
534
+ if (!this.closing) {
535
+ report(wait * 2);
536
+ }
537
+ if (clock.reports === 0 && this._stallReported) {
538
+ return;
539
+ }
540
+ this._stallReported = true;
541
+ clock.reports++;
542
+ this._reportStall(watch, { started, reports: clock.reports }).catch(noop);
543
+ }, wait);
544
+ };
545
+ report(watch.ms);
540
546
  try {
541
547
  await pending;
542
- this._endStall(watch, { reported, started, failed: false });
548
+ this._endStall(watch, { reported: clock.reports > 0, started, failed: false });
543
549
  }
544
550
  catch (err) {
545
- this._endStall(watch, { reported, started, failed: true, err });
551
+ this._endStall(watch, { reported: clock.reports > 0, started, failed: true, err });
546
552
  throw err;
547
553
  }
548
554
  finally {
549
- clearTimeout(timer);
555
+ if (clock.timer) {
556
+ clearTimeout(clock.timer);
557
+ }
550
558
  }
551
559
  }
552
560
  _endStall({ what, op }, { reported, started, failed, err }) {
@@ -562,23 +570,39 @@ export class Mesh extends ReadyResource {
562
570
  this._log?.info(this._scope, `${what} settled`, { op, ms });
563
571
  }
564
572
  }
565
- async _reportStall({ what, op }, started) {
566
- const { stats, system } = this.autobee;
573
+ async _reportStall({ what, op }, { started, reports }) {
574
+ const { stats, system, writable } = this.autobee;
567
575
  const missing = await this._coresNotHeld([system.bee, this.autobee.bee]).catch(noop);
576
+ const fastForwarding = await this._fastForwardHeld().catch(noop);
577
+ const writers = writable ? undefined : await this._writersHeld().catch(noop);
568
578
  this._log?.warn(this._scope, `${what} still pending`, {
569
579
  op,
580
+ reports,
570
581
  afterMs: Date.now() - started,
571
- writable: this.autobee.writable,
582
+ writable,
572
583
  interrupted: !!this.autobee.interrupted,
573
584
  drains: stats.drains,
574
585
  applies: stats.applies,
575
586
  appends: stats.appends,
576
587
  fastForwards: stats.fastForwards,
588
+ fastForwarding,
577
589
  localLength: this.autobee.local.length,
578
590
  missing,
591
+ writers,
579
592
  blindPeers: this.blindPeerStats()
580
593
  });
581
594
  }
595
+ async _fastForwardHeld() {
596
+ const ff = this.autobee.fastForwarding;
597
+ if (!ff) {
598
+ return null;
599
+ }
600
+ return { to: ff.head.length, ...(await this._coreHeld(ff.head.key)) };
601
+ }
602
+ async _writersHeld() {
603
+ const writers = [...this.autobee.activeWriters];
604
+ return Promise.all(writers.map((writer) => this._coreHeld(writer.core.key)));
605
+ }
582
606
  async _coresNotHeld(views) {
583
607
  const keys = new Map();
584
608
  for (const { context } of views) {
@@ -593,11 +617,14 @@ export class Mesh extends ReadyResource {
593
617
  const core = this._store.get({ key, active: false });
594
618
  try {
595
619
  await core.ready();
620
+ const reach = core.peers.map(remoteReach);
596
621
  return {
597
622
  key: b4a.toHex(key).slice(0, 8),
598
623
  length: core.length,
599
624
  held: core.contiguousLength,
600
- peers: core.peers.length
625
+ peers: core.peers.length,
626
+ peerLength: Math.max(0, ...reach.map((peer) => peer.length)),
627
+ peerHeld: Math.max(0, ...reach.map((peer) => peer.held))
601
628
  };
602
629
  }
603
630
  finally {
@@ -795,9 +822,9 @@ export class Mesh extends ReadyResource {
795
822
  return false;
796
823
  const snap = reference.db.snapshot();
797
824
  try {
798
- const trusted = await isOurTrustedWriter(snap, key, this._ourMemberId);
825
+ const trusted = await isTrustedWriter(snap, key, this);
799
826
  if (!trusted) {
800
- this._log?.debug(this._scope, 'writer is no device of ours', {
827
+ this._log?.debug(this._scope, 'writer is no live device of this mesh', {
801
828
  writerKey: b4a.toHex(key).slice(0, 16)
802
829
  });
803
830
  }
@@ -815,7 +842,7 @@ export class Mesh extends ReadyResource {
815
842
  for await (const writer of target.db.find(LIVE_WRITERS_BY_MOST_RECENT, {
816
843
  reverse: true
817
844
  })) {
818
- if ((await isOurTrustedWriter(snap, writer.writerKey, this._ourMemberId)) ||
845
+ if ((await isTrustedWriter(snap, writer.writerKey, this)) ||
819
846
  (await this._vouchedByItsRow(target, reference, writer.writerKey))) {
820
847
  this._log?.debug(this._scope, 'vouching for a head', {
821
848
  writerKey: b4a.toHex(writer.writerKey).slice(0, 16),
@@ -824,7 +851,7 @@ export class Mesh extends ReadyResource {
824
851
  return { key: writer.writerKey, length: writer.length };
825
852
  }
826
853
  }
827
- this._log?.debug(this._scope, 'vouching for no head: no writer of ours has been active');
854
+ this._log?.debug(this._scope, 'vouching for no head: no live writer has been active');
828
855
  return null;
829
856
  }
830
857
  finally {
@@ -862,8 +889,11 @@ export class Mesh extends ReadyResource {
862
889
  return { db };
863
890
  }
864
891
  async _autobeeWarmup(view) {
892
+ let snap = null;
865
893
  try {
866
- const counts = await Promise.all(WARMUP_COLLECTIONS.map((collection) => drain(view.db.find(collection))));
894
+ const read = view.db.snapshot();
895
+ snap = read;
896
+ const counts = await Promise.all(WARMUP_COLLECTIONS.map((collection) => drain(read.find(collection))));
867
897
  const warmed = counts.reduce((total, count) => total + count, 0);
868
898
  this._log?.info(this._scope, 'warmed candidate view', {
869
899
  warmed,
@@ -875,6 +905,9 @@ export class Mesh extends ReadyResource {
875
905
  catch (err) {
876
906
  this._log?.warn(this._scope, 'warmup failed', err);
877
907
  }
908
+ finally {
909
+ await snap?.close();
910
+ }
878
911
  }
879
912
  async _autobeeApply(nodes, view, host) {
880
913
  const tx = view.db.transaction();
@@ -983,17 +1016,11 @@ export class Mesh extends ReadyResource {
983
1016
  }
984
1017
  async _readLanding() {
985
1018
  const started = Date.now();
986
- const [own] = await Promise.all([
987
- this.refreshOurMember(),
988
- this.refreshMembership(),
989
- this.refreshName()
990
- ]);
1019
+ await Promise.all([this.refreshOurMember(), this.refreshMembership(), this.refreshName()]);
991
1020
  if (this.closing) {
992
1021
  return;
993
1022
  }
994
- if (own && isOwnKickedRow(this, own)) {
995
- this.emit('kicked');
996
- }
1023
+ await this.kickDirty();
997
1024
  if (this._membership.unattested > 0) {
998
1025
  this.emit('unattested-device');
999
1026
  }
@@ -1005,6 +1032,8 @@ export class Mesh extends ReadyResource {
1005
1032
  async checkOwnAdmission() {
1006
1033
  if (this.autobee.writable) {
1007
1034
  this._admitted = true;
1035
+ this._refused = false;
1036
+ await this.kickDirty();
1008
1037
  return;
1009
1038
  }
1010
1039
  if (!this._admitted || !this._deviceId) {
@@ -1018,33 +1047,68 @@ export class Mesh extends ReadyResource {
1018
1047
  return;
1019
1048
  }
1020
1049
  this._admitted = false;
1021
- this.emit('kicked');
1050
+ this._refused = true;
1051
+ await this.kickDirty();
1052
+ }
1053
+ async kickDirty() {
1054
+ const seen = await this._kickState();
1055
+ if (seen === this._kickSeen || this.closing) {
1056
+ return;
1057
+ }
1058
+ this._kickSeen = seen;
1059
+ this.emit('kick-dirty');
1060
+ }
1061
+ async _kickState() {
1062
+ const writable = this.autobee.writable;
1063
+ const own = await this._ownRow();
1064
+ const member = this.ourMemberId;
1065
+ const devices = !writable && member && this._role === 'workspace'
1066
+ ? await devicesOfMember(this.view, member)
1067
+ : [];
1068
+ return JSON.stringify([
1069
+ writable,
1070
+ this._refused,
1071
+ member && b4a.toHex(member),
1072
+ own && [
1073
+ own.revoked,
1074
+ own.revokedBy && b4a.toHex(own.revokedBy),
1075
+ own.writerKey && b4a.toHex(own.writerKey)
1076
+ ],
1077
+ devices.map((device) => [b4a.toHex(device.id), device.revoked])
1078
+ ]);
1079
+ }
1080
+ async readKicked() {
1081
+ if (this.autobee.writable)
1082
+ return false;
1083
+ const own = await this._ownRow();
1084
+ if (own && isOwnKickedRow(this, own)) {
1085
+ return true;
1086
+ }
1087
+ return this._refused;
1088
+ }
1089
+ async checkResumedRefusal() {
1090
+ if (!(await this.ownWriterRefused())) {
1091
+ return false;
1092
+ }
1093
+ this._refused = true;
1094
+ return true;
1022
1095
  }
1023
1096
  async checkResumedRevocation() {
1024
- const snap = this.view.snapshot();
1025
- try {
1026
- for await (const device of snap.find(DEVICES)) {
1027
- if (!isOwnRevokedRow(this, device))
1028
- continue;
1029
- return isOwnKickedRow(this, device) ? 'kicked' : 'left';
1030
- }
1097
+ const own = await this._ownRow();
1098
+ if (!own || !isOwnRevokedRow(this, own)) {
1031
1099
  return null;
1032
1100
  }
1033
- finally {
1034
- await snap.close();
1035
- }
1101
+ return isOwnKickedRow(this, own) ? 'kicked' : 'left';
1036
1102
  }
1037
1103
  async ownWriterRefused() {
1038
1104
  const info = await this.autobee.system.get(this.localWriterKey, { unflushed: true });
1039
1105
  if (!info?.isRemoved) {
1040
1106
  return false;
1041
1107
  }
1042
- for await (const device of this.view.find(DEVICES)) {
1043
- if (device.writerKey && b4a.equals(device.writerKey, this.localWriterKey)) {
1044
- return false;
1045
- }
1046
- }
1047
- return true;
1108
+ return !(await liveDeviceForWriter(this.view, this.localWriterKey));
1109
+ }
1110
+ async _ownRow() {
1111
+ return this._deviceId ? this.view.get(DEVICES, { id: this._deviceId }) : null;
1048
1112
  }
1049
1113
  }
1050
1114
  export class MeshBatch {
@@ -1082,6 +1146,15 @@ export class MeshBatch {
1082
1146
  }
1083
1147
  }
1084
1148
  function noop() { }
1149
+ function remoteReach(peer) {
1150
+ if (!isRecord(peer)) {
1151
+ return { length: 0, held: 0 };
1152
+ }
1153
+ return {
1154
+ length: typeof peer.remoteLength === 'number' ? peer.remoteLength : 0,
1155
+ held: typeof peer.remoteContiguousLength === 'number' ? peer.remoteContiguousLength : 0
1156
+ };
1157
+ }
1085
1158
  function viewCoreOf(head) {
1086
1159
  return isRecord(head) && b4a.isBuffer(head.key) ? b4a.toHex(head.key).slice(0, 8) : null;
1087
1160
  }
@@ -0,0 +1,4 @@
1
+ import type { Mesh } from '../mesh.ts';
2
+ export declare const REINDEX_FROM_APP_VERSION = "0.21.0";
3
+ export declare function floorExcludesBefore(range: string | null | undefined, release: string): boolean;
4
+ export declare function backfillChatChunks(mesh: Mesh): Promise<number>;
@@ -0,0 +1,47 @@
1
+ import { Range, Version, constants, satisfies } from 'bare-semver';
2
+ import { CHAT_CHUNKS, CHUNK_COUNTERS, PURGED_CHATS } from '../collections.js';
3
+ import { inRole } from '../contract/roles.js';
4
+ import { releaseVersion } from '../min-version.js';
5
+ export const REINDEX_FROM_APP_VERSION = '0.21.0';
6
+ export function floorExcludesBefore(range, release) {
7
+ let floor;
8
+ try {
9
+ floor = Range.parse(range ?? '');
10
+ }
11
+ catch {
12
+ return false;
13
+ }
14
+ const atLeast = Range.parse(`>=${releaseVersion(release)}`);
15
+ return (floor.comparators.length > 0 &&
16
+ floor.comparators.every((set) => set.some((bound) => LOWER_BOUNDS.has(bound.operator) && satisfies(releaseOf(bound.version), atLeast))));
17
+ }
18
+ const LOWER_BOUNDS = new Set([constants.GTE, constants.GT, constants.EQ]);
19
+ function releaseOf({ major, minor, patch }) {
20
+ return new Version(major, minor, patch);
21
+ }
22
+ export async function backfillChatChunks(mesh) {
23
+ if (!inRole(mesh.role, '@qvac/reindex-chat-chunks')) {
24
+ return 0;
25
+ }
26
+ const counters = await mesh.view.find(CHUNK_COUNTERS).toArray();
27
+ const purged = await mesh.view.find(PURGED_CHATS).toArray();
28
+ let sent = 0;
29
+ for (const chat of [...counters, ...purged]) {
30
+ if (mesh.closing || !mesh.writable)
31
+ break;
32
+ const due = 'count' in chat ? !(await holdsEnds(mesh.view, chat)) : await holdsAny(mesh.view, chat);
33
+ if (due) {
34
+ await mesh.dispatch('reindex-chat-chunks', { chatId: chat.chatId });
35
+ sent++;
36
+ }
37
+ }
38
+ return sent;
39
+ }
40
+ async function holdsEnds(db, { chatId, count }) {
41
+ const first = await db.get(CHAT_CHUNKS, { chatId, seq: 0 });
42
+ const last = await db.get(CHAT_CHUNKS, { chatId, seq: count - 1 });
43
+ return first !== null && last !== null;
44
+ }
45
+ async function holdsAny(db, { chatId }) {
46
+ return ((await db.find(CHAT_CHUNKS, { gte: { chatId }, lte: { chatId } }).one()) !== null);
47
+ }
@@ -1,2 +1,2 @@
1
1
  import type { Mesh } from '../mesh.ts';
2
- export declare function backfillFileIndex(mesh: Mesh): Promise<void>;
2
+ export declare function backfillFileIndex(mesh: Mesh): Promise<number>;