@qvac/core 0.10.4 → 0.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) hide show
  1. package/CHANGELOG.md +114 -1
  2. package/dist-lib/index.d.ts +1 -0
  3. package/dist-lib/index.js +1 -0
  4. package/dist-lib/lib/api/chunk-delta.d.ts +6 -2
  5. package/dist-lib/lib/api/chunk-delta.js +69 -7
  6. package/dist-lib/lib/api/chunks.js +6 -2
  7. package/dist-lib/lib/api/config.d.ts +4 -1
  8. package/dist-lib/lib/api/config.js +23 -4
  9. package/dist-lib/lib/api/devices.js +3 -1
  10. package/dist-lib/lib/api/rag.js +4 -2
  11. package/dist-lib/lib/assistant/harness.d.ts +3 -1
  12. package/dist-lib/lib/assistant/harness.js +10 -0
  13. package/dist-lib/lib/assistant/history.d.ts +2 -1
  14. package/dist-lib/lib/assistant/history.js +39 -6
  15. package/dist-lib/lib/assistant/message.d.ts +3 -0
  16. package/dist-lib/lib/assistant/message.js +10 -2
  17. package/dist-lib/lib/assistant/pdf-budget.d.ts +3 -0
  18. package/dist-lib/lib/assistant/pdf-budget.js +13 -0
  19. package/dist-lib/lib/assistant-tree.d.ts +1 -0
  20. package/dist-lib/lib/assistant-tree.js +10 -3
  21. package/dist-lib/lib/assistant.d.ts +5 -1
  22. package/dist-lib/lib/assistant.js +112 -37
  23. package/dist-lib/lib/client.js +10 -1
  24. package/dist-lib/lib/core/archived-mesh.js +1 -0
  25. package/dist-lib/lib/core/chunk.d.ts +5 -2
  26. package/dist-lib/lib/core/chunk.js +74 -18
  27. package/dist-lib/lib/core/collections.d.ts +1 -0
  28. package/dist-lib/lib/core/collections.js +1 -0
  29. package/dist-lib/lib/core/config-value.d.ts +6 -0
  30. package/dist-lib/lib/core/config-value.js +14 -0
  31. package/dist-lib/lib/core/contract/chat.d.ts +1 -0
  32. package/dist-lib/lib/core/contract/chat.js +8 -1
  33. package/dist-lib/lib/core/contract/chunks.js +20 -6
  34. package/dist-lib/lib/core/contract/compute.d.ts +1 -1
  35. package/dist-lib/lib/core/contract/compute.js +2 -1
  36. package/dist-lib/lib/core/contract/devices.js +3 -2
  37. package/dist-lib/lib/core/departures.js +7 -0
  38. package/dist-lib/lib/core/devices.d.ts +2 -1
  39. package/dist-lib/lib/core/devices.js +4 -2
  40. package/dist-lib/lib/core/document-attachment.d.ts +7 -0
  41. package/dist-lib/lib/core/document-attachment.js +6 -0
  42. package/dist-lib/lib/core/files/index.d.ts +1 -1
  43. package/dist-lib/lib/core/files/index.js +1 -1
  44. package/dist-lib/lib/core/files/rag/index.d.ts +1 -1
  45. package/dist-lib/lib/core/files/rag/index.js +1 -1
  46. package/dist-lib/lib/core/files/rag/retrieval/full-text.d.ts +1 -0
  47. package/dist-lib/lib/core/files/rag/retrieval/full-text.js +4 -1
  48. package/dist-lib/lib/core/identity.d.ts +2 -0
  49. package/dist-lib/lib/core/identity.js +19 -0
  50. package/dist-lib/lib/core/inference-calls-receiver.js +2 -2
  51. package/dist-lib/lib/core/known-sender.d.ts +3 -0
  52. package/dist-lib/lib/core/known-sender.js +26 -0
  53. package/dist-lib/lib/core/local/index.d.ts +2 -0
  54. package/dist-lib/lib/core/local/index.js +17 -10
  55. package/dist-lib/lib/core/mesh.d.ts +5 -1
  56. package/dist-lib/lib/core/mesh.js +6 -6
  57. package/dist-lib/lib/core/migrations/chat-chunks.d.ts +4 -0
  58. package/dist-lib/lib/core/migrations/chat-chunks.js +43 -0
  59. package/dist-lib/lib/core/pdf/rasterize.d.ts +0 -1
  60. package/dist-lib/lib/core/pdf/rasterize.js +0 -1
  61. package/dist-lib/lib/core/skill-overlay-receiver.js +2 -2
  62. package/dist-lib/lib/core/skill-secrets.js +2 -2
  63. package/dist-lib/lib/core.d.ts +12 -1
  64. package/dist-lib/lib/core.js +75 -11
  65. package/dist-lib/lib/credential-pusher.d.ts +1 -0
  66. package/dist-lib/lib/credential-pusher.js +34 -48
  67. package/dist-lib/lib/device-tree.d.ts +18 -0
  68. package/dist-lib/lib/device-tree.js +32 -0
  69. package/dist-lib/lib/errors.d.ts +8 -0
  70. package/dist-lib/lib/errors.js +8 -0
  71. package/dist-lib/lib/oauth/flow-core.js +2 -0
  72. package/dist-lib/lib/oauth/loopback.d.ts +1 -0
  73. package/dist-lib/lib/oauth/loopback.js +31 -1
  74. package/dist-lib/lib/oauth/register.js +33 -18
  75. package/dist-lib/lib/oauth/request.d.ts +10 -0
  76. package/dist-lib/lib/oauth/request.js +22 -0
  77. package/dist-lib/lib/oauth/token-exchange-core.js +29 -32
  78. package/dist-lib/lib/oauth/types.d.ts +3 -0
  79. package/dist-lib/lib/oauth.d.ts +2 -1
  80. package/dist-lib/lib/oauth.js +1 -0
  81. package/dist-lib/lib/run-replay.d.ts +8 -2
  82. package/dist-lib/lib/run-replay.js +29 -11
  83. package/dist-lib/schema/engine.js +20 -3
  84. package/dist-lib/schema/field-order.js +8 -1
  85. package/dist-lib/schema/local.js +7 -3
  86. package/dist-lib/schema/qvac.js +19 -2
  87. package/dist-lib/spec/hrpc/messages.js +1236 -1184
  88. package/dist-lib/spec/hyperschema/index.js +1236 -1184
  89. package/dist-lib/spec/hyperschema/schema.json +47 -0
  90. package/dist-lib/spec/hyperschema/types.d.ts +11 -0
  91. package/dist-lib/spec/local/hyperdb/messages.js +70 -20
  92. package/dist-lib/spec/local/hyperschema/index.js +36 -11
  93. package/dist-lib/spec/local/hyperschema/schema.json +21 -1
  94. package/dist-lib/spec/local/hyperschema/types.d.ts +5 -1
  95. package/dist-lib/spec/mesh/hyperdb/db.json +17 -1
  96. package/dist-lib/spec/mesh/hyperdb/index.js +83 -2
  97. package/dist-lib/spec/mesh/hyperdb/messages.js +573 -255
  98. package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +7 -1
  99. package/dist-lib/spec/mesh/hyperdispatch/index.js +18 -1
  100. package/dist-lib/spec/mesh/hyperdispatch/messages.js +121 -8
  101. package/dist-lib/spec/mesh/hyperschema/index.js +121 -8
  102. package/dist-lib/spec/mesh/hyperschema/schema.json +69 -1
  103. package/dist-lib/spec/mesh/hyperschema/types.d.ts +19 -1
  104. package/package.json +12 -10
@@ -1,11 +1,15 @@
1
1
  import { devicesByWriterKey, trackActivity } from './activity.js';
2
- import { isPurged } from './chat.js';
2
+ import { deleteChatChunks, isPurged } from './chat.js';
3
3
  import { CHUNK_PAYLOAD_FIELDS, ragSourcesWithinBounds } from '../../../schema/qvac.js';
4
4
  import { chunkTypeOrdinal } from '../../../schema/indexes.js';
5
5
  import b4a from 'b4a';
6
- import { CHUNKS, CHUNKS_BY_TYPE, CHUNK_COUNTERS, DEVICES } from '../collections.js';
6
+ import { CHAT_CHUNKS, CHUNKS, CHUNKS_BY_CHAT, CHUNKS_BY_TYPE, CHUNK_COUNTERS, DEVICES } from '../collections.js';
7
7
  const TERMINAL_RUN_STATES = new Set(['completed', 'failed', 'canceled']);
8
8
  const CREDITED_CHUNK_TYPES = new Set(['run-request', 'run-status']);
9
+ async function putChunk(tx, chunk) {
10
+ await tx.insert(CHUNKS, chunk);
11
+ await tx.insert(CHAT_CHUNKS, chunk);
12
+ }
9
13
  async function runHasTerminalStatus(tx, chatId, runId) {
10
14
  const type = chunkTypeOrdinal('run-status');
11
15
  const statuses = tx.find(CHUNKS_BY_TYPE, {
@@ -99,7 +103,7 @@ export function registerChunkRoutes(router) {
99
103
  count: prevCount + 1,
100
104
  lastActivityAt: Math.max(counter?.lastActivityAt ?? 0, op.createdAt)
101
105
  });
102
- await tx.insert(CHUNKS, appended);
106
+ await putChunk(tx, appended);
103
107
  return CREDITED_CHUNK_TYPES.has(op.type) ? appended : undefined;
104
108
  }));
105
109
  router.add('@qvac/update-chunk', trackActivity(async (op, { tx, node, log }) => {
@@ -122,7 +126,7 @@ export function registerChunkRoutes(router) {
122
126
  return;
123
127
  }
124
128
  const next = { ...chunk, runStatus: op.runStatus, updatedAt: op.updatedAt };
125
- await tx.insert(CHUNKS, next);
129
+ await putChunk(tx, next);
126
130
  return next;
127
131
  }
128
132
  if (chunk.type === 'attachment' && op.attachment !== null && op.attachment !== undefined) {
@@ -131,7 +135,7 @@ export function registerChunkRoutes(router) {
131
135
  return;
132
136
  }
133
137
  const next = { ...chunk, attachment: op.attachment, updatedAt: op.updatedAt };
134
- await tx.insert(CHUNKS, next);
138
+ await putChunk(tx, next);
135
139
  return next;
136
140
  }
137
141
  log?.warn('contract', 'update-chunk dropped: only a run-status (runStatus) or attachment (attachment) edits', { id: op.id });
@@ -161,8 +165,18 @@ export function registerChunkRoutes(router) {
161
165
  },
162
166
  updatedAt: op.resolvedAt
163
167
  };
164
- await tx.insert(CHUNKS, resolved);
168
+ await putChunk(tx, resolved);
165
169
  log?.info('contract', `approval ${op.approved ? 'approved' : 'denied'}`, { id: op.id });
166
170
  return resolved;
167
171
  }));
172
+ router.add('@qvac/reindex-chat-chunks', async ({ chatId }, { tx }) => {
173
+ if (await isPurged(tx, chatId)) {
174
+ await deleteChatChunks(tx, chatId);
175
+ return;
176
+ }
177
+ const inChat = { gte: { chatId }, lte: { chatId } };
178
+ for (const chunk of await tx.find(CHUNKS_BY_CHAT, inChat).toArray()) {
179
+ await tx.insert(CHAT_CHUNKS, chunk);
180
+ }
181
+ });
168
182
  }
@@ -1,5 +1,5 @@
1
1
  import type { Router } from '../../../spec/mesh/hyperdispatch/index.js';
2
- declare const DROPPED: readonly ['@qvac/append-chunk', '@qvac/update-chunk', '@qvac/resolve-approval', '@qvac/put-agent', '@qvac/put-approval-rule', '@qvac/seed-defaults', '@qvac/add-knowledge', '@qvac/remove-knowledge', '@qvac/add-knowledge-artifact', '@qvac/claim-indexing', '@qvac/resolve-indexing', '@qvac/put-chat', '@qvac/put-tool-configs', '@qvac/append-tool-activity', '@qvac/migrate-agent-catalog', '@qvac/reindex-agents', '@qvac/put-chat-group', '@qvac/put-joined-mesh', '@qvac/purge-chat', '@qvac/name-mesh', '@qvac/put-mesh-seed'];
2
+ declare const DROPPED: readonly ['@qvac/append-chunk', '@qvac/update-chunk', '@qvac/resolve-approval', '@qvac/put-agent', '@qvac/put-approval-rule', '@qvac/seed-defaults', '@qvac/add-knowledge', '@qvac/remove-knowledge', '@qvac/add-knowledge-artifact', '@qvac/claim-indexing', '@qvac/resolve-indexing', '@qvac/put-chat', '@qvac/put-tool-configs', '@qvac/append-tool-activity', '@qvac/migrate-agent-catalog', '@qvac/reindex-agents', '@qvac/put-chat-group', '@qvac/put-joined-mesh', '@qvac/purge-chat', '@qvac/name-mesh', '@qvac/put-mesh-seed', '@qvac/reindex-chat-chunks'];
3
3
  export type DroppedComputeRoute = (typeof DROPPED)[number];
4
4
  export declare function droppedComputeRoutes(): ReadonlyArray<DroppedComputeRoute>;
5
5
  export declare function assertComputeRoute(compute: boolean, name: string): void;
@@ -20,7 +20,8 @@ const DROPPED = [
20
20
  '@qvac/put-joined-mesh',
21
21
  '@qvac/purge-chat',
22
22
  '@qvac/name-mesh',
23
- '@qvac/put-mesh-seed'
23
+ '@qvac/put-mesh-seed',
24
+ '@qvac/reindex-chat-chunks'
24
25
  ];
25
26
  const DROPPED_ROUTES = new Set(DROPPED);
26
27
  export function droppedComputeRoutes() {
@@ -82,7 +82,7 @@ export function registerDeviceRoutes(router) {
82
82
  await tx.insert(DEVICES, next);
83
83
  return next;
84
84
  }));
85
- router.add('@qvac/report-device-capabilities', trackActivity(async ({ deviceId, installedToolNames, installedSkillNames, installedModelNames, cachedModelNames, readySkillNames, toolCatalog }, { tx, node, log }) => {
85
+ router.add('@qvac/report-device-capabilities', trackActivity(async ({ deviceId, installedToolNames, installedSkillNames, installedModelNames, cachedModelNames, readySkillNames, toolCatalog, customModels }, { tx, node, log }) => {
86
86
  const existing = await tx.get(DEVICES, { id: deviceId });
87
87
  if (!existing) {
88
88
  log?.debug('contract', 'report-device-capabilities dropped: unknown device');
@@ -101,7 +101,8 @@ export function registerDeviceRoutes(router) {
101
101
  installedModelNames: installedModelNames ?? [],
102
102
  cachedModelNames: cachedModelNames ?? [],
103
103
  readySkillNames: readySkillNames ?? existing.readySkillNames,
104
- toolCatalog: toolCatalog ?? existing.toolCatalog
104
+ toolCatalog: toolCatalog ?? existing.toolCatalog,
105
+ customModels: customModels ?? existing.customModels
105
106
  };
106
107
  await tx.insert(DEVICES, next);
107
108
  return next;
@@ -47,6 +47,7 @@ export class PendingDepartures {
47
47
  seed: row.seed,
48
48
  key: row.creator ? null : row.key,
49
49
  writerSeed: row.writerSeed ?? undefined,
50
+ compute: row.compute,
50
51
  deviceId: this._deviceId() ?? undefined,
51
52
  logger: this._log,
52
53
  blindPeerKeys: this._blindPeerKeys,
@@ -163,6 +164,9 @@ async function admitted(mesh) {
163
164
  return true;
164
165
  if (await mesh.ownWriterRefused())
165
166
  return false;
167
+ if (mesh.closing) {
168
+ return false;
169
+ }
166
170
  return new Promise((resolve) => {
167
171
  const settle = (ok) => {
168
172
  mesh.autobee.off('writable', onWritable);
@@ -188,6 +192,9 @@ function waitForDelivery(mesh) {
188
192
  if (delivered()) {
189
193
  return Promise.resolve(true);
190
194
  }
195
+ if (mesh.closing) {
196
+ return Promise.resolve(false);
197
+ }
191
198
  return new Promise((resolve) => {
192
199
  const settle = (ok) => {
193
200
  local.off('upload', onUpload);
@@ -1,4 +1,4 @@
1
- import type { QvacToolReportEntry } from '../../spec/mesh/hyperschema/types.d.ts';
1
+ import type { QvacCustomModelReportEntry, QvacToolReportEntry } from '../../spec/mesh/hyperschema/types.d.ts';
2
2
  import type { AppBuildMetadata } from './app-build.ts';
3
3
  import type { Mesh } from './mesh.ts';
4
4
  export interface SaveDeviceCapabilitiesInput {
@@ -9,6 +9,7 @@ export interface SaveDeviceCapabilitiesInput {
9
9
  cachedModelNames: string[];
10
10
  readySkillNames?: string[] | null;
11
11
  toolCatalog?: QvacToolReportEntry[] | null;
12
+ customModels?: QvacCustomModelReportEntry[] | null;
12
13
  }
13
14
  export declare function saveDeviceCapabilities(store: Mesh, input: SaveDeviceCapabilitiesInput, isAccount: (mesh: Mesh) => boolean): Promise<void>;
14
15
  export declare function retractInventory(store: Mesh, deviceId: Buffer, isAccount: (mesh: Mesh) => boolean): Promise<void>;
@@ -32,7 +32,8 @@ export async function retractInventory(store, deviceId, isAccount) {
32
32
  row.installedSkillNames,
33
33
  row.installedModelNames,
34
34
  row.cachedModelNames,
35
- row.toolCatalog
35
+ row.toolCatalog,
36
+ row.customModels
36
37
  ].some((list) => list?.length) ||
37
38
  (row.readySkillNames?.length ?? 0) > (trimmed.readySkillNames?.length ?? 0);
38
39
  if (leaked) {
@@ -48,7 +49,8 @@ async function hostedOnly(store, input) {
48
49
  installedModelNames: [],
49
50
  cachedModelNames: [],
50
51
  readySkillNames: readinessForAgents(input.readySkillNames ?? [], hosted),
51
- toolCatalog: []
52
+ toolCatalog: [],
53
+ customModels: []
52
54
  };
53
55
  }
54
56
  export async function advertiseDeviceVersion(store, deviceId, app) {
@@ -23,6 +23,13 @@ export interface DocumentReadState {
23
23
  }
24
24
  export declare function documentUnreadReason({ knowledgeOn, failedFiles, attachedCount }: DocumentReadState): DocumentUnreadReason;
25
25
  export declare function documentUnreadNotice(reason: DocumentUnreadReason, plural: boolean): string;
26
+ export interface PdfPagesCutState {
27
+ readonly fileName: string;
28
+ readonly lastPage: number;
29
+ readonly pageCount: number;
30
+ }
31
+ export declare function pdfPagesCutNotice({ fileName, lastPage, pageCount }: PdfPagesCutState): string;
32
+ export declare function pdfPagesSkippedNotice(fileName: string): string;
26
33
  export interface AnnotatedMessage {
27
34
  content: string;
28
35
  documents?: DocumentAttachment[];
@@ -33,6 +33,12 @@ export function documentUnreadNotice(reason, plural) {
33
33
  }[reason];
34
34
  return `[The attached document${plural ? 's' : ''} could not be read for this reply: ${why}.]`;
35
35
  }
36
+ export function pdfPagesCutNotice({ fileName, lastPage, pageCount }) {
37
+ return `[Only pages 1–${lastPage} of "${fileName}" (${pageCount} pages) were shown to you as images; pages ${lastPage + 1}–${pageCount} were not, and no tool can show them. If asked about a later page you have no excerpt for, tell the user only the first ${lastPage} pages could be read. Do not guess its content.]`;
38
+ }
39
+ export function pdfPagesSkippedNotice(fileName) {
40
+ return `[No page of "${fileName}" was shown to you as an image: the files before it in this message used up the page images, and no tool can show them. If asked about it and you have no excerpt, tell the user it could not be read. Do not guess its content.]`;
41
+ }
36
42
  export function annotateDocumentAttachments(messages) {
37
43
  for (const turn of messages.filter((message) => message.documents?.length)) {
38
44
  const block = (turn.documents ?? []).map(documentAttachmentLine).join('\n');
@@ -11,7 +11,7 @@ import { type IndexingProgress, type IndexingEmbedder, type RagExtractors, type
11
11
  import type { Mesh } from '../mesh.ts';
12
12
  import type { ChatFileKey } from './records/source.ts';
13
13
  import type { SettleOptions } from './processing/settle.ts';
14
- export { fullTextBudgetChars, fullTextHits, retrieveRagKnowledge, searchRagChunks, referencesPendingFile, type VectorHit, type RagDocumentRef } from './rag/index.ts';
14
+ export { fullTextBudgetChars, fullTextHits, retrievalBudgetTokens, retrieveRagKnowledge, searchRagChunks, referencesPendingFile, type VectorHit, type RagDocumentRef } from './rag/index.ts';
15
15
  export { isKnowledgeRoutedAttachment } from './records/index.ts';
16
16
  export { assertRemovableAt } from './records/index.ts';
17
17
  export { assertArtifactOutput } from './records/index.ts';
@@ -15,7 +15,7 @@ import { PreviewProcessor, PREVIEW_VERSION, previewId } from './preview/index.js
15
15
  import { CoreError } from '../../errors.js';
16
16
  import { errorMessage } from '../../guards.js';
17
17
  import { ACTIVE_KNOWLEDGE_BY_CHAT, AGENTS, CHATS, INDEXING_STATUS_BY_CHAT, KNOWLEDGE, KNOWLEDGE_ARTIFACTS_BY_KNOWLEDGE } from '../collections.js';
18
- export { fullTextBudgetChars, fullTextHits, retrieveRagKnowledge, searchRagChunks, referencesPendingFile } from './rag/index.js';
18
+ export { fullTextBudgetChars, fullTextHits, retrievalBudgetTokens, retrieveRagKnowledge, searchRagChunks, referencesPendingFile } from './rag/index.js';
19
19
  export { isKnowledgeRoutedAttachment } from './records/index.js';
20
20
  export { assertRemovableAt } from './records/index.js';
21
21
  export { assertArtifactOutput } from './records/index.js';
@@ -31,6 +31,6 @@ export declare class RagProcessor implements FileProcessor {
31
31
  export { MAX_KNOWLEDGE_BYTES } from './limits.ts';
32
32
  export { canIndexChat } from './policy.ts';
33
33
  export type { IndexingEmbedder, IndexingProgress } from './produce.ts';
34
- export { formatFullDocumentContext, fullTextBudgetChars, fullTextHits, isFullTextHit, isTruncatedFullTextHit } from './retrieval/full-text.ts';
34
+ export { formatFullDocumentContext, fullTextBudgetChars, fullTextHits, isFullTextHit, isTruncatedFullTextHit, retrievalBudgetTokens } from './retrieval/full-text.ts';
35
35
  export { MemoryVectorIndex, RAG_DOCUMENT_KIND, RAG_KNOWLEDGE_OFF_NOTICE, RAG_TRUNCATED_NOTICE, RAG_UNAVAILABLE_NOTICE, WEB_TOOLS, chooseRagNotice, citedByPhraseOverlap, classifyRagDocument, createRagSourceTagFilter, encodeRagArtifact, formatRagContext, isSupportedRagDocument, isWholeDocumentQuery, referencedDocuments, referencesPendingFile, retrieveRagKnowledge, searchRagChunks, sourcesFromHits, webSourcesFromToolResult, withoutDataChannel } from 'rag-extract';
36
36
  export type { RagArtifactPayload, RagDocumentRef, RagExtractors, RagSource, VectorHit, VectorIndex } from 'rag-extract';
@@ -70,5 +70,5 @@ export class RagProcessor {
70
70
  }
71
71
  export { MAX_KNOWLEDGE_BYTES } from './limits.js';
72
72
  export { canIndexChat } from './policy.js';
73
- export { formatFullDocumentContext, fullTextBudgetChars, fullTextHits, isFullTextHit, isTruncatedFullTextHit } from './retrieval/full-text.js';
73
+ export { formatFullDocumentContext, fullTextBudgetChars, fullTextHits, isFullTextHit, isTruncatedFullTextHit, retrievalBudgetTokens } from './retrieval/full-text.js';
74
74
  export { MemoryVectorIndex, RAG_DOCUMENT_KIND, RAG_KNOWLEDGE_OFF_NOTICE, RAG_TRUNCATED_NOTICE, RAG_UNAVAILABLE_NOTICE, WEB_TOOLS, chooseRagNotice, citedByPhraseOverlap, classifyRagDocument, createRagSourceTagFilter, encodeRagArtifact, formatRagContext, isSupportedRagDocument, isWholeDocumentQuery, referencedDocuments, referencesPendingFile, retrieveRagKnowledge, searchRagChunks, sourcesFromHits, webSourcesFromToolResult, withoutDataChannel } from 'rag-extract';
@@ -2,6 +2,7 @@ import type { QvacKnowledge } from '../../../../../spec/mesh/hyperschema/types.d
2
2
  import type { Mesh } from '../../../mesh.ts';
3
3
  import { type VectorHit } from 'rag-extract';
4
4
  export declare function fullTextBudgetChars(ctxSize: number): number;
5
+ export declare function retrievalBudgetTokens(ctxSize: number): number | undefined;
5
6
  export interface IdentifiedHit {
6
7
  readonly id: string;
7
8
  }
@@ -1,5 +1,5 @@
1
1
  import * as z from 'zod';
2
- import { classifyRagDocument, extractPdfHead, extractRagDocumentFromBytes, parseRagMetadata, RAG_DOCUMENT_KIND } from 'rag-extract';
2
+ import { classifyRagDocument, DEFAULT_CHUNK_BUDGET_TOKENS, extractPdfHead, extractRagDocumentFromBytes, parseRagMetadata, RAG_DOCUMENT_KIND } from 'rag-extract';
3
3
  const DEFAULT_FULL_TEXT_BUDGET_TOKENS = 4096;
4
4
  const CHARS_PER_TOKEN = 4;
5
5
  const FULL_TEXT_KINDS = new Set([
@@ -14,6 +14,9 @@ export function fullTextBudgetChars(ctxSize) {
14
14
  const tokens = ctxSize > 0 ? Math.floor(ctxSize / 2) : DEFAULT_FULL_TEXT_BUDGET_TOKENS;
15
15
  return tokens * CHARS_PER_TOKEN;
16
16
  }
17
+ export function retrievalBudgetTokens(ctxSize) {
18
+ return ctxSize > 0 ? Math.min(DEFAULT_CHUNK_BUDGET_TOKENS, Math.floor(ctxSize / 4)) : undefined;
19
+ }
17
20
  const FULL_TEXT_HIT_PREFIX = 'full-text-';
18
21
  export function isFullTextHit(hit) {
19
22
  return hit.id.startsWith(FULL_TEXT_HIT_PREFIX);
@@ -18,6 +18,8 @@ export declare function generateMnemonic(): Promise<string>;
18
18
  export declare function openIdentity(mnemonic: string): Promise<IdentityKey>;
19
19
  export declare function mintIdentity(mnemonic: string, devicePublicKey: Buffer): Promise<MintedIdentity>;
20
20
  export declare function attestDeviceProof(devicePublicKey: Buffer, parent: KeyPair, parentProof: Buffer): Promise<Buffer>;
21
+ export declare function pairingAnchorSeed(accountSeed: Buffer, memberId: Buffer): Buffer;
22
+ export declare function chainTo(proof: Buffer, publicKey: Buffer): Buffer | null;
21
23
  export declare function delegationFits(proof: Buffer, delegate: KeyPair): Promise<boolean>;
22
24
  export declare function verifyDeviceProof(proof: Buffer, expected: ExpectedDeviceProof): Promise<boolean>;
23
25
  export declare function claimAttestation(meshPublicKey: Buffer, deviceId: Buffer, writerKey: Buffer): Buffer;
@@ -1,8 +1,10 @@
1
+ import b4a from 'b4a';
1
2
  import c from 'compact-encoding';
2
3
  import crypto from 'hypercore-crypto';
3
4
  import { ProofEncoding } from 'keet-identity-key/lib/encoding.js';
4
5
  export const MAX_PROOF_BYTES = 1024;
5
6
  const [CLAIM_NAMESPACE] = crypto.namespace('@qvac/core/member-claim', 1);
7
+ const [ANCHOR_NAMESPACE] = crypto.namespace('@qvac/core/pairing-anchor', 1);
6
8
  let loaded = null;
7
9
  async function identityKey() {
8
10
  if (loaded === null) {
@@ -25,6 +27,23 @@ export async function attestDeviceProof(devicePublicKey, parent, parentProof) {
25
27
  throw new Error('attestDeviceProof needs the parent proof');
26
28
  return (await identityKey()).attestDevice(devicePublicKey, parent, parentProof);
27
29
  }
30
+ export function pairingAnchorSeed(accountSeed, memberId) {
31
+ return crypto.hash([ANCHOR_NAMESPACE, accountSeed, memberId]);
32
+ }
33
+ export function chainTo(proof, publicKey) {
34
+ let decoded;
35
+ try {
36
+ decoded = c.decode(ProofEncoding, proof);
37
+ }
38
+ catch {
39
+ return null;
40
+ }
41
+ const at = decoded.chain.findIndex((link) => b4a.equals(link.publicKey, publicKey));
42
+ if (at === -1) {
43
+ return null;
44
+ }
45
+ return c.encode(ProofEncoding, { ...decoded, chain: decoded.chain.slice(0, at + 1) });
46
+ }
28
47
  export async function delegationFits(proof, delegate) {
29
48
  const device = crypto.keyPair();
30
49
  const extended = await attestDeviceProof(device.publicKey, delegate, proof);
@@ -1,12 +1,12 @@
1
1
  import b4a from 'b4a';
2
2
  import { getAgent } from './agent.js';
3
3
  import { getChat } from './chat.js';
4
- import { DEVICES } from './collections.js';
5
4
  import { CoreError } from '../errors.js';
5
+ import { knownSender } from './known-sender.js';
6
6
  const MAX_ANSWER_BYTES = 2 * 1024 * 1024;
7
7
  export function createInferenceCallsReceiver(opts) {
8
8
  return async (request, fromDeviceId) => {
9
- const sender = await opts.mesh.view.get(DEVICES, { id: fromDeviceId });
9
+ const sender = await knownSender(opts.mesh, fromDeviceId);
10
10
  if (!sender || sender.revoked) {
11
11
  throw new CoreError('PEER_SENDER_REVOKED');
12
12
  }
@@ -0,0 +1,3 @@
1
+ import type { QvacDevice } from '../../spec/mesh/hyperschema/types.d.ts';
2
+ import type { Mesh } from './mesh.ts';
3
+ export declare function knownSender(mesh: Mesh, deviceId: Uint8Array): Promise<QvacDevice | null>;
@@ -0,0 +1,26 @@
1
+ import { DEVICES } from './collections.js';
2
+ import { ACK_TIMEOUT_MS } from './handoff-channel.js';
3
+ const SENDER_ROW_WAIT_MS = ACK_TIMEOUT_MS / 2;
4
+ export async function knownSender(mesh, deviceId) {
5
+ const read = () => mesh.view.get(DEVICES, { id: deviceId });
6
+ const row = await read();
7
+ if (row) {
8
+ return row;
9
+ }
10
+ return new Promise((resolve) => {
11
+ const settle = (found) => {
12
+ clearTimeout(timer);
13
+ mesh.unwatch(check);
14
+ mesh.off('close', onClose);
15
+ resolve(found);
16
+ };
17
+ const onClose = () => settle(null);
18
+ const check = () => {
19
+ void read().then((found) => found && settle(found), onClose);
20
+ };
21
+ const timer = setTimeout(onClose, SENDER_ROW_WAIT_MS);
22
+ mesh.watch(check);
23
+ mesh.on('close', onClose);
24
+ check();
25
+ });
26
+ }
@@ -1,6 +1,7 @@
1
1
  import CorestoreStorage from 'hypercore-storage';
2
2
  import HyperDB from 'hyperdb';
3
3
  import type { LocalAccountRecovery, LocalAppConfig, LocalCapabilityProfile, LocalChatMove, LocalMeshUpdate, LocalChatNotify, LocalChatOrder, LocalCustomModel, LocalIdentity, LocalMemberProof, LocalMeshDeparture, LocalMeshMembership, LocalModelDownload, LocalModelNotify } from '../../../spec/local/hyperschema/types.d.ts';
4
+ import type { QvacAutobeeHead } from '../../../spec/mesh/hyperschema/types.d.ts';
4
5
  import type { DeviceMetadata } from '../mesh.ts';
5
6
  import { LocalFiles } from './files.ts';
6
7
  export interface ResolvedSession {
@@ -8,6 +9,7 @@ export interface ResolvedSession {
8
9
  readonly baseKey: Buffer | undefined;
9
10
  readonly writerSeed: string | undefined;
10
11
  readonly compute: boolean;
12
+ readonly bootFrom?: QvacAutobeeHead | null;
11
13
  }
12
14
  export interface ArchivedMeshMembership extends LocalMeshMembership {
13
15
  readonly leftAt: number;
@@ -9,6 +9,7 @@ import LocalDatabaseSpec from '../../../spec/local/hyperdb/index.js';
9
9
  import b4a from 'b4a';
10
10
  import { dropMeshFiles, LocalFiles } from './files.js';
11
11
  import { LOCAL_ACCOUNT_RECOVERY, LOCAL_ACTIVE_MESH, LOCAL_APP_CONFIG, LOCAL_CHAT_MOVES, LOCAL_MESH_UPDATES, LOCAL_CHAT_NOTIFY, LOCAL_CHAT_ORDER, LOCAL_CHAT_ORDER_BY_KEY, LOCAL_CONFIG, LOCAL_CUSTOM_MODEL, LOCAL_DEVICE, LOCAL_IDENTITY, LOCAL_MEMBERSHIP, LOCAL_MEMBER_PROOF, LOCAL_MESH_DEPARTURE, LOCAL_MESH_HISTORY, LOCAL_MESH_MEMBERSHIP, LOCAL_MESH_SESSION, LOCAL_MODEL_DOWNLOAD, LOCAL_MODEL_NOTIFY } from '../collections.js';
12
+ import { configValue, readConfigValue } from '../config-value.js';
12
13
  const IDENTITY_ID = 'identity';
13
14
  const MEMBERSHIP_ID = 'membership';
14
15
  const APP_CONFIG_ID = 'default';
@@ -20,7 +21,8 @@ function sessionOf(row) {
20
21
  seed: row.seed,
21
22
  baseKey: row.key ?? undefined,
22
23
  writerSeed: row.writerSeed ?? undefined,
23
- compute: row.compute ?? false
24
+ compute: row.compute ?? false,
25
+ bootFrom: row.bootKey ? { key: row.bootKey, length: row.bootLength ?? 0 } : null
24
26
  };
25
27
  }
26
28
  export function openLocalStore(storage) {
@@ -46,12 +48,12 @@ class LocalStore {
46
48
  }
47
49
  async get(key) {
48
50
  const row = await this._db.get(LOCAL_CONFIG, { key });
49
- return row === null ? null : row.value;
51
+ return row === null ? null : readConfigValue(row);
50
52
  }
51
53
  set(key, value) {
52
54
  return this._locked(async () => {
53
55
  const tx = this._db.transaction();
54
- await tx.insert(LOCAL_CONFIG, { key, value });
56
+ await tx.insert(LOCAL_CONFIG, { key, ...configValue(value) });
55
57
  await tx.flush();
56
58
  });
57
59
  }
@@ -59,7 +61,7 @@ class LocalStore {
59
61
  return this._locked(async () => {
60
62
  const tx = this._db.transaction();
61
63
  for (const { key, value } of entries) {
62
- await tx.insert(LOCAL_CONFIG, { key, value });
64
+ await tx.insert(LOCAL_CONFIG, { key, ...configValue(value) });
63
65
  }
64
66
  await tx.flush();
65
67
  });
@@ -71,10 +73,10 @@ class LocalStore {
71
73
  return null;
72
74
  }
73
75
  const tx = this._db.transaction();
74
- await tx.insert(LOCAL_CONFIG, { key: toKey, value: row.value });
76
+ await tx.insert(LOCAL_CONFIG, { ...row, key: toKey });
75
77
  await tx.delete(LOCAL_CONFIG, { key: fromKey });
76
78
  await tx.flush();
77
- return row.value;
79
+ return readConfigValue(row);
78
80
  });
79
81
  }
80
82
  deleteAll(keys) {
@@ -510,7 +512,9 @@ class LocalStore {
510
512
  seed: session.seed,
511
513
  writerSeed: session.writerSeed,
512
514
  compute: session.compute,
513
- awaitingAdmission: existing?.awaitingAdmission
515
+ awaitingAdmission: existing?.awaitingAdmission,
516
+ bootKey: session.bootFrom?.key,
517
+ bootLength: session.bootFrom?.length
514
518
  });
515
519
  if (minted) {
516
520
  await tx.insert(LOCAL_ACTIVE_MESH, { deviceId, homeMeshKey: key });
@@ -537,7 +541,8 @@ class LocalStore {
537
541
  key: row.key,
538
542
  seed: row.seed,
539
543
  creator: opts.creator,
540
- writerSeed: row.writerSeed
544
+ writerSeed: row.writerSeed,
545
+ compute: row.compute
541
546
  });
542
547
  await tx.flush();
543
548
  });
@@ -590,12 +595,14 @@ class LocalStore {
590
595
  const { awaitingAdmission } = opts;
591
596
  return this._locked(async () => {
592
597
  const existing = await this._db.get(LOCAL_MESH_MEMBERSHIP, { key });
593
- if (!existing || !!existing.awaitingAdmission === awaitingAdmission) {
598
+ const settled = awaitingAdmission || !existing?.bootKey;
599
+ if (!existing || (!!existing.awaitingAdmission === awaitingAdmission && settled)) {
594
600
  return;
595
601
  }
602
+ const { bootKey, bootLength, ...rest } = existing;
596
603
  const tx = this._db.transaction();
597
604
  await tx.insert(LOCAL_MESH_MEMBERSHIP, {
598
- ...existing,
605
+ ...(awaitingAdmission ? existing : rest),
599
606
  awaitingAdmission
600
607
  });
601
608
  await tx.flush();
@@ -46,7 +46,11 @@ export interface MeshOptions {
46
46
  slowDrainMs?: number;
47
47
  }
48
48
  export declare function meshPublicKeyFromSeed(seed: Buffer): Buffer;
49
- export declare function isOurTrustedWriter(snap: HyperDB, writerKey: Buffer, ourMemberId: Buffer | null): Promise<boolean>;
49
+ export interface TrustScope {
50
+ readonly ourMemberId: Buffer | null;
51
+ readonly compute: boolean;
52
+ }
53
+ export declare function isTrustedWriter(snap: HyperDB, writerKey: Buffer, { ourMemberId, compute }: TrustScope): Promise<boolean>;
50
54
  export type BirthRow = {
51
55
  readonly id: Buffer;
52
56
  readonly joinedAt: number;
@@ -36,7 +36,7 @@ const WARMUP_COLLECTIONS = [
36
36
  '@qvac/members',
37
37
  '@qvac/active-chats'
38
38
  ];
39
- export async function isOurTrustedWriter(snap, writerKey, ourMemberId) {
39
+ export async function isTrustedWriter(snap, writerKey, { ourMemberId, compute }) {
40
40
  const device = await liveDeviceForWriter(snap, writerKey);
41
41
  if (!device) {
42
42
  return false;
@@ -44,7 +44,7 @@ export async function isOurTrustedWriter(snap, writerKey, ourMemberId) {
44
44
  if (!ourMemberId) {
45
45
  return true;
46
46
  }
47
- return !!device.memberId && b4a.equals(device.memberId, ourMemberId);
47
+ return !!device.memberId && (!compute || b4a.equals(device.memberId, ourMemberId));
48
48
  }
49
49
  export function claimableDevice(row) {
50
50
  return {
@@ -665,9 +665,9 @@ export class Mesh extends ReadyResource {
665
665
  return false;
666
666
  const snap = reference.db.snapshot();
667
667
  try {
668
- const trusted = await isOurTrustedWriter(snap, key, this._ourMemberId);
668
+ const trusted = await isTrustedWriter(snap, key, this);
669
669
  if (!trusted) {
670
- this._log?.debug(this._scope, 'writer is no device of ours', {
670
+ this._log?.debug(this._scope, 'writer is no live device of this mesh', {
671
671
  writerKey: b4a.toHex(key).slice(0, 16)
672
672
  });
673
673
  }
@@ -685,7 +685,7 @@ export class Mesh extends ReadyResource {
685
685
  for await (const writer of target.db.find(LIVE_WRITERS_BY_MOST_RECENT, {
686
686
  reverse: true
687
687
  })) {
688
- if ((await isOurTrustedWriter(snap, writer.writerKey, this._ourMemberId)) ||
688
+ if ((await isTrustedWriter(snap, writer.writerKey, this)) ||
689
689
  (await this._vouchedByItsRow(target, reference, writer.writerKey))) {
690
690
  this._log?.debug(this._scope, 'vouching for a head', {
691
691
  writerKey: b4a.toHex(writer.writerKey).slice(0, 16),
@@ -694,7 +694,7 @@ export class Mesh extends ReadyResource {
694
694
  return { key: writer.writerKey, length: writer.length };
695
695
  }
696
696
  }
697
- this._log?.debug(this._scope, 'vouching for no head: no writer of ours has been active');
697
+ this._log?.debug(this._scope, 'vouching for no head: no live writer has been active');
698
698
  return null;
699
699
  }
700
700
  finally {
@@ -0,0 +1,4 @@
1
+ import type { Mesh } from '../mesh.ts';
2
+ export declare const REINDEX_FROM_APP_VERSION = "0.21.0";
3
+ export declare function floorExcludesBefore(range: string | null | undefined, release: string): boolean;
4
+ export declare function backfillChatChunks(mesh: Mesh): Promise<number>;
@@ -0,0 +1,43 @@
1
+ import { Range, Version, constants, satisfies } from 'bare-semver';
2
+ import { CHAT_CHUNKS, CHUNK_COUNTERS, PURGED_CHATS } from '../collections.js';
3
+ import { releaseVersion } from '../min-version.js';
4
+ export const REINDEX_FROM_APP_VERSION = '0.21.0';
5
+ export function floorExcludesBefore(range, release) {
6
+ let floor;
7
+ try {
8
+ floor = Range.parse(range ?? '');
9
+ }
10
+ catch {
11
+ return false;
12
+ }
13
+ const atLeast = Range.parse(`>=${releaseVersion(release)}`);
14
+ return (floor.comparators.length > 0 &&
15
+ floor.comparators.every((set) => set.some((bound) => LOWER_BOUNDS.has(bound.operator) && satisfies(releaseOf(bound.version), atLeast))));
16
+ }
17
+ const LOWER_BOUNDS = new Set([constants.GTE, constants.GT, constants.EQ]);
18
+ function releaseOf({ major, minor, patch }) {
19
+ return new Version(major, minor, patch);
20
+ }
21
+ export async function backfillChatChunks(mesh) {
22
+ const counters = await mesh.view.find(CHUNK_COUNTERS).toArray();
23
+ const purged = await mesh.view.find(PURGED_CHATS).toArray();
24
+ let sent = 0;
25
+ for (const chat of [...counters, ...purged]) {
26
+ if (mesh.closing || !mesh.writable)
27
+ break;
28
+ const due = 'count' in chat ? !(await holdsEnds(mesh.view, chat)) : await holdsAny(mesh.view, chat);
29
+ if (due) {
30
+ await mesh.dispatch('reindex-chat-chunks', { chatId: chat.chatId });
31
+ sent++;
32
+ }
33
+ }
34
+ return sent;
35
+ }
36
+ async function holdsEnds(db, { chatId, count }) {
37
+ const first = await db.get(CHAT_CHUNKS, { chatId, seq: 0 });
38
+ const last = await db.get(CHAT_CHUNKS, { chatId, seq: count - 1 });
39
+ return first !== null && last !== null;
40
+ }
41
+ async function holdsAny(db, { chatId }) {
42
+ return ((await db.find(CHAT_CHUNKS, { gte: { chatId }, lte: { chatId } }).one()) !== null);
43
+ }
@@ -1,6 +1,5 @@
1
1
  import type { LoggerInterface } from '@qvac/logging';
2
2
  export declare const PDF_RENDER_SCALE = 2;
3
- export declare const MAX_PDF_PAGE_IMAGES = 10;
4
3
  interface PageFlags {
5
4
  hasImage: boolean;
6
5
  hasText: boolean;
@@ -1,6 +1,5 @@
1
1
  import { MAX_IMAGE_WIDTH, rasterToModelJpeg } from '../image/normalize.js';
2
2
  export const PDF_RENDER_SCALE = 2;
3
- export const MAX_PDF_PAGE_IMAGES = 10;
4
3
  export function renderScale(pageWidth) {
5
4
  return Math.min(PDF_RENDER_SCALE, MAX_IMAGE_WIDTH / pageWidth);
6
5
  }
@@ -1,10 +1,10 @@
1
1
  import b4a from 'b4a';
2
2
  import { getAgent } from './agent.js';
3
- import { DEVICES } from './collections.js';
4
3
  import { CoreError } from '../errors.js';
4
+ import { knownSender } from './known-sender.js';
5
5
  export function createSkillOverlayReceiver(opts) {
6
6
  return async (op, fromDeviceId) => {
7
- const sender = await opts.mesh.view.get(DEVICES, { id: fromDeviceId });
7
+ const sender = await knownSender(opts.mesh, fromDeviceId);
8
8
  if (!sender || sender.revoked) {
9
9
  throw new CoreError('PEER_SENDER_REVOKED');
10
10
  }
@@ -4,11 +4,11 @@ import { isDeviceSharedCredentialKey } from '@qvac/harness/lib/tools/skills/scop
4
4
  import { settingConfigKey } from '../credential-pusher.js';
5
5
  import { scopedKey } from '../oauth/keys.js';
6
6
  import { getAgent } from './agent.js';
7
- import { DEVICES } from './collections.js';
8
7
  import { CoreError } from '../errors.js';
8
+ import { knownSender } from './known-sender.js';
9
9
  export function createSkillSecretReceiver(opts) {
10
10
  return async (request, fromDeviceId) => {
11
- const sender = await opts.mesh.view.get(DEVICES, { id: fromDeviceId });
11
+ const sender = await knownSender(opts.mesh, fromDeviceId);
12
12
  if (!sender || sender.revoked) {
13
13
  throw new CoreError('PEER_SENDER_REVOKED');
14
14
  }