@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
@@ -0,0 +1,48 @@
1
+ export function latestFrame(push) {
2
+ let latest = null;
3
+ let ended = false;
4
+ let pending = null;
5
+ const settle = () => {
6
+ const callback = pending;
7
+ pending = null;
8
+ callback?.(null);
9
+ };
10
+ return {
11
+ offer(frame) {
12
+ if (pending) {
13
+ push(frame);
14
+ settle();
15
+ }
16
+ else {
17
+ latest = { frame };
18
+ }
19
+ },
20
+ end() {
21
+ ended = true;
22
+ if (pending) {
23
+ push(null);
24
+ settle();
25
+ }
26
+ },
27
+ read(callback) {
28
+ if (latest) {
29
+ const { frame } = latest;
30
+ latest = null;
31
+ push(frame);
32
+ return callback(null);
33
+ }
34
+ if (ended) {
35
+ push(null);
36
+ return callback(null);
37
+ }
38
+ pending = callback;
39
+ },
40
+ clear() {
41
+ latest = null;
42
+ },
43
+ release() {
44
+ latest = null;
45
+ settle();
46
+ }
47
+ };
48
+ }
@@ -95,6 +95,7 @@ export const MESH_SCOPED = new Set([
95
95
  'membersWatch',
96
96
  'removeMember',
97
97
  'listApprovalRules',
98
+ 'removeApprovalRule',
98
99
  'ragSearch',
99
100
  'runAgent',
100
101
  'sendMessage',
@@ -22,6 +22,8 @@ export type DownloadFrame = ModelProgressFrame;
22
22
  export type DownloadEventStream = AsyncIterable<ModelProgressFrame>;
23
23
  export interface CacheWaitOptions {
24
24
  readonly onHold?: () => void;
25
+ readonly onProgress?: (percentage: number) => void;
26
+ readonly onStall?: () => void;
25
27
  }
26
28
  interface DownloadHarness {
27
29
  list?(options?: {
@@ -171,7 +173,7 @@ export declare class ModelDownloadReconciler {
171
173
  observeImageProgress(observation: ImageDownloadObservation): void;
172
174
  private _transfer;
173
175
  private _completeTransfer;
174
- waitUntilCached(model: string, signal: AbortSignal, { onHold }?: CacheWaitOptions): Promise<void>;
176
+ waitUntilCached(model: string, signal: AbortSignal, { onHold, onProgress, onStall }?: CacheWaitOptions): Promise<void>;
175
177
  retry(name: string): Promise<void>;
176
178
  release<T>(name: string, removeFiles: () => Promise<T>): Promise<T>;
177
179
  close(): Promise<void>;
@@ -187,6 +189,9 @@ export declare class ModelDownloadReconciler {
187
189
  private _step;
188
190
  private _writeWaiting;
189
191
  private _announceHolds;
192
+ private _announceProgress;
193
+ private _announceStall;
194
+ private _writeFailure;
190
195
  private _flushObservedProgress;
191
196
  private _shouldPreempt;
192
197
  private _waiting;
@@ -182,7 +182,7 @@ export class ModelDownloadReconciler {
182
182
  this._pendingCompletions.delete(name);
183
183
  this._completedTransfers.add(name);
184
184
  }
185
- async waitUntilCached(model, signal, { onHold } = {}) {
185
+ async waitUntilCached(model, signal, { onHold, onProgress, onStall } = {}) {
186
186
  if (this._closed ||
187
187
  !this._harness.list ||
188
188
  !this._harness.download ||
@@ -207,7 +207,15 @@ export class ModelDownloadReconciler {
207
207
  this._removeDemandWaiter(model, waiter);
208
208
  resolve();
209
209
  };
210
- const waiter = { signal, abort, reject, resolve, onHold: onHold ?? null };
210
+ const waiter = {
211
+ signal,
212
+ abort,
213
+ reject,
214
+ resolve,
215
+ onHold: onHold ?? null,
216
+ onProgress: onProgress ?? null,
217
+ onStall: onStall ?? null
218
+ };
211
219
  const demand = this._demands.get(model) ?? {
212
220
  addon,
213
221
  failures: 0,
@@ -455,6 +463,7 @@ export class ModelDownloadReconciler {
455
463
  for (const target of targets) {
456
464
  if (target.paused) {
457
465
  await this._status.write(target.name, 'paused', { bytesPerSecond: 0 });
466
+ this._announceStall(target.name);
458
467
  }
459
468
  else if (target.held) {
460
469
  await this._status.write(target.name, 'held', { total: target.total, bytesPerSecond: 0 });
@@ -472,12 +481,24 @@ export class ModelDownloadReconciler {
472
481
  if (!target.held)
473
482
  continue;
474
483
  for (const waiter of this._demands.get(target.name)?.waiters ?? []) {
475
- const onHold = waiter.onHold;
476
- waiter.onHold = null;
477
- onHold?.();
484
+ waiter.onHold?.();
478
485
  }
479
486
  }
480
487
  }
488
+ _announceProgress(model, percentage) {
489
+ for (const waiter of this._demands.get(model)?.waiters ?? []) {
490
+ waiter.onProgress?.(percentage);
491
+ }
492
+ }
493
+ _announceStall(model) {
494
+ for (const waiter of this._demands.get(model)?.waiters ?? []) {
495
+ waiter.onStall?.();
496
+ }
497
+ }
498
+ async _writeFailure(model, error) {
499
+ await this._status.write(model, 'error', { error, bytesPerSecond: 0 });
500
+ this._announceStall(model);
501
+ }
481
502
  async _flushObservedProgress() {
482
503
  for (const [model, progress] of this._observedProgress) {
483
504
  await this._transfer(model);
@@ -513,6 +534,7 @@ export class ModelDownloadReconciler {
513
534
  total,
514
535
  bytesPerSecond: speed.sample({ downloaded, total, aggregate: true }, progress.at, model)
515
536
  });
537
+ this._announceProgress(model, progress.percentage);
516
538
  }
517
539
  }
518
540
  _shouldPreempt(preferred, preemptLlm) {
@@ -828,6 +850,7 @@ export class ModelDownloadReconciler {
828
850
  }
829
851
  const bytes = transfer ?? progressBytes(event);
830
852
  const percentage = transfer?.percentage ?? 0;
853
+ this._announceProgress(model, percentage);
831
854
  if (shouldFlush(localFlush, { at, percentage })) {
832
855
  const written = await this._status.write(model, 'downloading', {
833
856
  percentage,
@@ -859,6 +882,7 @@ export class ModelDownloadReconciler {
859
882
  })) {
860
883
  localFlush = { at: initialAt, percentage: initialPercentage };
861
884
  }
885
+ this._announceProgress(model, initialPercentage);
862
886
  if (!this._harness.download) {
863
887
  throw new Error('the harness cannot download models');
864
888
  }
@@ -887,18 +911,12 @@ export class ModelDownloadReconciler {
887
911
  else if (event.type === 'error' && current()) {
888
912
  retry = true;
889
913
  failureMessage = typeof event.message === 'string' ? event.message : 'download failed';
890
- await this._status.write(model, 'error', {
891
- error: failureMessage,
892
- bytesPerSecond: 0
893
- });
914
+ await this._writeFailure(model, failureMessage);
894
915
  }
895
916
  else if (event.type === 'aborted' && current() && !controller.signal.aborted) {
896
917
  retry = true;
897
918
  failureMessage = 'download interrupted';
898
- await this._status.write(model, 'error', {
899
- error: failureMessage,
900
- bytesPerSecond: 0
901
- });
919
+ await this._writeFailure(model, failureMessage);
902
920
  }
903
921
  }
904
922
  if (current() && !controller.signal.aborted && !completed && !retry) {
@@ -909,10 +927,7 @@ export class ModelDownloadReconciler {
909
927
  if (!this._closed && current()) {
910
928
  retry = true;
911
929
  failureMessage = errorMessage(err);
912
- await this._status.write(model, 'error', {
913
- error: failureMessage,
914
- bytesPerSecond: 0
915
- });
930
+ await this._writeFailure(model, failureMessage);
916
931
  }
917
932
  }
918
933
  finally {
@@ -1,6 +1,6 @@
1
1
  import { scopedKey } from './keys.js';
2
2
  export const REFRESH_SKEW_MS = 60_000;
3
- const inFlightRefresh = new Map();
3
+ const inFlightRefresh = new WeakMap();
4
4
  export async function resolveOAuthCredential(opts) {
5
5
  const { engine, agentId, credentialKey, stateKey, refresh, requireClientSecret = false } = opts;
6
6
  const { value: token } = await engine.configGet({ key: scopedKey(agentId, credentialKey) });
@@ -28,9 +28,17 @@ export async function resolveOAuthCredential(opts) {
28
28
  if (typeof expiresAt !== 'number' || expiresAt - REFRESH_SKEW_MS > Date.now()) {
29
29
  return token;
30
30
  }
31
- const refreshKey = `${credentialKey}:${agentId ?? ''}`;
31
+ const refreshKey = JSON.stringify([
32
+ scopedKey(agentId, credentialKey),
33
+ scopedKey(agentId, stateKey)
34
+ ]);
32
35
  try {
33
- let pending = inFlightRefresh.get(refreshKey);
36
+ let engineRefreshes = inFlightRefresh.get(engine);
37
+ if (!engineRefreshes) {
38
+ engineRefreshes = new Map();
39
+ inFlightRefresh.set(engine, engineRefreshes);
40
+ }
41
+ let pending = engineRefreshes.get(refreshKey);
34
42
  if (!pending) {
35
43
  pending = (async () => {
36
44
  const refreshed = await refresh({
@@ -52,8 +60,8 @@ export async function resolveOAuthCredential(opts) {
52
60
  })
53
61
  });
54
62
  return refreshed.accessToken;
55
- })().finally(() => inFlightRefresh.delete(refreshKey));
56
- inFlightRefresh.set(refreshKey, pending);
63
+ })().finally(() => engineRefreshes?.delete(refreshKey));
64
+ engineRefreshes.set(refreshKey, pending);
57
65
  }
58
66
  return await pending;
59
67
  }
@@ -1,6 +1,7 @@
1
1
  import { Readable } from 'streamx';
2
2
  import { frameReadable } from './frame-readable.js';
3
3
  import { asError } from './guards.js';
4
+ import { latestFrame } from './latest-frame.js';
4
5
  const RETRY_MIN_MS = 250;
5
6
  const RETRY_MAX_MS = 5_000;
6
7
  export function reconnectingWatch(open, onDisconnect) {
@@ -8,8 +9,9 @@ export function reconnectingWatch(open, onDisconnect) {
8
9
  let retry = null;
9
10
  let retryMs = RETRY_MIN_MS;
10
11
  let output;
12
+ const frames = latestFrame((frame) => output.push(frame));
11
13
  const schedule = (error) => {
12
- if (output.destroying)
14
+ if (output.destroying || retry)
13
15
  return;
14
16
  onDisconnect(error);
15
17
  retry = setTimeout(() => {
@@ -19,7 +21,7 @@ export function reconnectingWatch(open, onDisconnect) {
19
21
  retryMs = Math.min(retryMs * 2, RETRY_MAX_MS);
20
22
  };
21
23
  const connect = () => {
22
- if (output.destroying)
24
+ if (output.destroying || closeConnection)
23
25
  return;
24
26
  let source;
25
27
  try {
@@ -40,12 +42,13 @@ export function reconnectingWatch(open, onDisconnect) {
40
42
  if (closeConnection !== close)
41
43
  return;
42
44
  closeConnection = null;
45
+ frames.clear();
43
46
  close();
44
47
  schedule(error);
45
48
  };
46
49
  const onData = (frame) => {
47
50
  retryMs = RETRY_MIN_MS;
48
- output.push(frame);
51
+ frames.offer(frame);
49
52
  };
50
53
  const onError = (error) => disconnect(error);
51
54
  const onEnd = () => disconnect(new Error('Watch stream ended unexpectedly'));
@@ -55,22 +58,27 @@ export function reconnectingWatch(open, onDisconnect) {
55
58
  source.on('end', onEnd);
56
59
  source.on('close', onEnd);
57
60
  };
61
+ const stop = () => {
62
+ if (retry) {
63
+ clearTimeout(retry);
64
+ }
65
+ retry = null;
66
+ frames.release();
67
+ const close = closeConnection;
68
+ closeConnection = null;
69
+ close?.();
70
+ };
58
71
  output = new Readable({
59
72
  open(cb) {
60
73
  connect();
61
74
  cb(null);
62
75
  },
63
76
  read(cb) {
64
- cb(null);
77
+ frames.read(cb);
65
78
  },
79
+ predestroy: stop,
66
80
  destroy(cb) {
67
- if (retry) {
68
- clearTimeout(retry);
69
- }
70
- retry = null;
71
- const close = closeConnection;
72
- closeConnection = null;
73
- close?.();
81
+ stop();
74
82
  cb(null);
75
83
  }
76
84
  });
@@ -1,6 +1,7 @@
1
1
  export declare const RUN_ABORT_REASONS: {
2
2
  readonly suspended: 'suspended';
3
3
  readonly ownershipLost: 'ownership-lost';
4
+ readonly agentRemoved: 'agent-removed';
4
5
  readonly runSettled: 'run-settled';
5
6
  readonly runnerDied: 'runner-died';
6
7
  readonly runnerClosed: 'runner-closed';
@@ -13,6 +14,7 @@ export declare const RUN_FAILURE_REASONS: {
13
14
  readonly runtimeEndedTurn: 'the model runtime ended this turn';
14
15
  readonly capabilityUnavailable: 'this device cannot run a capability without a model';
15
16
  readonly stalled: 'the agent stopped responding';
17
+ readonly agentRemoved: 'the agent was removed';
16
18
  readonly videoNeedsVision: 'this model cannot see video: switch to a model that reads images to ask about one';
17
19
  };
18
20
  export type AbortOutcome = {
@@ -1,6 +1,7 @@
1
1
  export const RUN_ABORT_REASONS = {
2
2
  suspended: 'suspended',
3
3
  ownershipLost: 'ownership-lost',
4
+ agentRemoved: 'agent-removed',
4
5
  runSettled: 'run-settled',
5
6
  runnerDied: 'runner-died',
6
7
  runnerClosed: 'runner-closed',
@@ -12,6 +13,7 @@ export const RUN_FAILURE_REASONS = {
12
13
  runtimeEndedTurn: 'the model runtime ended this turn',
13
14
  capabilityUnavailable: 'this device cannot run a capability without a model',
14
15
  stalled: 'the agent stopped responding',
16
+ agentRemoved: 'the agent was removed',
15
17
  videoNeedsVision: 'this model cannot see video: switch to a model that reads images to ask about one'
16
18
  };
17
19
  export function knownAbortOutcome(reason) {
@@ -26,6 +28,9 @@ export function knownAbortOutcome(reason) {
26
28
  if (reason === RUN_ABORT_REASONS.runnerDied) {
27
29
  return { kind: 'failed', reason: RUN_FAILURE_REASONS.harnessDied };
28
30
  }
31
+ if (reason === RUN_ABORT_REASONS.agentRemoved) {
32
+ return { kind: 'failed', reason: RUN_FAILURE_REASONS.agentRemoved };
33
+ }
29
34
  if (reason === RUN_ABORT_REASONS.stalled) {
30
35
  return { kind: 'failed', reason: RUN_FAILURE_REASONS.stalled };
31
36
  }
@@ -4,10 +4,18 @@ export interface ReplayChat {
4
4
  readonly holder: string;
5
5
  readonly chatId: string;
6
6
  }
7
+ export interface Forgotten {
8
+ readonly holder?: string | undefined;
9
+ readonly reason?: string | undefined;
10
+ }
7
11
  export interface SentRun {
8
12
  readonly messages: ChatMessage[];
9
13
  readonly asked?: string | undefined;
10
14
  }
15
+ export interface ReplayData {
16
+ readonly sentRuns: Map<string, ChatMessage[]>;
17
+ readonly askedRuns: Map<string, string>;
18
+ }
11
19
  export declare class RunReplayStore {
12
20
  private readonly _dir;
13
21
  constructor(dir: string);
@@ -15,15 +23,19 @@ export declare class RunReplayStore {
15
23
  start(): Promise<void>;
16
24
  load(chat: ReplayChat): Promise<Map<string, ChatMessage[]>>;
17
25
  loadAsked(chat: ReplayChat): Promise<Map<string, string>>;
26
+ loadReplay(chat: ReplayChat): Promise<ReplayData>;
18
27
  save({ holder, chatId }: ReplayChat, runId: string, { messages, asked }: SentRun): Promise<void>;
28
+ rebuild({ holder, chatId }: ReplayChat): Promise<string | null>;
29
+ consume({ holder, chatId }: ReplayChat): Promise<void>;
19
30
  private _records;
20
- forget(chatId: string, holder?: string): Promise<void>;
31
+ forget(chatId: string, { holder, reason }?: Forgotten): Promise<void>;
21
32
  clear(holder: string): Promise<void>;
22
33
  private _chatDir;
23
34
  private _holders;
24
35
  }
25
36
  export interface SentRunTracker {
26
37
  readonly used: boolean;
38
+ readonly onKey: boolean;
27
39
  observe(call: Record<string, unknown>): void;
28
40
  turns(inputLength: number): ChatMessage[] | null;
29
41
  }
@@ -2,46 +2,56 @@ import fs from 'fs';
2
2
  import { join } from 'path';
3
3
  import { isRecord, safeName } from './guards.js';
4
4
  export const LOCAL_HOLDER = 'local';
5
+ const REBUILD_FILE = 'rebuild';
5
6
  export class RunReplayStore {
6
7
  _dir;
7
8
  constructor(dir) {
8
9
  this._dir = dir;
9
10
  }
10
- async started() {
11
- try {
12
- await fs.promises.stat(this._dir);
13
- return true;
14
- }
15
- catch {
16
- return false;
17
- }
11
+ started() {
12
+ return exists(this._dir);
18
13
  }
19
14
  async start() {
20
15
  await fs.promises.mkdir(this._dir, { recursive: true });
21
16
  }
22
17
  async load(chat) {
23
- const runs = new Map();
24
- for (const record of await this._records(chat)) {
25
- if (Array.isArray(record.messages)) {
26
- runs.set(record.runId, record.messages.filter(isChatMessage));
27
- }
28
- }
29
- return runs;
18
+ return (await this.loadReplay(chat)).sentRuns;
30
19
  }
31
20
  async loadAsked(chat) {
32
- const asked = new Map();
21
+ return (await this.loadReplay(chat)).askedRuns;
22
+ }
23
+ async loadReplay(chat) {
24
+ const sentRuns = new Map();
25
+ const askedRuns = new Map();
33
26
  for (const record of await this._records(chat)) {
27
+ if (Array.isArray(record.messages)) {
28
+ sentRuns.set(record.runId, record.messages.filter(isChatMessage));
29
+ }
34
30
  if (typeof record.asked === 'string') {
35
- asked.set(record.runId, record.asked);
31
+ askedRuns.set(record.runId, record.asked);
36
32
  }
37
33
  }
38
- return asked;
34
+ return { sentRuns, askedRuns };
39
35
  }
40
36
  async save({ holder, chatId }, runId, { messages, asked }) {
41
37
  const chatDir = this._chatDir(holder, chatId);
42
38
  await fs.promises.mkdir(chatDir, { recursive: true });
43
39
  await fs.promises.writeFile(join(chatDir, `${safeName(runId)}.json`), JSON.stringify({ runId, messages, asked }));
44
40
  }
41
+ rebuild({ holder, chatId }) {
42
+ return fs.promises
43
+ .readFile(join(this._chatDir(holder, chatId), REBUILD_FILE), 'utf8')
44
+ .catch((err) => {
45
+ if (isRecord(err) && err.code === 'ENOENT')
46
+ return null;
47
+ throw err;
48
+ });
49
+ }
50
+ async consume({ holder, chatId }) {
51
+ const chatDir = this._chatDir(holder, chatId);
52
+ await fs.promises.mkdir(chatDir, { recursive: true });
53
+ await fs.promises.rm(join(chatDir, REBUILD_FILE), { force: true });
54
+ }
45
55
  async _records({ holder, chatId }) {
46
56
  const chatDir = this._chatDir(holder, chatId);
47
57
  let names;
@@ -62,10 +72,16 @@ export class RunReplayStore {
62
72
  }
63
73
  return records;
64
74
  }
65
- async forget(chatId, holder) {
75
+ async forget(chatId, { holder, reason } = {}) {
66
76
  const holders = holder === undefined ? await this._holders() : [holder];
67
77
  for (const each of holders) {
68
- await fs.promises.rm(this._chatDir(each, chatId), { recursive: true, force: true });
78
+ const chatDir = this._chatDir(each, chatId);
79
+ const marks = reason !== undefined && (await exists(chatDir));
80
+ const kept = marks ? await this.rebuild({ holder: each, chatId }).catch(() => null) : null;
81
+ await fs.promises.rm(chatDir, { recursive: true, force: true });
82
+ if (marks) {
83
+ await mark(chatDir, kept ?? reason);
84
+ }
69
85
  }
70
86
  }
71
87
  async clear(holder) {
@@ -85,13 +101,18 @@ export class RunReplayStore {
85
101
  }
86
102
  export function trackSentRun(cacheKey) {
87
103
  let used = false;
104
+ let onKey = false;
88
105
  let last = null;
89
106
  return {
90
107
  get used() {
91
108
  return used;
92
109
  },
110
+ get onKey() {
111
+ return onKey;
112
+ },
93
113
  observe(call) {
94
- if (cacheKey && call.cacheKey === cacheKey) {
114
+ onKey = !!cacheKey && call.cacheKey === cacheKey;
115
+ if (onKey) {
95
116
  used = true;
96
117
  last = call.droppedTurns ? null : call.messages;
97
118
  }
@@ -114,8 +135,24 @@ export function appendedTurns(sent, inputLength) {
114
135
  .map((turn) => (isRecord(turn) ? { role: turn.role, content: turn.content } : null));
115
136
  return turns.every(isChatMessage) ? turns : null;
116
137
  }
138
+ function mark(chatDir, reason) {
139
+ return fs.promises
140
+ .mkdir(chatDir, { recursive: true })
141
+ .then(() => fs.promises.writeFile(join(chatDir, REBUILD_FILE), reason))
142
+ .catch(noop);
143
+ }
144
+ async function exists(path) {
145
+ try {
146
+ await fs.promises.stat(path);
147
+ return true;
148
+ }
149
+ catch {
150
+ return false;
151
+ }
152
+ }
117
153
  export function isChatMessage(value) {
118
154
  return (isRecord(value) &&
119
155
  (value.role === 'user' || value.role === 'assistant' || value.role === 'tool') &&
120
156
  typeof value.content === 'string');
121
157
  }
158
+ function noop() { }