@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
@@ -1,5 +1,6 @@
1
1
  import ReadyResource from 'ready-resource';
2
2
  import { reconnectingWatch } from './reconnecting-watch.js';
3
+ import { readConfigAnswer, readConfigFrames } from './api/config.js';
3
4
  import { openLocalEngineSpec } from './spec-loader.js';
4
5
  import { settleOnClose } from '@qvac/harness/rpc';
5
6
  export class Client extends ReadyResource {
@@ -28,7 +29,15 @@ export class Client extends ReadyResource {
28
29
  const modelDownloadsWatch = this._reconnecting(capabilities.modelDownloadsWatch, 'model-download');
29
30
  const modelCatalogWatch = this._reconnecting(capabilities.modelCatalogWatch, 'model-catalog');
30
31
  const modelsWatch = this._reconnecting(capabilities.modelsWatch, 'models');
31
- Object.assign(this, capabilities, { modelCatalogWatch, modelDownloadsWatch, modelsWatch });
32
+ const config = {
33
+ configGet: async (input) => readConfigAnswer(await capabilities.configGet(input)),
34
+ configWatch: (input) => readConfigFrames(capabilities.configWatch(input))
35
+ };
36
+ Object.assign(this, capabilities, config, {
37
+ modelCatalogWatch,
38
+ modelDownloadsWatch,
39
+ modelsWatch
40
+ });
32
41
  this._log?.info('lifecycle', 'client open');
33
42
  }
34
43
  _reconnecting(open, scope) {
@@ -14,7 +14,9 @@ export interface CompactionBoundary {
14
14
  throughId: string;
15
15
  foldedTurns: number;
16
16
  }
17
- export declare function compactionBoundary(chunks: ReadonlyArray<AssistantChunk>, keepTurns?: number): CompactionBoundary | null;
17
+ export declare function compactionBoundary(chunks: ReadonlyArray<AssistantChunk>, keepTurns?: number, beforeSeq?: number): CompactionBoundary | null;
18
+ export declare function foldPosition(chunks: ReadonlyArray<AssistantChunk>): (chunk: AssistantChunk) => number;
19
+ export declare function foldsThrough(chunks: ReadonlyArray<AssistantChunk>, through: AssistantChunk): (chunk: AssistantChunk) => boolean;
18
20
  export declare function lastCompactedThroughId(chunks: ReadonlyArray<AssistantChunk>): string | null;
19
21
  export interface FoldedChunks {
20
22
  chunks: ReadonlyArray<AssistantChunk>;
@@ -35,10 +35,10 @@ export function parseCompactionSummary(text) {
35
35
  const summary = text.trim();
36
36
  return summary.length > 0 ? summary : null;
37
37
  }
38
- export function compactionBoundary(chunks, keepTurns = COMPACTION_KEEP_TURNS) {
38
+ export function compactionBoundary(chunks, keepTurns = COMPACTION_KEEP_TURNS, beforeSeq = Infinity) {
39
39
  const userTurns = [];
40
40
  for (let index = 0; index < chunks.length; index++) {
41
- if (chunks[index].userMessage) {
41
+ if (chunks[index].userMessage && chunks[index].seq < beforeSeq) {
42
42
  userTurns.push(index);
43
43
  }
44
44
  }
@@ -52,12 +52,25 @@ export function compactionBoundary(chunks, keepTurns = COMPACTION_KEEP_TURNS) {
52
52
  from = index;
53
53
  }
54
54
  }
55
- const boundary = chunks[from - 1];
55
+ const boundary = chunks.slice(0, from).findLast((chunk) => chunk.type === 'run-request') ?? chunks[from - 1];
56
56
  if (!boundary) {
57
57
  return null;
58
58
  }
59
59
  return { throughId: boundary.id, foldedTurns: userTurns.length - keepTurns };
60
60
  }
61
+ export function foldPosition(chunks) {
62
+ const requested = new Map();
63
+ for (const chunk of chunks) {
64
+ if (chunk.type === 'run-request' && chunk.runId && !requested.has(chunk.runId)) {
65
+ requested.set(chunk.runId, chunk.seq);
66
+ }
67
+ }
68
+ return (chunk) => (chunk.runId ? (requested.get(chunk.runId) ?? chunk.seq) : chunk.seq);
69
+ }
70
+ export function foldsThrough(chunks, through) {
71
+ const at = foldPosition(chunks);
72
+ return (chunk) => at(chunk) <= through.seq;
73
+ }
61
74
  function newestCompaction(chunks) {
62
75
  let newest = null;
63
76
  for (const chunk of chunks) {
@@ -79,8 +92,9 @@ export function foldCompactedChunks(chunks) {
79
92
  if (!through) {
80
93
  return { chunks, summary: null };
81
94
  }
95
+ const folded = foldsThrough(chunks, through);
82
96
  return {
83
- chunks: chunks.filter((chunk) => chunk.seq > through.seq && !chunk.compaction),
97
+ chunks: chunks.filter((chunk) => !folded(chunk) && !chunk.compaction),
84
98
  summary: compaction.summary
85
99
  };
86
100
  }
@@ -9,5 +9,8 @@ export declare const MODEL_MEMORY_CAP_KEY = "model.memoryCapPercent";
9
9
  export declare const MODEL_MEMORY_CAP_DEFAULT = 85;
10
10
  export declare const MODEL_MEMORY_CAP_MIN = 10;
11
11
  export declare const MODEL_MEMORY_CAP_MAX = 100;
12
+ export declare const KV_CACHE_RETENTION_KEY = "kvCache.retentionDays";
13
+ export declare const KV_CACHE_RETENTION_DAYS_DEFAULT = 14;
14
+ export declare const KV_CACHE_RETENTION_NEVER = 0;
12
15
  export declare const FAKE_MODELS_KEY = "debug.fakeModels";
13
16
  export declare const QUACK_MODEL = "quack";
@@ -11,5 +11,8 @@ export const MODEL_MEMORY_CAP_KEY = 'model.memoryCapPercent';
11
11
  export const MODEL_MEMORY_CAP_DEFAULT = 85;
12
12
  export const MODEL_MEMORY_CAP_MIN = 10;
13
13
  export const MODEL_MEMORY_CAP_MAX = 100;
14
+ export const KV_CACHE_RETENTION_KEY = 'kvCache.retentionDays';
15
+ export const KV_CACHE_RETENTION_DAYS_DEFAULT = 14;
16
+ export const KV_CACHE_RETENTION_NEVER = 0;
14
17
  export const FAKE_MODELS_KEY = 'debug.fakeModels';
15
18
  export const QUACK_MODEL = 'quack';
@@ -23,9 +23,15 @@ export async function followAgentCopies(account, into, { deviceId, agentId }) {
23
23
  if (!copy || into.closing)
24
24
  continue;
25
25
  const agent = await putNewer(account, into, { id: copy.id, copy });
26
- if (agent?.deletedAt && !copy.deletedAt) {
26
+ if (!agent?.deletedAt || copy.deletedAt) {
27
+ continue;
28
+ }
29
+ if (agent.chatRetention === 'delete') {
27
30
  await softDeleteChatsForAgent(into, deviceId, agent.id);
28
31
  }
32
+ else if (deviceId) {
33
+ await cancelRunsForAgent(into, deviceId, agent.id);
34
+ }
29
35
  }
30
36
  }
31
37
  export async function keepCopyChats(into, agent, deviceId) {
@@ -212,6 +212,7 @@ export async function deleteAgent(store, deviceId, { id, keepChats, keepCopies }
212
212
  const updated = {
213
213
  ...current,
214
214
  deletedAt,
215
+ chatRetention: keepChats ? 'retain' : 'delete',
215
216
  updatedAt: Math.max(deletedAt, current.updatedAt + 1)
216
217
  };
217
218
  if (keepChats) {
@@ -4,13 +4,17 @@ export interface ResourceDescriptor {
4
4
  kind: string;
5
5
  label: string;
6
6
  }
7
- type PutApprovalRuleInput = Pick<QvacApprovalRule, 'toolName' | 'resourceKind' | 'resourcePattern' | 'decision' | 'expiresAt' | 'agentId' | 'effect'>;
7
+ type PutApprovalRuleInput = Pick<QvacApprovalRule, 'toolName' | 'resourceKind' | 'resourcePattern' | 'decision' | 'expiresAt' | 'agentId' | 'effect' | 'chatId'> & {
8
+ readonly id?: string;
9
+ };
8
10
  export declare function listApprovalRules(store: Mesh): Promise<QvacApprovalRule[]>;
9
11
  export declare function putApprovalRule(store: Mesh, input: PutApprovalRuleInput): Promise<QvacApprovalRule>;
12
+ export declare function removeApprovalRule(store: Mesh, id: string): Promise<void>;
10
13
  export interface ApprovalRuleQuery {
11
14
  readonly toolName: string;
12
15
  readonly resource?: ResourceDescriptor | null;
13
16
  readonly agentId?: string;
17
+ readonly chatId?: string;
14
18
  readonly memberId?: Buffer | null;
15
19
  }
16
20
  export declare function matchApprovalRule(rules: ReadonlyArray<QvacApprovalRule>, input: ApprovalRuleQuery, now?: number): QvacApprovalRule | null;
@@ -1,6 +1,7 @@
1
1
  import { randomId } from './id.js';
2
2
  import b4a from 'b4a';
3
3
  import { APPROVAL_RULES } from './collections.js';
4
+ import { CoreError } from '../errors.js';
4
5
  export async function listApprovalRules(store) {
5
6
  const snap = store.view.snapshot();
6
7
  try {
@@ -13,10 +14,21 @@ export async function listApprovalRules(store) {
13
14
  }
14
15
  export async function putApprovalRule(store, input) {
15
16
  const now = Date.now();
16
- const rule = { ...input, id: randomId(), createdAt: now, updatedAt: now };
17
+ const rule = { id: randomId(), ...input, createdAt: now, updatedAt: now };
17
18
  await store.dispatch('put-approval-rule', { rule });
18
19
  return rule;
19
20
  }
21
+ export async function removeApprovalRule(store, id) {
22
+ const rule = await store.view.get(APPROVAL_RULES, { id });
23
+ const ours = !!rule?.memberId && !!store.ourMemberId && b4a.equals(rule.memberId, store.ourMemberId);
24
+ if (!rule || !ours) {
25
+ throw new CoreError('APPROVAL_RULE_NOT_FOUND', [id]);
26
+ }
27
+ const now = Date.now();
28
+ await store.dispatch('put-approval-rule', {
29
+ rule: { ...rule, expiresAt: now, updatedAt: Math.max(now, rule.updatedAt + 1) }
30
+ });
31
+ }
20
32
  export function matchApprovalRule(rules, input, now = Date.now()) {
21
33
  for (const rule of rules) {
22
34
  if (!input.memberId || !rule.memberId || !b4a.equals(rule.memberId, input.memberId)) {
@@ -30,6 +42,9 @@ export function matchApprovalRule(rules, input, now = Date.now()) {
30
42
  if (rule.agentId && rule.agentId !== input.agentId) {
31
43
  continue;
32
44
  }
45
+ if (rule.chatId && rule.chatId !== input.chatId) {
46
+ continue;
47
+ }
33
48
  if (!matchesResource(rule, input.resource ?? null)) {
34
49
  continue;
35
50
  }
@@ -1,7 +1,8 @@
1
+ import b4a from 'b4a';
1
2
  import ScopeLock from 'scope-lock';
2
- import { findChunksOfType } from './chunk.js';
3
+ import { findChunksOfTypes } from './chunk.js';
3
4
  import { ACTIVE_CHATS, CHUNKS } from './collections.js';
4
- const TERMINAL_RUN_STATES = new Set(['completed', 'failed', 'canceled']);
5
+ import { TERMINAL_RUN_STATES } from './contract/run-requests.js';
5
6
  const ACTIVITY_CHUNK_TYPES = [
6
7
  'run-request',
7
8
  'run-status',
@@ -39,15 +40,16 @@ class ChatActivityCatalog {
39
40
  try {
40
41
  const head = store.view.db.head();
41
42
  const forwards = store.autobee.stats.fastForwards;
42
- if (forwards !== this._forwards) {
43
- this._head = null;
44
- }
45
- this._forwards = forwards;
43
+ const from = forwards === this._forwards && this._head && head && b4a.equals(this._head.key, head.key)
44
+ ? this._head
45
+ : null;
46
46
  const snap = store.view.snapshot();
47
47
  try {
48
48
  const chats = await snap.find(ACTIVE_CHATS).toArray();
49
49
  const activeChatIds = new Set(chats.map((chat) => chat.id));
50
- await this._refresh(snap, head, activeChatIds);
50
+ await this._refresh(snap, from, activeChatIds);
51
+ this._head = head;
52
+ this._forwards = forwards;
51
53
  const window = chatIds && chatIds.length > 0 ? new Set(chatIds) : null;
52
54
  return [...this._states()]
53
55
  .filter(([id]) => !window || window.has(id))
@@ -62,18 +64,17 @@ class ChatActivityCatalog {
62
64
  this._lock.unlock();
63
65
  }
64
66
  }
65
- async _refresh(snap, head, activeChatIds) {
66
- if (!this._head || !head) {
67
+ async _refresh(snap, from, activeChatIds) {
68
+ if (!from) {
67
69
  await this._rebuild(snap, activeChatIds);
68
70
  }
69
71
  else {
70
72
  this._prune(activeChatIds);
71
73
  await this._loadMissing(snap, activeChatIds);
72
- if (!(await this._applyChanges(snap, this._head, activeChatIds))) {
74
+ if (!(await this._applyChanges(snap, from, activeChatIds))) {
73
75
  await this._rebuild(snap, activeChatIds);
74
76
  }
75
77
  }
76
- this._head = head;
77
78
  }
78
79
  async _rebuild(snap, activeChatIds) {
79
80
  this._chats.clear();
@@ -98,7 +99,7 @@ class ChatActivityCatalog {
98
99
  async _loadChat(snap, chatId) {
99
100
  const activity = { runs: new Map() };
100
101
  this._chats.set(chatId, activity);
101
- for (const chunk of await activityChunks(snap, chatId)) {
102
+ for (const chunk of await findChunksOfTypes(snap, { chatId, types: ACTIVITY_CHUNK_TYPES })) {
102
103
  applyChunk(activity, chunk);
103
104
  }
104
105
  }
@@ -263,10 +264,6 @@ function applyApproval(run, chunk) {
263
264
  run.pendingApprovals.delete(chunk.id);
264
265
  }
265
266
  }
266
- async function activityChunks(snap, chatId) {
267
- const groups = await Promise.all(ACTIVITY_CHUNK_TYPES.map((type) => findChunksOfType(snap, { chatId, type })));
268
- return groups.flat().sort((left, right) => left.seq - right.seq);
269
- }
270
267
  function settleTool(tools, name) {
271
268
  for (let index = tools.length - 1; index >= 0; index -= 1) {
272
269
  if (tools[index]?.name !== name)
@@ -2,7 +2,7 @@ import b4a from 'b4a';
2
2
  import { PENDING_BLOB_REFERENCE, isPendingBlobReference, parseBlobReference } from './files/store/index.js';
3
3
  import { CoreError } from '../errors.js';
4
4
  import { inlineReference } from './files/store/inline.js';
5
- import { TERMINAL_RUN_STATES } from './chat.js';
5
+ import { TERMINAL_RUN_STATES } from './contract/run-requests.js';
6
6
  import { appendChunk, ownedByUs, updateOp } from './chunk.js';
7
7
  import { runRequestOf } from './contract/chunks.js';
8
8
  import { ACTIVE_AGENTS, AGENTS, CHATS, CHAT_GROUPS, CHUNKS, CHUNKS_BY_CHAT, DEVICES, KNOWLEDGE, KNOWLEDGE_ARTIFACTS, KNOWLEDGE_BY_CHAT, PURGED_CHATS, TOOL_ACTIVITY } from './collections.js';
@@ -1,7 +1,8 @@
1
1
  import b4a from 'b4a';
2
2
  import { CoreError } from '../errors.js';
3
3
  import { landingAgentId, mustGetActiveAgent } from './agent.js';
4
- import { TERMINAL_RUN_STATES, enrichChat, listSidebar } from './chat.js';
4
+ import { enrichChat, listSidebar } from './chat.js';
5
+ import { TERMINAL_RUN_STATES } from './contract/run-requests.js';
5
6
  import { chunksOf, copyBlob, removedReference, sentBy, stripped } from './chat-copy.js';
6
7
  import { chatSortKey } from './chat-order.js';
7
8
  import { isPendingBlobReference } from './files/index.js';
@@ -6,6 +6,7 @@ import type { openLocalStore } from './local/index.ts';
6
6
  export declare const CHAT_MOVED = "chat-moved";
7
7
  export declare const SOURCE_GONE = "the chat was deleted before its files arrived";
8
8
  export declare const TARGET_GONE = "the moved chat was deleted before its files arrived";
9
+ export declare const TARGET_REVOKED = "you were removed from the target before its files arrived";
9
10
  export declare const NOT_CARRIED = "a file could not be carried into the target";
10
11
  export declare const OTHERS_RUN = "another member's move left a run in the target that only they can finish";
11
12
  type LocalStore = Awaited<ReturnType<typeof openLocalStore>>;
@@ -35,6 +36,7 @@ export declare class ChatMoves {
35
36
  unwatch(listener: () => void): void;
36
37
  move(request: AssistantMoveChatToMeshRequest): Promise<AssistantChatMove>;
37
38
  status({ meshId, chatId }: AssistantChatId): Promise<AssistantChatMove>;
39
+ private _pairOf;
38
40
  resume(): Promise<void>;
39
41
  close(): Promise<void>;
40
42
  private _begin;
@@ -57,6 +59,8 @@ export declare class ChatMoves {
57
59
  private _fetch;
58
60
  private _owns;
59
61
  private _gone;
62
+ private _revoked;
63
+ private _sourceDropped;
60
64
  private _left;
61
65
  private _giveUp;
62
66
  private _dropPending;
@@ -3,18 +3,21 @@ import { AbortController } from 'bare-abort-controller';
3
3
  import { AbortedError } from '@qvac/harness/race-abort';
4
4
  import { getAgent, landingAgentId } from './agent.js';
5
5
  import { accountPicks } from './shared-agents.js';
6
- import { TERMINAL_RUN_STATES, cancelActiveRuns, deleteChat, getChat, mustGetActiveChat } from './chat.js';
6
+ import { cancelActiveRuns, deleteChat, getChat, mustGetActiveChat } from './chat.js';
7
+ import { TERMINAL_RUN_STATES } from './contract/run-requests.js';
7
8
  import { copyMovedChunks, copyMovedKnowledge, movedBlob, stripped } from './chat-copy.js';
8
9
  import { ownReadThrough } from './chat-read.js';
9
10
  import { appendChunk, getChunk, listChunks, updateOp } from './chunk.js';
10
11
  import { derivedId } from './id.js';
11
12
  import { isPendingBlobReference } from './files/index.js';
12
- import { CoreError } from '../errors.js';
13
+ import { isOwnRevokedRow } from './helpers/index.js';
14
+ import { CoreError, isCoreError } from '../errors.js';
13
15
  import { errorMessage } from '../guards.js';
14
16
  import { CHATS, CHAT_READS, DEVICES, KNOWLEDGE, KNOWLEDGE_BY_CHAT } from './collections.js';
15
17
  export const CHAT_MOVED = 'chat-moved';
16
18
  export const SOURCE_GONE = 'the chat was deleted before its files arrived';
17
19
  export const TARGET_GONE = 'the moved chat was deleted before its files arrived';
20
+ export const TARGET_REVOKED = 'you were removed from the target before its files arrived';
18
21
  export const NOT_CARRIED = 'a file could not be carried into the target';
19
22
  export const OTHERS_RUN = "another member's move left a run in the target that only they can finish";
20
23
  export class ChatMoves {
@@ -65,13 +68,7 @@ export class ChatMoves {
65
68
  if (!row) {
66
69
  throw new CoreError('CHAT_MOVE_NOT_FOUND');
67
70
  }
68
- const settled = row.completedAt || row.failure;
69
- const pair = settled
70
- ? null
71
- : {
72
- source: await this._open(hex(row.meshKey)),
73
- target: await this._open(hex(row.targetMeshKey))
74
- };
71
+ const pair = row.completedAt || row.failure ? null : await this._pairOf(row);
75
72
  const waiting = pair ? filesOf(await waitingFiles(pair, row)) : [];
76
73
  return {
77
74
  meshId,
@@ -86,6 +83,20 @@ export class ChatMoves {
86
83
  failure: row.failure ?? undefined
87
84
  };
88
85
  }
86
+ async _pairOf(row) {
87
+ try {
88
+ return {
89
+ source: await this._open(hex(row.meshKey)),
90
+ target: await this._open(hex(row.targetMeshKey))
91
+ };
92
+ }
93
+ catch (err) {
94
+ if (isCoreError(err, 'MESH_NOT_FOUND')) {
95
+ return null;
96
+ }
97
+ throw err;
98
+ }
99
+ }
89
100
  async resume() {
90
101
  for (const row of await this._local.listChatMoves()) {
91
102
  if (!row.completedAt && !row.failure) {
@@ -359,6 +370,9 @@ export class ChatMoves {
359
370
  return SOURCE_GONE;
360
371
  if (await this._left(target))
361
372
  return TARGET_GONE;
373
+ if (await this._revoked(target)) {
374
+ return TARGET_REVOKED;
375
+ }
362
376
  const chat = await getChat(source, row.chatId);
363
377
  if (!chat || chat.deletedAt) {
364
378
  return SOURCE_GONE;
@@ -366,11 +380,26 @@ export class ChatMoves {
366
380
  const moved = await getChat(target, row.targetChatId);
367
381
  return moved?.deletedAt ? TARGET_GONE : null;
368
382
  }
383
+ async _revoked(mesh) {
384
+ const own = await mesh.view.get(DEVICES, { id: this._deviceId });
385
+ return !!own && isOwnRevokedRow(mesh, own);
386
+ }
387
+ async _sourceDropped(row) {
388
+ try {
389
+ return await this._left(await this._open(hex(row.meshKey)));
390
+ }
391
+ catch (err) {
392
+ if (isCoreError(err, 'MESH_NOT_FOUND')) {
393
+ return true;
394
+ }
395
+ throw err;
396
+ }
397
+ }
369
398
  async _left(mesh) {
370
399
  return mesh.role !== 'public' && !(await this._local.readMeshSession(mesh.key));
371
400
  }
372
401
  async _giveUp(pair, row, reason) {
373
- if (reason === TARGET_GONE) {
402
+ if (reason === TARGET_GONE || reason === TARGET_REVOKED) {
374
403
  await this._patch(row, { failure: reason });
375
404
  return;
376
405
  }
@@ -430,7 +459,9 @@ export class ChatMoves {
430
459
  const write = this._patching.then(async () => {
431
460
  const stored = (await this._local.readChatMove(row.meshKey, row.chatId)) ?? row;
432
461
  const next = { ...stored, ...(typeof patch === 'function' ? patch(stored) : patch) };
433
- await this._local.recordChatMove(next);
462
+ const settled = !!(next.completedAt || next.failure);
463
+ const dropped = settled && (await this._sourceDropped(next));
464
+ await (dropped ? this._local.dropChatMove(next) : this._local.recordChatMove(next));
434
465
  if (!quiet) {
435
466
  this._changed();
436
467
  }
@@ -1,7 +1,6 @@
1
1
  import type HyperDB from 'hyperdb';
2
2
  import type { QvacChat, QvacChatSuggestion, QvacChatGroup } from '../../spec/mesh/hyperschema/types.d.ts';
3
3
  import type { Mesh } from './mesh.ts';
4
- export declare const TERMINAL_RUN_STATES: ReadonlySet<string>;
5
4
  export interface ChatListing extends QvacChat {
6
5
  readonly lastActivityAt: number;
7
6
  }
@@ -1,4 +1,5 @@
1
1
  import { CoreError } from '../errors.js';
2
+ import { TERMINAL_RUN_STATES } from './contract/run-requests.js';
2
3
  import { mustGetActiveAgent } from './agent.js';
3
4
  import { listChatGroupsIn, mustGetActiveChatGroup } from './chat-group.js';
4
5
  import { appendChunk, listChunks, updateOp } from './chunk.js';
@@ -10,7 +11,6 @@ import { chatSortKey } from './chat-order.js';
10
11
  import { ownReadThrough, ownReads } from './chat-read.js';
11
12
  import b4a from 'b4a';
12
13
  import { ACTIVE_CHATS, ACTIVE_CHATS_BY_AGENT, ACTIVE_CHAT_GROUPS, CHATS, CHAT_GROUPS, CHUNK_COUNTERS } from './collections.js';
13
- export const TERMINAL_RUN_STATES = new Set(['completed', 'failed', 'canceled']);
14
14
  function byChatOrder(a, b) {
15
15
  return chatSortKey(a) - chatSortKey(b) || (a.id < b.id ? -1 : 1);
16
16
  }
@@ -1,5 +1,6 @@
1
1
  import type HyperDB from 'hyperdb';
2
2
  import type { QvacChunk, QvacUpdateChunkOperation } from '../../spec/mesh/hyperschema/types.d.ts';
3
+ import type { AssistantChunkContentToolResult } from '../../spec/hyperschema/types.d.ts';
3
4
  import type { Mesh, MeshDispatcher } from './mesh.ts';
4
5
  import { type Bounds } from './range.ts';
5
6
  type ChunkPayload = Pick<QvacChunk, 'userMessage' | 'content' | 'thinking' | 'toolCall' | 'toolResult' | 'attachment' | 'approvalRequest' | 'runStatus' | 'runRequest' | 'sources' | 'compaction' | 'context' | 'modelRequest' | 'roundReset' | 'agentSwitch'>;
@@ -18,13 +19,16 @@ type ListAttachmentsByKindInput = Bounds & {
18
19
  kind: MediaKind;
19
20
  origin?: AttachmentOrigin;
20
21
  };
21
- export type ReadChunk = QvacChunk & {
22
+ export type ReadChunk = Omit<QvacChunk, 'toolResult' | 'attachment'> & {
23
+ readonly toolResult: AssistantChunkContentToolResult | null;
22
24
  readonly attachment?: (NonNullable<QvacChunk['attachment']> & {
23
25
  readonly url?: string;
24
26
  }) | null;
25
27
  };
28
+ export declare function readChunk({ toolResult, ...chunk }: QvacChunk): ReadChunk;
29
+ export declare function linkChunks(store: Mesh, chunks: QvacChunk[]): ReadChunk[];
26
30
  export declare function recordLinks(store: Mesh, chunks: ReadChunk[]): Promise<ReadChunk[]>;
27
- type ListChunksInput = Bounds & {
31
+ export type ListChunksInput = Bounds & {
28
32
  chatId: string;
29
33
  type?: QvacChunk['type'];
30
34
  types?: ReadonlyArray<QvacChunk['type']>;
@@ -34,8 +38,15 @@ export interface FindChunksOfTypeInput extends Bounds {
34
38
  readonly type: QvacChunk['type'];
35
39
  }
36
40
  export declare function findChunksOfType(snap: HyperDB, { chatId, type, ...bounds }: FindChunksOfTypeInput): Promise<QvacChunk[]>;
37
- export declare function listChunks(store: Mesh, { chatId, type, types, ...bounds }: ListChunksInput): Promise<ReadChunk[]>;
41
+ export interface FindChunksOfTypesInput extends Omit<Bounds, 'limit'> {
42
+ readonly chatId: string;
43
+ readonly types: ReadonlyArray<QvacChunk['type']>;
44
+ }
45
+ export declare function findChunksOfTypes(db: HyperDB, { chatId, types, ...bounds }: FindChunksOfTypesInput): Promise<QvacChunk[]>;
46
+ export declare function listChunks(store: Mesh, input: ListChunksInput): Promise<ReadChunk[]>;
47
+ export declare function readChunks(db: HyperDB, { chatId, type, types, ...bounds }: ListChunksInput): Promise<QvacChunk[]>;
38
48
  export declare function listAttachmentsByKind(store: Mesh, { kind, origin, ...bounds }: ListAttachmentsByKindInput): Promise<ReadChunk[]>;
49
+ export declare function selectsChunk({ chatId, type, types, gt, gte, lt, lte }: ListChunksInput, chunk: QvacChunk): boolean;
39
50
  export declare function getChunk(store: Mesh, id: string): Promise<QvacChunk | null>;
40
51
  export declare function mustGetChunk(store: Mesh, id: string): Promise<QvacChunk>;
41
52
  export interface AppendedChunk {