@qvac/core 1.0.0-rc.8 → 1.0.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 (226) hide show
  1. package/CHANGELOG.md +234 -3
  2. package/README.md +1 -0
  3. package/dist-lib/lib/api/attachments.d.ts +1 -1
  4. package/dist-lib/lib/api/attachments.js +11 -0
  5. package/dist-lib/lib/api/chunks.d.ts +4 -2
  6. package/dist-lib/lib/api/chunks.js +6 -3
  7. package/dist-lib/lib/api/devices.js +6 -18
  8. package/dist-lib/lib/api/execution.d.ts +6 -2
  9. package/dist-lib/lib/api/execution.js +29 -3
  10. package/dist-lib/lib/api/host-run-log.d.ts +7 -1
  11. package/dist-lib/lib/api/host-run-log.js +3 -1
  12. package/dist-lib/lib/api/index.d.ts +3 -1
  13. package/dist-lib/lib/api/index.js +17 -6
  14. package/dist-lib/lib/api/members.js +13 -29
  15. package/dist-lib/lib/api/mesh.d.ts +2 -1
  16. package/dist-lib/lib/api/projects.d.ts +1 -1
  17. package/dist-lib/lib/api/projects.js +9 -5
  18. package/dist-lib/lib/api/rag-label.d.ts +4 -0
  19. package/dist-lib/lib/api/rag-label.js +21 -0
  20. package/dist-lib/lib/api/rag-lexical.d.ts +22 -0
  21. package/dist-lib/lib/api/rag-lexical.js +1058 -0
  22. package/dist-lib/lib/api/rag-manifest.d.ts +13 -0
  23. package/dist-lib/lib/api/rag-manifest.js +46 -0
  24. package/dist-lib/lib/api/rag-outline.d.ts +36 -0
  25. package/dist-lib/lib/api/rag-outline.js +426 -0
  26. package/dist-lib/lib/api/rag-plugin.d.ts +20 -0
  27. package/dist-lib/lib/api/rag-plugin.js +70 -0
  28. package/dist-lib/lib/api/rag-synthesis.d.ts +7 -0
  29. package/dist-lib/lib/api/rag-synthesis.js +102 -0
  30. package/dist-lib/lib/api/rag-targets.d.ts +23 -0
  31. package/dist-lib/lib/api/rag-targets.js +215 -0
  32. package/dist-lib/lib/api/rag-window.d.ts +5 -0
  33. package/dist-lib/lib/api/rag-window.js +46 -0
  34. package/dist-lib/lib/api/rag.d.ts +3 -2
  35. package/dist-lib/lib/api/rag.js +421 -181
  36. package/dist-lib/lib/api/served.js +8 -14
  37. package/dist-lib/lib/api/workspace.d.ts +8 -1
  38. package/dist-lib/lib/api/workspace.js +27 -13
  39. package/dist-lib/lib/assistant/harness.d.ts +9 -1
  40. package/dist-lib/lib/assistant/history.d.ts +11 -2
  41. package/dist-lib/lib/assistant/history.js +74 -4
  42. package/dist-lib/lib/assistant/media-round.d.ts +61 -0
  43. package/dist-lib/lib/assistant/media-round.js +111 -0
  44. package/dist-lib/lib/assistant/message.d.ts +7 -0
  45. package/dist-lib/lib/assistant/model-round.d.ts +12 -0
  46. package/dist-lib/lib/assistant/model-round.js +130 -17
  47. package/dist-lib/lib/assistant/pdf-pages.d.ts +16 -1
  48. package/dist-lib/lib/assistant/pdf-pages.js +67 -3
  49. package/dist-lib/lib/assistant/round-host.d.ts +22 -0
  50. package/dist-lib/lib/assistant/round-host.js +142 -8
  51. package/dist-lib/lib/assistant/upload.d.ts +12 -0
  52. package/dist-lib/lib/assistant/upload.js +36 -0
  53. package/dist-lib/lib/assistant-client.d.ts +1 -0
  54. package/dist-lib/lib/assistant-client.js +40 -19
  55. package/dist-lib/lib/assistant-tree.d.ts +2 -0
  56. package/dist-lib/lib/assistant-tree.js +41 -1
  57. package/dist-lib/lib/assistant.d.ts +15 -0
  58. package/dist-lib/lib/assistant.js +474 -89
  59. package/dist-lib/lib/client.d.ts +1 -0
  60. package/dist-lib/lib/client.js +4 -1
  61. package/dist-lib/lib/compaction.js +2 -2
  62. package/dist-lib/lib/core/agent.js +12 -45
  63. package/dist-lib/lib/core/approval-rules.js +1 -8
  64. package/dist-lib/lib/core/archived-mesh.d.ts +2 -1
  65. package/dist-lib/lib/core/archived-mesh.js +12 -8
  66. package/dist-lib/lib/core/chat-copy.d.ts +18 -3
  67. package/dist-lib/lib/core/chat-copy.js +74 -25
  68. package/dist-lib/lib/core/chat-fork.js +15 -6
  69. package/dist-lib/lib/core/chat-group.js +16 -24
  70. package/dist-lib/lib/core/chat-move.d.ts +9 -0
  71. package/dist-lib/lib/core/chat-move.js +94 -3
  72. package/dist-lib/lib/core/chat-order.js +12 -18
  73. package/dist-lib/lib/core/chat.js +38 -31
  74. package/dist-lib/lib/core/chunk.d.ts +2 -2
  75. package/dist-lib/lib/core/chunk.js +18 -25
  76. package/dist-lib/lib/core/collections.d.ts +2 -0
  77. package/dist-lib/lib/core/collections.js +2 -0
  78. package/dist-lib/lib/core/contract/activity.d.ts +4 -0
  79. package/dist-lib/lib/core/contract/activity.js +34 -4
  80. package/dist-lib/lib/core/contract/chat-group.d.ts +1 -0
  81. package/dist-lib/lib/core/contract/chat-group.js +21 -1
  82. package/dist-lib/lib/core/contract/chat.d.ts +9 -0
  83. package/dist-lib/lib/core/contract/chat.js +127 -5
  84. package/dist-lib/lib/core/contract/chunks.d.ts +1 -0
  85. package/dist-lib/lib/core/contract/chunks.js +37 -6
  86. package/dist-lib/lib/core/contract/features.d.ts +21 -0
  87. package/dist-lib/lib/core/contract/features.js +22 -0
  88. package/dist-lib/lib/core/contract/index.js +2 -0
  89. package/dist-lib/lib/core/contract/indexing.js +4 -2
  90. package/dist-lib/lib/core/contract/knowledge.js +5 -4
  91. package/dist-lib/lib/core/contract/members.js +16 -10
  92. package/dist-lib/lib/core/contract/mesh-kind.d.ts +1 -0
  93. package/dist-lib/lib/core/contract/mesh-kind.js +3 -1
  94. package/dist-lib/lib/core/contract/projects.d.ts +5 -0
  95. package/dist-lib/lib/core/contract/projects.js +13 -5
  96. package/dist-lib/lib/core/contract/roles.js +32 -4
  97. package/dist-lib/lib/core/contract/run-requests.d.ts +1 -0
  98. package/dist-lib/lib/core/contract/run-requests.js +35 -2
  99. package/dist-lib/lib/core/contract/seed-defaults.js +2 -2
  100. package/dist-lib/lib/core/contract/tool-configs.js +3 -2
  101. package/dist-lib/lib/core/contract/workspace.d.ts +2 -0
  102. package/dist-lib/lib/core/contract/workspace.js +40 -7
  103. package/dist-lib/lib/core/contract/writers.js +2 -2
  104. package/dist-lib/lib/core/departures.d.ts +3 -1
  105. package/dist-lib/lib/core/departures.js +3 -0
  106. package/dist-lib/lib/core/devices.js +1 -8
  107. package/dist-lib/lib/core/files/attachments.d.ts +2 -0
  108. package/dist-lib/lib/core/files/attachments.js +19 -1
  109. package/dist-lib/lib/core/files/index.d.ts +20 -4
  110. package/dist-lib/lib/core/files/index.js +132 -21
  111. package/dist-lib/lib/core/files/processing/document.d.ts +4 -0
  112. package/dist-lib/lib/core/files/processing/document.js +7 -0
  113. package/dist-lib/lib/core/files/processing/index.d.ts +13 -0
  114. package/dist-lib/lib/core/files/processing/index.js +144 -12
  115. package/dist-lib/lib/core/files/processing/protocol.js +13 -34
  116. package/dist-lib/lib/core/files/processing/state.d.ts +2 -0
  117. package/dist-lib/lib/core/files/rag/build.d.ts +19 -2
  118. package/dist-lib/lib/core/files/rag/build.js +123 -19
  119. package/dist-lib/lib/core/files/rag/failure.js +2 -1
  120. package/dist-lib/lib/core/files/rag/index.d.ts +11 -3
  121. package/dist-lib/lib/core/files/rag/index.js +13 -1
  122. package/dist-lib/lib/core/files/rag/load.d.ts +6 -3
  123. package/dist-lib/lib/core/files/rag/load.js +27 -6
  124. package/dist-lib/lib/core/files/rag/plugin-index.d.ts +32 -0
  125. package/dist-lib/lib/core/files/rag/plugin-index.js +127 -0
  126. package/dist-lib/lib/core/files/rag/produce.d.ts +5 -3
  127. package/dist-lib/lib/core/files/rag/produce.js +104 -16
  128. package/dist-lib/lib/core/files/rag/retrieval/full-text.d.ts +3 -1
  129. package/dist-lib/lib/core/files/rag/retrieval/full-text.js +62 -13
  130. package/dist-lib/lib/core/files/rag/text-cache.d.ts +28 -0
  131. package/dist-lib/lib/core/files/rag/text-cache.js +141 -0
  132. package/dist-lib/lib/core/files/records/knowledge.d.ts +1 -3
  133. package/dist-lib/lib/core/files/records/knowledge.js +7 -40
  134. package/dist-lib/lib/core/files/records/reclaim.js +5 -10
  135. package/dist-lib/lib/core/files/upload.js +68 -41
  136. package/dist-lib/lib/core/fs-drives.js +14 -26
  137. package/dist-lib/lib/core/invite.d.ts +1 -0
  138. package/dist-lib/lib/core/invite.js +4 -2
  139. package/dist-lib/lib/core/local/files.d.ts +1 -1
  140. package/dist-lib/lib/core/local/files.js +5 -5
  141. package/dist-lib/lib/core/local/index.d.ts +13 -6
  142. package/dist-lib/lib/core/local/index.js +103 -69
  143. package/dist-lib/lib/core/local/run-logs.d.ts +1 -1
  144. package/dist-lib/lib/core/local/run-logs.js +5 -5
  145. package/dist-lib/lib/core/mesh-processor.d.ts +19 -0
  146. package/dist-lib/lib/core/mesh-processor.js +87 -0
  147. package/dist-lib/lib/core/mesh.d.ts +48 -5
  148. package/dist-lib/lib/core/mesh.js +318 -129
  149. package/dist-lib/lib/core/migrations/agent-catalog.js +2 -8
  150. package/dist-lib/lib/core/migrations/agent-tools-scope.js +5 -11
  151. package/dist-lib/lib/core/migrations/workspace-from-0-21/index.d.ts +88 -0
  152. package/dist-lib/lib/core/migrations/workspace-from-0-21/index.js +731 -0
  153. package/dist-lib/lib/core/migrations/writer-links.d.ts +2 -0
  154. package/dist-lib/lib/core/migrations/writer-links.js +32 -0
  155. package/dist-lib/lib/core/project-keys.d.ts +6 -0
  156. package/dist-lib/lib/core/project-keys.js +11 -0
  157. package/dist-lib/lib/core/public-mesh.d.ts +3 -0
  158. package/dist-lib/lib/core/public-mesh.js +10 -8
  159. package/dist-lib/lib/core/run-requests.d.ts +3 -0
  160. package/dist-lib/lib/core/run-requests.js +21 -1
  161. package/dist-lib/lib/core/skill-availability.d.ts +33 -0
  162. package/dist-lib/lib/core/skill-availability.js +67 -0
  163. package/dist-lib/lib/core/sync/background.d.ts +0 -1
  164. package/dist-lib/lib/core/sync/background.js +2 -1
  165. package/dist-lib/lib/core/sync/changed.d.ts +4 -2
  166. package/dist-lib/lib/core/sync/changed.js +1 -1
  167. package/dist-lib/lib/core/sync/index.js +52 -10
  168. package/dist-lib/lib/core/sync/view-updates.d.ts +18 -12
  169. package/dist-lib/lib/core/sync/view-updates.js +82 -91
  170. package/dist-lib/lib/core/sync-status.js +97 -41
  171. package/dist-lib/lib/core/tool-activity.js +6 -12
  172. package/dist-lib/lib/core/tool-configs.js +17 -29
  173. package/dist-lib/lib/core/upgrade-summary.d.ts +19 -0
  174. package/dist-lib/lib/core/upgrade-summary.js +67 -0
  175. package/dist-lib/lib/core/view-read.d.ts +4 -0
  176. package/dist-lib/lib/core/view-read.js +23 -0
  177. package/dist-lib/lib/core/workspace-upgrade.d.ts +7 -0
  178. package/dist-lib/lib/core/workspace-upgrade.js +27 -16
  179. package/dist-lib/lib/core.d.ts +39 -42
  180. package/dist-lib/lib/core.js +661 -639
  181. package/dist-lib/lib/errors.d.ts +20 -0
  182. package/dist-lib/lib/errors.js +20 -0
  183. package/dist-lib/lib/llm-model-names.d.ts +1 -1
  184. package/dist-lib/lib/llm-model-names.js +1 -1
  185. package/dist-lib/lib/mesh-scoped-engine.js +4 -0
  186. package/dist-lib/lib/model-context.js +1 -2
  187. package/dist-lib/lib/probe-rounds.d.ts +7 -2
  188. package/dist-lib/lib/probe-rounds.js +6 -1
  189. package/dist-lib/lib/run-transcript.d.ts +4 -1
  190. package/dist-lib/lib/run-transcript.js +15 -6
  191. package/dist-lib/schema/engine.js +83 -6
  192. package/dist-lib/schema/indexes.d.ts +3 -0
  193. package/dist-lib/schema/indexes.js +5 -0
  194. package/dist-lib/schema/local.js +11 -2
  195. package/dist-lib/schema/qvac.js +49 -5
  196. package/dist-lib/spec/bind.js +34 -0
  197. package/dist-lib/spec/calls.js +5 -0
  198. package/dist-lib/spec/capabilities.d.ts +17 -0
  199. package/dist-lib/spec/hrpc/hrpc.json +65 -0
  200. package/dist-lib/spec/hrpc/index.js +68 -5
  201. package/dist-lib/spec/hrpc/messages.js +260 -23
  202. package/dist-lib/spec/hyperschema/index.js +260 -23
  203. package/dist-lib/spec/hyperschema/schema.json +181 -0
  204. package/dist-lib/spec/hyperschema/types.d.ts +48 -0
  205. package/dist-lib/spec/local/hyperdb/messages.js +270 -224
  206. package/dist-lib/spec/local/hyperschema/index.js +73 -38
  207. package/dist-lib/spec/local/hyperschema/schema.json +31 -0
  208. package/dist-lib/spec/local/hyperschema/types.d.ts +8 -0
  209. package/dist-lib/spec/mesh/hyperdb/db.json +39 -2
  210. package/dist-lib/spec/mesh/hyperdb/index.js +120 -3
  211. package/dist-lib/spec/mesh/hyperdb/messages.js +1328 -838
  212. package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +30 -0
  213. package/dist-lib/spec/mesh/hyperdispatch/index.js +86 -1
  214. package/dist-lib/spec/mesh/hyperdispatch/messages.js +682 -287
  215. package/dist-lib/spec/mesh/hyperschema/index.js +682 -287
  216. package/dist-lib/spec/mesh/hyperschema/schema.json +201 -0
  217. package/dist-lib/spec/mesh/hyperschema/types.d.ts +52 -0
  218. package/package.json +75 -70
  219. package/tui/README.md +2 -1
  220. package/tui/app.mjs +28 -7
  221. package/tui/boot.mjs +5 -1
  222. package/tui/lib/attachment-files.mjs +82 -0
  223. package/tui/lib/reopen-meshes.mjs +15 -0
  224. package/tui/main.mjs +4 -43
  225. package/worker/assistant-sidecar.mjs +10 -3
  226. package/worker/stow-entry.mjs +3 -52
@@ -2,131 +2,122 @@ import b4a from 'b4a';
2
2
  export class ViewUpdates {
3
3
  _run;
4
4
  _pending;
5
- _next = null;
6
- _running = null;
7
- _kept = null;
5
+ _onerror;
6
+ _pushing = new Set();
7
+ _waiting = [];
8
+ _draining = null;
9
+ _written = 0;
10
+ _bumped = false;
8
11
  _started = false;
9
12
  _closed = false;
10
- constructor(run, pending = null) {
13
+ constructor(run, { pending, onerror }) {
11
14
  this._run = run;
12
15
  this._pending = pending;
16
+ this._onerror = onerror;
13
17
  }
14
- async start() {
15
- try {
16
- const resumed = await this._pending?.read();
17
- if (this._closed) {
18
- return;
19
- }
20
- if (resumed) {
21
- this._kept = resumed;
22
- this._queue(resumed, { older: true });
23
- }
24
- if (this._next) {
25
- await this._keep(this._next.update);
26
- }
27
- }
28
- finally {
29
- this._started = true;
30
- this._kick();
31
- }
18
+ start() {
19
+ this._started = true;
20
+ this.bump();
32
21
  }
33
22
  push(update) {
34
- const next = this._queue(update);
35
- if (!this._started) {
36
- return Promise.resolve();
23
+ const pushing = this._pending
24
+ .update((row) => (row ? merged(row, update) : update))
25
+ .then(() => {
26
+ this._written++;
27
+ this.bump();
28
+ });
29
+ this._pushing.add(pushing);
30
+ return pushing.finally(() => this._pushing.delete(pushing));
31
+ }
32
+ bump() {
33
+ this._bumped = true;
34
+ if (this._started && !this._closed && !this._draining) {
35
+ this._draining = this._drain().finally(() => {
36
+ this._draining = null;
37
+ if (this._bumped && !this._closed) {
38
+ this.bump();
39
+ }
40
+ else {
41
+ this._release(Infinity);
42
+ }
43
+ });
37
44
  }
38
- const running = this._running && !this._running.done ? this._running : null;
39
- const written = this._keep(running ? merged(running.update, next) : next);
40
- this._kick();
41
- return written;
42
45
  }
43
46
  close() {
44
47
  this._closed = true;
45
- this._running?.settle();
46
- this._next?.settle();
47
48
  }
48
- flush() {
49
- return (this._next ?? this._running)?.synced ?? Promise.resolve();
50
- }
51
- _kick() {
52
- if (this._started && !this._closed && this._next && !this._running) {
53
- void this._drain();
49
+ async flush() {
50
+ await Promise.allSettled([...this._pushing]);
51
+ if (!this._draining) {
52
+ return;
54
53
  }
54
+ const written = this._written;
55
+ await new Promise((resolve) => this._waiting.push({ written, resolve }));
55
56
  }
56
57
  async _drain() {
58
+ while (this._bumped && !this._closed) {
59
+ this._bumped = false;
60
+ await this._passes();
61
+ }
62
+ }
63
+ async _passes() {
64
+ let seen = this._written;
57
65
  try {
58
- while (this._next) {
59
- this._running = this._next;
60
- this._next = null;
61
- const reached = await this._run(this._running.update);
66
+ for (let row = await this._pending.read(); row; row = await this._pending.read()) {
67
+ if (this._closed) {
68
+ return;
69
+ }
70
+ const reached = (await this._run(row)) || null;
62
71
  if (this._closed) {
63
72
  return;
64
73
  }
65
- this._running.done = true;
66
- await this._keepNext(reached || null);
67
- this._running.settle();
68
- this._running = null;
74
+ await this._settle(row, reached);
75
+ this._release(seen);
76
+ seen = this._written;
69
77
  }
70
78
  }
71
- finally {
72
- this._running = null;
73
- }
74
- }
75
- _queue(update, { older = false } = {}) {
76
- if (!this._next) {
77
- this._next = pass(update);
78
- }
79
- else {
80
- const queued = this._next.update;
81
- this._next.update = older ? merged(update, queued) : merged(queued, update);
79
+ catch (err) {
80
+ this._release(seen);
81
+ if (!this._closed) {
82
+ this._onerror(err);
83
+ }
82
84
  }
83
- return this._next.update;
84
85
  }
85
- _keepNext(reached) {
86
- if (!this._next) {
87
- return this._clear();
88
- }
89
- if (reached) {
90
- this._next.update = rebased(this._next.update, reached);
86
+ _release(seen) {
87
+ const waiting = this._waiting;
88
+ this._waiting = [];
89
+ for (const waiter of waiting) {
90
+ if (waiter.written <= seen) {
91
+ waiter.resolve();
92
+ }
93
+ else {
94
+ this._waiting.push(waiter);
95
+ }
91
96
  }
92
- return this._keep(this._next.update);
93
97
  }
94
- _keep(update) {
95
- if (!this._pending || (this._kept && sameUpdate(this._kept, update))) {
96
- return Promise.resolve();
97
- }
98
- this._kept = update;
99
- return this._pending.write(update);
98
+ _settle(ran, reached) {
99
+ return this._pending.update((row) => !row || sameUpdate(row, ran) ? null : { ...row, from: reached ?? row.from });
100
100
  }
101
- _clear() {
102
- if (!this._pending || !this._kept) {
101
+ }
102
+ export function memoryPending() {
103
+ let row = null;
104
+ return {
105
+ read: () => Promise.resolve(row),
106
+ update: (change) => {
107
+ row = change(row);
103
108
  return Promise.resolve();
104
109
  }
105
- this._kept = null;
106
- return this._pending.clear();
107
- }
108
- }
109
- function pass(update) {
110
- let settle = () => { };
111
- const synced = new Promise((resolve) => (settle = resolve));
112
- return { update, done: false, synced, settle };
110
+ };
113
111
  }
114
- function merged(pending, next) {
112
+ function merged(row, next) {
115
113
  return {
116
- from: pending.from,
114
+ from: row.from,
117
115
  to: next.to,
118
- incremental: pending.incremental && next.incremental
116
+ incremental: row.incremental && next.incremental
119
117
  };
120
118
  }
121
- function rebased(update, reached) {
122
- const { from } = update;
123
- if (!update.incremental || !from || !b4a.equals(from.key, reached.key)) {
124
- return update;
125
- }
126
- return reached.length > from.length ? { ...update, from: reached } : update;
127
- }
128
119
  function sameUpdate(a, b) {
129
- return a.incremental === b.incremental && sameHead(a.from, b.from);
120
+ return a.to === b.to && a.incremental === b.incremental && sameHead(a.from, b.from);
130
121
  }
131
122
  function sameHead(a, b) {
132
123
  return a && b ? a.length === b.length && b4a.equals(a.key, b.key) : a === b;
@@ -1,13 +1,22 @@
1
1
  import { unref } from '../unref.js';
2
+ import { isMissingBlock } from './view-read.js';
2
3
  import b4a from 'b4a';
3
- import { liveDevices } from './contract/activity.js';
4
+ import ScopeLock from 'scope-lock';
5
+ import { devicesByWriterKey, liveDevices } from './contract/activity.js';
6
+ import { WRITER_ACTIVITY } from './collections.js';
4
7
  const MIN_SYNCING_MS = 1000;
5
8
  export function createSyncActivity(mesh) {
6
9
  const listeners = new Set();
10
+ const lock = new ScopeLock({ debounce: true });
7
11
  const notify = () => {
8
- for (const listener of [...listeners]) {
9
- listener();
10
- }
12
+ void lock.lock().then((locked) => {
13
+ if (!locked)
14
+ return;
15
+ for (const listener of [...listeners]) {
16
+ listener();
17
+ }
18
+ lock.unlock();
19
+ });
11
20
  };
12
21
  const track = (writer) => {
13
22
  writer.core.on('append', notify);
@@ -60,40 +69,21 @@ function createSyncHold(notify) {
60
69
  };
61
70
  }
62
71
  export async function syncStatusSnapshot(mesh) {
63
- const deviceByWriter = await writerDeviceMap(mesh);
64
- const localKey = mesh.autobee.local.key;
65
- const sessions = openWriterSessions(mesh);
66
- const remoteWriters = [...new Set([...deviceByWriter.keys(), ...sessions.keys()])]
67
- .map((hex) => {
68
- const session = sessions.get(hex);
69
- const device = deviceByWriter.get(hex);
70
- return { session, device, writerKey: session ? session.core.key : b4a.from(hex, 'hex') };
71
- })
72
- .filter((writer) => !b4a.equals(writer.writerKey, localKey));
73
- const rows = await Promise.all(remoteWriters.map(async ({ session, device, writerKey }) => {
74
- const info = await mesh.autobee.system.get(writerKey, { unflushed: true });
75
- return {
76
- device,
77
- writerKey,
78
- info,
79
- length: session?.core.length ?? 0,
80
- contiguousLength: session?.core.contiguousLength ?? 0
81
- };
82
- }));
83
72
  const devices = [];
84
73
  let pendingOps = 0;
85
- for (const { device, writerKey, info, length, contiguousLength } of rows) {
86
- if (info?.isRemoved)
87
- continue;
88
- const known = Math.max(info?.length ?? 0, length);
89
- const applied = Math.max(info?.length ?? 0, contiguousLength);
90
- pendingOps += Math.max(0, known - applied);
91
- devices.push({
92
- writerKey,
93
- deviceId: device?.id,
94
- applied,
95
- known
96
- });
74
+ const snap = mesh.read();
75
+ try {
76
+ const writers = remoteWriters(mesh, await writerDeviceMap(snap));
77
+ const rows = await Promise.all(writers.map((writer) => writerProgress(mesh, snap, writer)));
78
+ for (const row of rows) {
79
+ if (!row)
80
+ continue;
81
+ pendingOps += Math.max(0, row.known - row.applied);
82
+ devices.push(row);
83
+ }
84
+ }
85
+ finally {
86
+ await snap.close();
97
87
  }
98
88
  const pendingUploads = mesh.pendingUploads();
99
89
  const syncing = mesh.syncActivity.hold.keep(pendingOps + pendingUploads > 0);
@@ -104,21 +94,85 @@ export async function syncStatusSnapshot(mesh) {
104
94
  devices
105
95
  };
106
96
  }
97
+ function remoteWriters(mesh, deviceByWriter) {
98
+ const localKey = mesh.autobee.local.key;
99
+ const sessions = openWriterSessions(mesh);
100
+ return [...new Set([...deviceByWriter.keys(), ...sessions.keys()])]
101
+ .map((hex) => {
102
+ const session = sessions.get(hex);
103
+ const device = deviceByWriter.get(hex);
104
+ return { session, device, writerKey: session ? session.core.key : b4a.from(hex, 'hex') };
105
+ })
106
+ .filter((writer) => !b4a.equals(writer.writerKey, localKey));
107
+ }
108
+ async function writerProgress(mesh, snap, { writerKey, session, device }) {
109
+ const activity = await heldOr(snap.get(WRITER_ACTIVITY, { writerKey }), {
110
+ unheld: null
111
+ });
112
+ const removed = async () => heldOr(removedByRows(snap, writerKey), { unheld: false });
113
+ if (!device && (activity?.revoked || (await removed()))) {
114
+ return null;
115
+ }
116
+ const length = session?.core.length ?? 0;
117
+ const position = mesh.appliedLength(writerKey);
118
+ const applied = position === undefined
119
+ ? await landedLength(session, activity, length)
120
+ : await arrivedLength(session, position, length);
121
+ const known = Math.max(length, applied);
122
+ return { writerKey, deviceId: device?.id, applied, known };
123
+ }
124
+ async function heldOr(read, { unheld }) {
125
+ try {
126
+ return await read;
127
+ }
128
+ catch (err) {
129
+ if (isMissingBlock(err)) {
130
+ return unheld;
131
+ }
132
+ throw err;
133
+ }
134
+ }
135
+ async function removedByRows(snap, writerKey) {
136
+ let seen = false;
137
+ for await (const row of devicesByWriterKey(snap, writerKey)) {
138
+ if (!row.revoked) {
139
+ return false;
140
+ }
141
+ seen = true;
142
+ }
143
+ return seen;
144
+ }
145
+ async function arrivedLength(session, position, known) {
146
+ if (session && position < known && (await session.core.has(position, known))) {
147
+ return known;
148
+ }
149
+ return Math.max(position, session?.core.contiguousLength ?? 0);
150
+ }
151
+ async function landedLength(session, activity, known) {
152
+ if (session && known > 0 && (await session.core.has(known - 1, known))) {
153
+ return known;
154
+ }
155
+ return Math.max(activity?.length ?? 0, session?.core.contiguousLength ?? 0);
156
+ }
107
157
  function statusState(writerCount, syncing) {
108
158
  if (writerCount === 0)
109
159
  return 'idle';
110
160
  return syncing ? 'syncing' : 'synced';
111
161
  }
162
+ function holdsBlocks(writer) {
163
+ return 'has' in writer.core;
164
+ }
112
165
  function openWriterSessions(mesh) {
113
166
  const sessions = new Map();
114
167
  for (const writer of mesh.autobee.activeWriters) {
115
- sessions.set(b4a.toHex(writer.core.key), writer);
168
+ if (holdsBlocks(writer)) {
169
+ sessions.set(b4a.toHex(writer.core.key), writer);
170
+ }
116
171
  }
117
172
  return sessions;
118
173
  }
119
- async function writerDeviceMap(mesh) {
174
+ async function writerDeviceMap(snap) {
120
175
  const map = new Map();
121
- const snap = mesh.view.snapshot();
122
176
  try {
123
177
  for await (const device of liveDevices(snap)) {
124
178
  if (device.writerKey) {
@@ -126,8 +180,10 @@ async function writerDeviceMap(mesh) {
126
180
  }
127
181
  }
128
182
  }
129
- finally {
130
- await snap.close();
183
+ catch (err) {
184
+ if (!isMissingBlock(err)) {
185
+ throw err;
186
+ }
131
187
  }
132
188
  return map;
133
189
  }
@@ -21,16 +21,10 @@ export async function recordToolActivity(store, input) {
21
21
  return { id: activity.id };
22
22
  }
23
23
  export async function listToolActivity(store, agentId, bounds = {}) {
24
- const snap = store.view.snapshot();
25
- try {
26
- return await snap
27
- .find(TOOL_ACTIVITY_BY_AGENT, {
28
- ...keyRange({ agentId }, 'seq', bounds),
29
- reverse: true
30
- })
31
- .toArray();
32
- }
33
- finally {
34
- await snap.close();
35
- }
24
+ return store.view
25
+ .find(TOOL_ACTIVITY_BY_AGENT, {
26
+ ...keyRange({ agentId }, 'seq', bounds),
27
+ reverse: true
28
+ })
29
+ .toArray();
36
30
  }
@@ -73,20 +73,14 @@ export async function getToolConfigs(store, agentId, toolName) {
73
73
  return store.view.get(TOOL_CONFIGS, { agentId, toolName });
74
74
  }
75
75
  export async function listToolConfigs(store, agentId) {
76
- const snap = store.view.snapshot();
77
- try {
78
- const rows = [];
79
- for await (const row of snap.find(ACTIVE_TOOL_CONFIGS_BY_AGENT, {
80
- gte: { agentId },
81
- lte: { agentId }
82
- })) {
83
- rows.push(row);
84
- }
85
- return rows;
86
- }
87
- finally {
88
- await snap.close();
76
+ const rows = [];
77
+ for await (const row of store.view.find(ACTIVE_TOOL_CONFIGS_BY_AGENT, {
78
+ gte: { agentId },
79
+ lte: { agentId }
80
+ })) {
81
+ rows.push(row);
89
82
  }
83
+ return rows;
90
84
  }
91
85
  export async function setToolConfigs(store, input, agents = store) {
92
86
  const agent = await mustGetAgent(agents, input.agentId);
@@ -126,21 +120,15 @@ export async function deleteToolConfigs(store, { agentId, toolName }, agents = s
126
120
  return settings;
127
121
  }
128
122
  export async function softDeleteToolConfigsForAgent(store, agentId) {
129
- const snap = store.view.snapshot();
130
- try {
131
- for await (const row of snap.find(ACTIVE_TOOL_CONFIGS_BY_AGENT, {
132
- gte: { agentId },
133
- lte: { agentId }
134
- })) {
135
- if (store.closing)
136
- return;
137
- const now = Date.now();
138
- await store.dispatch('put-tool-configs', {
139
- settings: { ...row, deletedAt: now, updatedAt: Math.max(now, row.updatedAt + 1) }
140
- });
141
- }
142
- }
143
- finally {
144
- await snap.close();
123
+ for await (const row of store.view.find(ACTIVE_TOOL_CONFIGS_BY_AGENT, {
124
+ gte: { agentId },
125
+ lte: { agentId }
126
+ })) {
127
+ if (store.closing)
128
+ return;
129
+ const now = Date.now();
130
+ await store.dispatch('put-tool-configs', {
131
+ settings: { ...row, deletedAt: now, updatedAt: Math.max(now, row.updatedAt + 1) }
132
+ });
145
133
  }
146
134
  }
@@ -0,0 +1,19 @@
1
+ import type HyperDB from 'hyperdb';
2
+ import type { AssistantUpgradedWorkspace } from '../../spec/hyperschema/types.d.ts';
3
+ import type { ConfigEntry } from './local/index.ts';
4
+ import type { Mesh } from './mesh.ts';
5
+ import { type GroupRecord } from './workspace-upgrade.ts';
6
+ type GroupRecords = ReadonlyMap<string, GroupRecord>;
7
+ export interface ConvertedWorkspace {
8
+ readonly records: GroupRecords;
9
+ readonly view: HyperDB;
10
+ readonly ours: Buffer | null;
11
+ }
12
+ export declare function groupRecordsByWorkspace(entries: ReadonlyArray<ConfigEntry>): Map<string, Map<string, GroupRecord>>;
13
+ export declare function convertedHere(records: GroupRecords): boolean;
14
+ export declare function recordsGroup(records: GroupRecords): boolean;
15
+ export declare function upgradedWorkspace(meshId: string, { records, view, ours }: ConvertedWorkspace): Promise<AssistantUpgradedWorkspace | null>;
16
+ export declare function ifClosing<T>(mesh: Pick<Mesh, 'closing'>, err: unknown, fallback: T): T;
17
+ export declare function oldMessages(account: Mesh): () => Promise<boolean>;
18
+ export declare function migratedWorkspace(entries: ReadonlyArray<ConfigEntry>, prefix: string): string | undefined;
19
+ export {};
@@ -0,0 +1,67 @@
1
+ import b4a from 'b4a';
2
+ import { UNGROUPED, listedProject } from './contract/projects.js';
3
+ import { upgradeMembers } from './contract/workspace.js';
4
+ import { isPublicRecord, oldChats } from './public-mesh.js';
5
+ import { UPGRADE_RECORDS, isGroupRecord, liveGroups, projectsByGroup } from './workspace-upgrade.js';
6
+ export function groupRecordsByWorkspace(entries) {
7
+ const workspaces = new Map();
8
+ for (const { key, value } of entries) {
9
+ const [meshId, groupId] = key.slice(UPGRADE_RECORDS.length).split(/\/(.*)/);
10
+ if (groupId === undefined || !isGroupRecord(value)) {
11
+ continue;
12
+ }
13
+ const records = workspaces.get(meshId) ?? new Map();
14
+ workspaces.set(meshId, records.set(groupId, value));
15
+ }
16
+ return workspaces;
17
+ }
18
+ export function convertedHere(records) {
19
+ return [...records].some(([groupId, record]) => typeof record.key === 'string' || (!!record.done && groupId !== UNGROUPED));
20
+ }
21
+ export function recordsGroup(records) {
22
+ return [...records.keys()].some((groupId) => groupId !== UNGROUPED);
23
+ }
24
+ export async function upgradedWorkspace(meshId, { records, view, ours }) {
25
+ if (!ours || !(await upgradeMembers(view)).some((id) => b4a.equals(id, ours))) {
26
+ return null;
27
+ }
28
+ const rows = await projectsByGroup(view);
29
+ const loose = rows.get(UNGROUPED);
30
+ const listed = loose && listedProject(loose, ours) ? loose : null;
31
+ if (!convertedHere(records) && !(records.get(UNGROUPED)?.done && listed)) {
32
+ return null;
33
+ }
34
+ const live = await liveGroups(view);
35
+ const groups = [...live.map((group) => group.id), UNGROUPED];
36
+ return {
37
+ meshId,
38
+ pending: groups.some((groupId) => !readable(records.get(groupId), rows.get(groupId))),
39
+ hasGroups: live.length > 0,
40
+ ungroupedProjectId: listed ? b4a.toHex(listed.id) : undefined
41
+ };
42
+ }
43
+ function readable(record, row) {
44
+ if (record?.done) {
45
+ return true;
46
+ }
47
+ return !!record && !!row && !row.deletedAt && record.key === b4a.toHex(row.id);
48
+ }
49
+ export function ifClosing(mesh, err, fallback) {
50
+ if (mesh.closing) {
51
+ return fallback;
52
+ }
53
+ throw err;
54
+ }
55
+ export function oldMessages(account) {
56
+ let read = null;
57
+ return () => {
58
+ read ??= oldChats(account)
59
+ .catch((err) => ifClosing(account, err, null))
60
+ .then((chats) => !!chats?.messages);
61
+ return read;
62
+ };
63
+ }
64
+ export function migratedWorkspace(entries, prefix) {
65
+ const entry = entries.find(({ value }) => isPublicRecord(value) && value.migrated);
66
+ return entry?.key.slice(prefix.length);
67
+ }
@@ -0,0 +1,4 @@
1
+ import type HyperDB from 'hyperdb';
2
+ import type { Mesh } from './mesh.ts';
3
+ export declare function isMissingBlock(err: unknown): boolean;
4
+ export declare function readView<T>(mesh: Mesh, read: (snap: HyperDB) => Promise<T>): Promise<T>;
@@ -0,0 +1,23 @@
1
+ import { CoreError } from '../errors.js';
2
+ import { isRecord } from '../guards.js';
3
+ const MISSING_BLOCK = new Set(['REQUEST_TIMEOUT', 'BLOCK_NOT_AVAILABLE', 'REQUEST_CANCELLED']);
4
+ export function isMissingBlock(err) {
5
+ return isRecord(err) && typeof err.code === 'string' && MISSING_BLOCK.has(err.code);
6
+ }
7
+ export async function readView(mesh, read) {
8
+ const snap = mesh.read();
9
+ try {
10
+ return await read(snap);
11
+ }
12
+ catch (err) {
13
+ if (isMissingBlock(err)) {
14
+ throw new CoreError('MESH_VIEW_UNAVAILABLE', [], {
15
+ cause: err instanceof Error ? err : undefined
16
+ });
17
+ }
18
+ throw err;
19
+ }
20
+ finally {
21
+ await snap.close();
22
+ }
23
+ }
@@ -2,14 +2,21 @@ import type HyperDB from 'hyperdb';
2
2
  import type { QvacChatGroup, QvacProject } from '../../spec/mesh/hyperschema/types.d.ts';
3
3
  export declare const GROUPS_PENDING = "groups";
4
4
  export declare const FOLD_PENDING = "agents";
5
+ export declare const UPGRADE_RECORDS = "workspace-upgrade/";
5
6
  export declare function upgradeCheckedKey(meshKey: Buffer): string;
6
7
  export declare function agentsFoldKey(workspaceKey: Buffer): string;
7
8
  export declare function groupRecordKey(workspaceKey: Buffer, groupId: string): string;
9
+ export declare function groupRecordsOf(workspaceKey: Buffer): string;
10
+ export declare function historyEditsOf(workspaceKey: Buffer): string;
11
+ export declare function editedChats(prefix: string, keys: ReadonlyArray<string>): ReadonlySet<string> | null;
12
+ export declare function touches(bucket: Bucket, edited: ReadonlySet<string> | null): boolean;
8
13
  export interface GroupRecord {
9
14
  readonly key?: string;
10
15
  readonly copied?: boolean;
11
16
  readonly listedMembers?: ReadonlyArray<string>;
17
+ readonly listedSettled?: boolean;
12
18
  readonly done?: boolean;
19
+ readonly into?: string;
13
20
  }
14
21
  export declare function isGroupRecord(value: unknown): value is GroupRecord;
15
22
  export interface Bucket {