@qvac/core 1.0.0-rc.11 → 1.0.0-rc.13

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 (82) hide show
  1. package/CHANGELOG.md +66 -1
  2. package/dist-lib/lib/assistant/harness.d.ts +2 -2
  3. package/dist-lib/lib/assistant/history.js +30 -2
  4. package/dist-lib/lib/assistant.js +2 -1
  5. package/dist-lib/lib/core/archived-mesh.js +7 -3
  6. package/dist-lib/lib/core/chat-copy.d.ts +18 -3
  7. package/dist-lib/lib/core/chat-copy.js +73 -23
  8. package/dist-lib/lib/core/chat-group.js +14 -9
  9. package/dist-lib/lib/core/chat-move.d.ts +9 -0
  10. package/dist-lib/lib/core/chat-move.js +94 -3
  11. package/dist-lib/lib/core/chat.js +21 -8
  12. package/dist-lib/lib/core/collections.d.ts +1 -0
  13. package/dist-lib/lib/core/collections.js +1 -0
  14. package/dist-lib/lib/core/contract/activity.js +2 -2
  15. package/dist-lib/lib/core/contract/chat-group.d.ts +1 -0
  16. package/dist-lib/lib/core/contract/chat-group.js +21 -1
  17. package/dist-lib/lib/core/contract/chat.d.ts +9 -0
  18. package/dist-lib/lib/core/contract/chat.js +127 -5
  19. package/dist-lib/lib/core/contract/chunks.d.ts +1 -0
  20. package/dist-lib/lib/core/contract/chunks.js +37 -6
  21. package/dist-lib/lib/core/contract/features.d.ts +21 -0
  22. package/dist-lib/lib/core/contract/features.js +22 -0
  23. package/dist-lib/lib/core/contract/indexing.js +4 -2
  24. package/dist-lib/lib/core/contract/knowledge.js +5 -4
  25. package/dist-lib/lib/core/contract/members.js +2 -2
  26. package/dist-lib/lib/core/contract/projects.d.ts +5 -0
  27. package/dist-lib/lib/core/contract/projects.js +12 -4
  28. package/dist-lib/lib/core/contract/roles.js +30 -4
  29. package/dist-lib/lib/core/contract/run-requests.js +2 -2
  30. package/dist-lib/lib/core/contract/seed-defaults.js +2 -2
  31. package/dist-lib/lib/core/contract/tool-configs.js +3 -2
  32. package/dist-lib/lib/core/contract/workspace.d.ts +2 -0
  33. package/dist-lib/lib/core/contract/workspace.js +40 -7
  34. package/dist-lib/lib/core/contract/writers.js +2 -2
  35. package/dist-lib/lib/core/files/attachments.js +4 -1
  36. package/dist-lib/lib/core/files/index.d.ts +1 -0
  37. package/dist-lib/lib/core/files/index.js +19 -6
  38. package/dist-lib/lib/core/files/processing/index.js +4 -1
  39. package/dist-lib/lib/core/files/rag/build.js +23 -1
  40. package/dist-lib/lib/core/files/rag/load.d.ts +3 -0
  41. package/dist-lib/lib/core/files/rag/load.js +27 -6
  42. package/dist-lib/lib/core/local/index.d.ts +6 -2
  43. package/dist-lib/lib/core/local/index.js +12 -3
  44. package/dist-lib/lib/core/mesh.d.ts +6 -0
  45. package/dist-lib/lib/core/mesh.js +22 -12
  46. package/dist-lib/lib/core/migrations/workspace-from-0-21/index.d.ts +85 -0
  47. package/dist-lib/lib/core/migrations/workspace-from-0-21/index.js +690 -0
  48. package/dist-lib/lib/core/migrations/writer-links.d.ts +0 -1
  49. package/dist-lib/lib/core/migrations/writer-links.js +12 -10
  50. package/dist-lib/lib/core/project-keys.d.ts +6 -0
  51. package/dist-lib/lib/core/project-keys.js +11 -0
  52. package/dist-lib/lib/core/public-mesh.d.ts +2 -0
  53. package/dist-lib/lib/core/public-mesh.js +5 -0
  54. package/dist-lib/lib/core/sync/changed.d.ts +4 -2
  55. package/dist-lib/lib/core/sync/changed.js +1 -1
  56. package/dist-lib/lib/core/sync/index.js +27 -2
  57. package/dist-lib/lib/core/workspace-upgrade.d.ts +6 -0
  58. package/dist-lib/lib/core/workspace-upgrade.js +26 -16
  59. package/dist-lib/lib/core.d.ts +4 -32
  60. package/dist-lib/lib/core.js +143 -482
  61. package/dist-lib/lib/errors.d.ts +4 -0
  62. package/dist-lib/lib/errors.js +4 -0
  63. package/dist-lib/lib/run-transcript.d.ts +2 -1
  64. package/dist-lib/lib/run-transcript.js +11 -5
  65. package/dist-lib/schema/engine.js +2 -1
  66. package/dist-lib/schema/qvac.js +26 -1
  67. package/dist-lib/spec/hrpc/messages.js +17 -8
  68. package/dist-lib/spec/hyperschema/index.js +17 -8
  69. package/dist-lib/spec/hyperschema/schema.json +5 -0
  70. package/dist-lib/spec/hyperschema/types.d.ts +1 -0
  71. package/dist-lib/spec/mesh/hyperdb/db.json +15 -0
  72. package/dist-lib/spec/mesh/hyperdb/index.js +70 -1
  73. package/dist-lib/spec/mesh/hyperdb/messages.js +806 -438
  74. package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +18 -0
  75. package/dist-lib/spec/mesh/hyperdispatch/index.js +52 -1
  76. package/dist-lib/spec/mesh/hyperdispatch/messages.js +303 -3
  77. package/dist-lib/spec/mesh/hyperschema/index.js +303 -3
  78. package/dist-lib/spec/mesh/hyperschema/schema.json +146 -0
  79. package/dist-lib/spec/mesh/hyperschema/types.d.ts +39 -0
  80. package/package.json +10 -8
  81. package/tui/README.md +2 -1
  82. package/tui/boot.mjs +2 -0
package/CHANGELOG.md CHANGED
@@ -13,6 +13,68 @@ release.
13
13
 
14
14
  ## [Unreleased]
15
15
 
16
+ ## [1.0.0-rc.13] - 2026-11-13
17
+
18
+ Since 1.0.0-rc.12.
19
+
20
+ ### Changed
21
+
22
+ - Uses `autobee` ^2.13.6. (#868)
23
+ - Needs `@qvac/harness` 1.0.0-rc.23 and `@qvac/skills` ^0.5.0, up from 1.0.0-rc.22 and ^0.4.0. The
24
+ harness's charts plugin needs `qvac-charts`, now a dependency of Core. (#870)
25
+
26
+ ### Fixed
27
+
28
+ - Keeps members' writes across workspace upgrades, chat moves and edits, and kick or removal paths. (#833)
29
+ - Projects made by the 0.20 workspace upgrade list every member the upgrade counted from their
30
+ first row, with the seed sealed to those members in the same batch, so they can open the project
31
+ even if the device that made it stops right after. See [projects](docs/api/projects.md). (#852)
32
+
33
+ ## [1.0.0-rc.12] - 2026-11-13
34
+
35
+ Since 1.0.0-rc.11.
36
+
37
+ ### Added
38
+
39
+ - `listKnowledgeBase` and `knowledgeBaseWatch`: an entry carries `progress`, the chunks embedded
40
+ so far and the total, while this device holds the file's indexing claim and is embedding it. It
41
+ is device-local, so other members see only `status`. See
42
+ [knowledge](docs/api/knowledge.md#listknowledgebase). (#808)
43
+ - `duck` runs up to four agent replies on one loaded model at once. The sidecar's own default is
44
+ unchanged. (#861)
45
+
46
+ ### Changed
47
+
48
+ - Needs `@qvac/harness` 1.0.0-rc.22 and `@qvac/skills` ^0.4.0, up from 1.0.0-rc.20 and ^0.3.0.
49
+ The skills' diagrams skill calls the harness's `draw_diagram`. (#817)
50
+ - A write that a mesh's close drops now refuses with `MESH_CLOSED_BEFORE_WRITE` (100133) and leaves
51
+ nothing in the log, where it used to resolve as if it had landed. Send it again once the mesh is
52
+ open. See [mesh](docs/api/mesh.md). (#838)
53
+ - hypercore 11.40.4, hyperbee2 2.22.0, compact-encoding 3.7.0 and protomux 3.13.1.
54
+ compact-encoding's `float32` and `float64` now throw a `TypeError` on a non-number instead of
55
+ writing NaN. (#857)
56
+
57
+ ### Fixed
58
+
59
+ - A project's knowledge files that another device indexed no longer stay "Not available here"
60
+ behind a file this device is rebuilding locally while one of its runs lasts. (#860)
61
+ - A run a tool answered, such as a diagram from `draw_diagram`, is replayed on later turns with
62
+ that call before its reply, so a follow-up edits the diagram rather than rewriting it by hand.
63
+ (#817)
64
+ - A PDF page of fewer than 12 words, such as a chapter divider or a contents page, is no longer a
65
+ chunk of its own that matched almost any short prompt: its text opens the next page and cites
66
+ it. A PDF indexed before this keeps its chunks. (#841)
67
+ - A 0.21 device that fast-forwarded onto its sibling's 1.0 account and halted boots on 1.0 with no
68
+ peer again, where `ready()` used to hang. (#838)
69
+ - A logout, a leave or a shutdown no longer waits on a mesh's drain, which hung a logout beside a
70
+ mesh still joining. (#838)
71
+ - A `joinMesh` refused with `MESH_VIEW_UNAVAILABLE` leaves nothing behind, so the same invite joins
72
+ once the row reads; the next `joinMesh` of it used to never return. (#838)
73
+
74
+ ## [1.0.0-rc.11] - 2026-11-12
75
+
76
+ Everything since 0.11.4, across the 1.0.0 prereleases up to this one.
77
+
16
78
  ### Added
17
79
 
18
80
  - `continueRunHere({ meshId, chunkId })`: the device a run was sent from takes back a run another
@@ -2341,7 +2403,10 @@ Published under the `release-0.1` dist-tag.
2341
2403
  await assistant.ready()
2342
2404
  ```
2343
2405
 
2344
- [Unreleased]: https://github.com/tetherto/qvac-core/compare/v0.11.4...HEAD
2406
+ [Unreleased]: https://github.com/tetherto/qvac-core/compare/v1.0.0-rc.13...HEAD
2407
+ [1.0.0-rc.13]: https://github.com/tetherto/qvac-core/compare/v1.0.0-rc.12...v1.0.0-rc.13
2408
+ [1.0.0-rc.12]: https://github.com/tetherto/qvac-core/compare/v1.0.0-rc.11...v1.0.0-rc.12
2409
+ [1.0.0-rc.11]: https://github.com/tetherto/qvac-core/compare/v0.11.4...v1.0.0-rc.11
2345
2410
  [0.11.4]: https://github.com/tetherto/qvac-core/compare/v0.11.3...v0.11.4
2346
2411
  [0.11.3]: https://github.com/tetherto/qvac-core/compare/v0.11.2...v0.11.3
2347
2412
  [0.11.2]: https://github.com/tetherto/qvac-core/compare/v0.11.1...v0.11.2
@@ -1,5 +1,5 @@
1
1
  import type { AbortSignal } from 'bare-abort-controller';
2
- import type { ApprovalCall, CompletionEvent, DeleteModelSummary, DriveOpFn, GrantedDrive, HostRunLog, KnowledgeArtifact, KnowledgeHit, KnowledgeQuery, KnowledgeRemoved, KnowledgeStatus, KnowledgeUpsertResult, ManagedSkill, MemoryBudget, ModelRoundIo, ModelRoundPort, ModelRoundRequest, ModelRoundResult, ModelSourcePreview, OcrReadContext, PreviewModelSourceContext, SkillFileContent } from '@qvac/harness';
2
+ import type { ApprovalCall, CompletionEvent, DeleteModelSummary, DriveOpFn, GrantedDrive, HostRunLog, KnowledgeArtifact, KnowledgeHit, KnowledgeQuery, KnowledgeRemoved, KnowledgeStatus, KnowledgeUpsertResult, ManagedSkill, MemoryBudget, ModelRoundIo, ModelRoundPort, ModelRoundRequest, ModelRoundResult, ModelSourcePreview, OcrPage, OcrReadContext, PreviewModelSourceContext, SkillFileContent } from '@qvac/harness';
3
3
  import type { MediaRoundIo, MediaRoundPort, MediaRoundRequest, MediaRoundResult } from '@qvac/harness/lib/contract/index.ts';
4
4
  import type { SkillCatalogOptions, SkillManagementHooks } from '../core.ts';
5
5
  import type { AssistantAgent, AssistantCapabilityProfile, AssistantChunkContentAttachment, AssistantCustomModelPreview, AssistantCustomModelReportEntry, AssistantDeleteModelRequest, AssistantDetectLanguageRequest, AssistantDetectLanguageResponse, AssistantDeleteModelResponse, AssistantEnhanceMode, AssistantKvCacheDeletionResponse, AssistantListModelsRequest, AssistantModelInfoList, AssistantPreviewCustomModelRequest, AssistantSkillCatalogResponse, AssistantSpeakRequest, AssistantSpeakResponse, AssistantToolCatalogResponse, AssistantToolReportEntry, AssistantTranscribeRequest, AssistantTranscribeResponse } from '../../spec/hyperschema/types.d.ts';
@@ -191,7 +191,7 @@ export interface Harness {
191
191
  knowledgeRemove?(artifactId: string): Promise<KnowledgeRemoved>;
192
192
  knowledgeSearch?(query: KnowledgeQuery): Promise<KnowledgeHit[]>;
193
193
  knowledgeStatus?(): Promise<KnowledgeStatus>;
194
- ocrRead?(context: OcrReadContext): Promise<string>;
194
+ ocrRead?(context: OcrReadContext): Promise<OcrPage>;
195
195
  detectLanguage?(context: {
196
196
  text: string;
197
197
  topK?: number;
@@ -1,4 +1,4 @@
1
- import { attachmentTurn, skillLoadedTurn } from '@qvac/harness/lib/capabilities/tool-turns.ts';
1
+ import { answeredToolTurns, attachmentTurn, isAnswer, skillLoadedTurn } from '@qvac/harness/lib/capabilities/tool-turns.ts';
2
2
  import { HISTORY_CHUNK_TYPES } from '../core/chunk.js';
3
3
  import { isAudioAttachment } from '../core/audio-attachment.js';
4
4
  import { isDocumentAttachment, isPdfAttachment } from '../core/document-attachment.js';
@@ -23,12 +23,20 @@ export async function listHistoryChunks(engine, chatId) {
23
23
  engine.listChunks({ chatId, type: 'tool-result' }),
24
24
  engine.listChunks({ chatId, type: 'run-status' })
25
25
  ]);
26
+ const answered = new Set((toolResults.chunks ?? []).flatMap((chunk) => isAnswer(chunk.toolResult?.result) ? [callKey(chunk)] : []));
27
+ const calls = answered.size
28
+ ? ((await engine.listChunks({ chatId, type: 'tool-call' })).chunks ?? [])
29
+ : [];
26
30
  return [
27
31
  ...(history.chunks ?? []),
28
32
  ...(toolResults.chunks ?? []),
29
- ...(runStatuses.chunks ?? [])
33
+ ...(runStatuses.chunks ?? []),
34
+ ...calls.filter((chunk) => answered.has(callKey(chunk)))
30
35
  ].sort((a, b) => a.seq - b.seq);
31
36
  }
37
+ function callKey(chunk) {
38
+ return `${chunk.runId ?? ''}:${chunk.callId ?? ''}`;
39
+ }
32
40
  export function latestAttemptStarts(chunks) {
33
41
  return attemptStarts(chunks);
34
42
  }
@@ -88,6 +96,7 @@ export function historyFromChunks(chunks, stale = {}, { sentRuns = new Map(), as
88
96
  const history = [];
89
97
  const replayedSkills = new Set();
90
98
  const answeredCalls = new Set();
99
+ const calls = new Map();
91
100
  if (folded.summary) {
92
101
  history.push({ role: 'user', content: compactionTurn(folded.summary) });
93
102
  }
@@ -175,7 +184,26 @@ export function historyFromChunks(chunks, stale = {}, { sentRuns = new Map(), as
175
184
  anchor(chunk.runId);
176
185
  continue;
177
186
  }
187
+ if (chunk.toolCall) {
188
+ calls.set(callKey(chunk), chunk);
189
+ continue;
190
+ }
178
191
  if (chunk.toolResult && chunk.runId && sentRuns.has(chunk.runId)) {
192
+ const call = calls.get(callKey(chunk))?.toolCall;
193
+ const live = !staleRunIds.has(chunk.runId) && !beforeLatestAttempt(chunk, attemptStarts);
194
+ if (call &&
195
+ chunk.callId &&
196
+ isAnswer(chunk.toolResult.result) &&
197
+ live &&
198
+ !repeatedCall(chunk, answeredCalls)) {
199
+ const turns = answeredToolTurns({
200
+ name: call.name,
201
+ id: chunk.callId,
202
+ arguments: isRecord(call.arguments) ? call.arguments : {}
203
+ });
204
+ history.splice(history.indexOf(anchor(chunk.runId)), 0, ...turns);
205
+ turns.forEach((turn) => kept.add(turn));
206
+ }
179
207
  continue;
180
208
  }
181
209
  if (chunk.toolResult?.name === SKILL_TOOL) {
@@ -1185,7 +1185,8 @@ export class QvacAssistant extends ReadyResource {
1185
1185
  .catch(() => [])
1186
1186
  : [];
1187
1187
  const musicModelChoice = musicModelChoiceOf(configs);
1188
- const model = skillModelsForAgent({ ...agent, musicModelChoice }).find((entry) => entry.use === kind);
1188
+ const host = await this._runnableHost();
1189
+ const model = skillModelsForAgent({ ...agent, musicModelChoice }, host).find((entry) => entry.use === kind);
1189
1190
  if (!model) {
1190
1191
  return false;
1191
1192
  }
@@ -4,7 +4,7 @@ import { copyBlob, copyChunks, stripped } from './chat-copy.js';
4
4
  import { publicMeshKey, publicSeed } from './public-mesh.js';
5
5
  import { Mesh } from './mesh.js';
6
6
  import { recordedRole } from './local/index.js';
7
- import { ACTIVE_AGENTS, ACTIVE_CHATS, ACTIVE_CHAT_GROUPS, ACTIVE_TOOL_CONFIGS_BY_AGENT, AGENTS, KNOWLEDGE, MESH_SEEDS } from './collections.js';
7
+ import { ACTIVE_AGENTS, ACTIVE_CHATS, ACTIVE_CHAT_GROUPS, ACTIVE_TOOL_CONFIGS_BY_AGENT, AGENTS, CHATS, CHAT_GROUPS, KNOWLEDGE, MESH_SEEDS } from './collections.js';
8
8
  import { CoreError } from '../errors.js';
9
9
  const MIGRATED_SUFFIX = ' (migrated)';
10
10
  export async function withArchivedMesh(row, opts, fn) {
@@ -109,10 +109,14 @@ export async function migrateContents(from, into, configs = {}) {
109
109
  const targets = { into, configsFrom: configs.from ?? from, configsInto: configs.into ?? into };
110
110
  const agents = await migrateAgents(targets, new Set(chats.map((chat) => chat.agentId)));
111
111
  for (const group of groups) {
112
- batch.dispatch('put-chat-group', { group: stripped(group) });
112
+ if (!(await into.view.get(CHAT_GROUPS, { id: group.id }))) {
113
+ batch.dispatch('put-chat-group', { group: stripped(group) });
114
+ }
113
115
  }
114
116
  for (const chat of chats) {
115
- batch.dispatch('put-chat', { chat: stripped(chat) });
117
+ if (!(await into.view.get(CHATS, { id: chat.id }))) {
118
+ batch.dispatch('put-chat', { chat: stripped(chat) });
119
+ }
116
120
  }
117
121
  await batch.flush();
118
122
  for (const chat of chats) {
@@ -1,14 +1,17 @@
1
1
  import type HyperDB from 'hyperdb';
2
2
  import type { QvacChunk } from '../../spec/mesh/hyperschema/types.d.ts';
3
3
  import type { Mesh } from './mesh.ts';
4
+ export declare const UNCOPIED: ReadonlySet<string>;
4
5
  export interface MeshCopy {
5
6
  readonly from: Mesh;
6
7
  readonly into: Mesh;
7
8
  }
8
- export interface ChatMoveCopy extends MeshCopy {
9
+ export interface BlobCopy extends MeshCopy {
10
+ readonly references: Map<string, string>;
11
+ }
12
+ export interface ChatMoveCopy extends BlobCopy {
9
13
  readonly chatId: string;
10
14
  readonly originDeviceId: Buffer;
11
- readonly references: Map<string, string>;
12
15
  }
13
16
  export declare function copyChunks(snap: HyperDB, chatId: string, { from, into }: MeshCopy): Promise<void>;
14
17
  export interface MovedChunks {
@@ -19,7 +22,7 @@ export interface MovedChunks {
19
22
  export declare function copyMovedChunks(snap: HyperDB, chatId: string, copy: ChatMoveCopy): Promise<MovedChunks>;
20
23
  export declare function sentBy(chunk: QvacChunk): Buffer | undefined;
21
24
  export declare function copyMovedKnowledge(snap: HyperDB, chatId: string, copy: ChatMoveCopy): Promise<void>;
22
- export declare function movedBlob({ from, into, references }: ChatMoveCopy, { blobId }: BlobHolder): Promise<string>;
25
+ export declare function movedBlob({ from, into, references }: BlobCopy, { blobId }: BlobHolder): Promise<string>;
23
26
  export declare function removedReference({ mimeType }: TypedFile): string;
24
27
  interface TypedFile {
25
28
  readonly mimeType: string;
@@ -36,6 +39,18 @@ export interface ContentCopy extends MeshCopy {
36
39
  readonly deviceId: Buffer;
37
40
  readonly chats?: ReadonlySet<string>;
38
41
  readonly endsOpenRuns?: boolean;
42
+ readonly baseline?: CopyBaseline;
43
+ readonly historyOnly?: boolean;
44
+ }
45
+ export interface CopyBaseline {
46
+ get(key: string): Promise<CopiedFields | null>;
47
+ set(rows: ReadonlyArray<CopiedRow>): Promise<void>;
48
+ }
49
+ export type CopiedFields = Readonly<Record<string, string>>;
50
+ export declare function isCopiedFields(value: unknown): value is CopiedFields;
51
+ export interface CopiedRow {
52
+ readonly key: string;
53
+ readonly fields: CopiedFields;
39
54
  }
40
55
  export declare function copyContent(copy: ContentCopy): Promise<void>;
41
56
  export declare const UPGRADED_RUN_REASON = "the app was updated before this was answered, send it again";
@@ -1,12 +1,15 @@
1
1
  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
+ import { isRecord } from '../guards.js';
4
5
  import { inlineReference } from './files/store/inline.js';
5
6
  import { TERMINAL_RUN_STATES } from './contract/run-requests.js';
6
7
  import { appendChunk, ownedByUs, updateOp } from './chunk.js';
7
- import { runRequestOf } from './contract/chunks.js';
8
+ import { mayEditChunk, runRequestOf } from './contract/chunks.js';
9
+ import { PATCHABLE_CHAT } from './contract/chat.js';
10
+ import { PATCHABLE_GROUP } from './contract/chat-group.js';
8
11
  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';
9
- const UNCOPIED = new Set(['context', 'model-request']);
12
+ export const UNCOPIED = new Set(['context', 'model-request']);
10
13
  export async function copyChunks(snap, chatId, { from, into }) {
11
14
  const batch = into.batch();
12
15
  for (const chunk of await chunksOf(snap, chatId)) {
@@ -175,16 +178,20 @@ export function stripped(row) {
175
178
  const { version: _version, ...rest } = row;
176
179
  return rest;
177
180
  }
181
+ export function isCopiedFields(value) {
182
+ return isRecord(value) && Object.values(value).every((field) => typeof field === 'string');
183
+ }
178
184
  export async function copyContent(copy) {
179
185
  await copy.into.autobee.update();
180
186
  await copy.from.autobee.update();
181
187
  const snap = copy.from.view.snapshot();
182
188
  try {
183
- const remap = await defaultAgentRemap(snap, copy.into);
184
- const chats = await copyRows(snap, copy, remap);
189
+ const chats = copy.historyOnly
190
+ ? [...(copy.chats ?? [])]
191
+ : (await copyRows(snap, copy, await defaultAgentRemap(snap, copy.into))).map(({ id }) => id);
185
192
  const references = new Map();
186
- for (const chat of chats) {
187
- await copyChatChunks(snap, chat.id, { ...copy, references });
193
+ for (const chatId of chats) {
194
+ await copyChatChunks(snap, chatId, { ...copy, references });
188
195
  }
189
196
  await copyFiles(snap, { ...copy, references });
190
197
  await copyToolActivity(snap, copy);
@@ -207,10 +214,12 @@ async function defaultAgentRemap(snap, into) {
207
214
  return remap;
208
215
  }
209
216
  async function copyRows(snap, copy, remap) {
210
- const { into } = copy;
217
+ const { into, baseline } = copy;
211
218
  const batch = into.batch();
212
219
  const chats = await copiedChats(snap, copy);
213
- await copyAgentsAndGroups(snap, { into, batch, remap, chats, ungroup: !!copy.chats });
220
+ const copied = [];
221
+ const catalog = { into, batch, remap, chats, ungroup: !!copy.chats, baseline, copied };
222
+ await copyAgentsAndGroups(snap, catalog);
214
223
  const live = [];
215
224
  for (const chat of chats) {
216
225
  const agent = remap.get(chat.agentId);
@@ -220,12 +229,10 @@ async function copyRows(snap, copy, remap) {
220
229
  if (!chat.deletedAt) {
221
230
  live.push(chat);
222
231
  }
223
- if (await newer(into, CHATS, chat)) {
224
- const groupId = copy.chats ? undefined : chat.groupId;
225
- batch.dispatch('put-chat', {
226
- chat: { ...stripped(chat), agentId: agent?.id ?? chat.agentId, groupId }
227
- });
228
- }
232
+ const groupId = copy.chats ? undefined : chat.groupId;
233
+ const sent = { ...stripped(chat), agentId: agent?.id ?? chat.agentId, groupId };
234
+ const edit = await editOf(into, { collection: CHATS, row: sent, patchable: PATCHABLE_CHAT, baseline }, copied);
235
+ dispatchEdit(batch, edit, { put: 'put-chat', patch: 'patch-chat', payload: { chat: sent } });
229
236
  }
230
237
  const purged = copy.chats ? [] : await snap.find(PURGED_CHATS).toArray();
231
238
  for (const row of purged) {
@@ -234,13 +241,14 @@ async function copyRows(snap, copy, remap) {
234
241
  }
235
242
  }
236
243
  await batch.flush();
244
+ await baseline?.set(copied);
237
245
  return live;
238
246
  }
239
247
  async function copiedChats(snap, { chats }) {
240
248
  const all = await snap.find(CHATS).toArray();
241
249
  return chats ? all.filter((chat) => chats.has(chat.id) && !chat.deletedAt) : all;
242
250
  }
243
- async function copyAgentsAndGroups(snap, { into, batch, remap, chats, ungroup }) {
251
+ async function copyAgentsAndGroups(snap, { into, batch, remap, chats, ungroup, baseline, copied }) {
244
252
  const used = new Set(chats.map((chat) => chat.agentId));
245
253
  for (const agent of await snap.find(AGENTS).toArray()) {
246
254
  if (used.has(agent.id) && !remap.has(agent.id) && (await newer(into, AGENTS, agent))) {
@@ -248,10 +256,37 @@ async function copyAgentsAndGroups(snap, { into, batch, remap, chats, ungroup })
248
256
  }
249
257
  }
250
258
  const groups = ungroup ? [] : await snap.find(CHAT_GROUPS).toArray();
251
- for (const group of groups) {
252
- if (await newer(into, CHAT_GROUPS, group)) {
253
- batch.dispatch('put-chat-group', { group: stripped(group) });
254
- }
259
+ for (const row of groups) {
260
+ const group = stripped(row);
261
+ const copy = { collection: CHAT_GROUPS, row: group, patchable: PATCHABLE_GROUP, baseline };
262
+ const edit = await editOf(into, copy, copied);
263
+ dispatchEdit(batch, edit, {
264
+ put: 'put-chat-group',
265
+ patch: 'patch-chat-group',
266
+ payload: { group }
267
+ });
268
+ }
269
+ }
270
+ async function editOf(into, { collection, row, patchable, baseline }, copied) {
271
+ const held = await into.view.get(collection, { id: row.id });
272
+ if (!baseline) {
273
+ return !held || (held.updatedAt ?? 0) < (row.updatedAt ?? 0) ? 'put' : [];
274
+ }
275
+ const key = `${collection}/${row.id}`;
276
+ const fields = Object.fromEntries(patchable.map((f) => [f, JSON.stringify(row[f] ?? null)]));
277
+ const last = await baseline.get(key);
278
+ const changed = last ? patchable.filter((field) => fields[field] !== last[field]) : [];
279
+ if (!last || changed.length > 0) {
280
+ copied.push({ key, fields });
281
+ }
282
+ return !held || (row.deletedAt && !held.deletedAt) ? 'put' : changed;
283
+ }
284
+ function dispatchEdit(batch, edit, { put, patch, payload }) {
285
+ if (edit === 'put') {
286
+ batch.dispatch(put, payload);
287
+ }
288
+ else if (edit.length > 0) {
289
+ batch.dispatch(patch, { ...payload, fields: edit });
255
290
  }
256
291
  }
257
292
  async function seededChat(snap, chat, agent) {
@@ -323,16 +358,27 @@ async function senderOf(snap, chunk) {
323
358
  return sentBy(chunk) ?? device?.memberId ?? undefined;
324
359
  }
325
360
  async function updateHeldChunk(chunk, held, copy) {
326
- if (!b4a.equals(chunk.originDeviceId, copy.deviceId))
327
- return;
328
361
  const { into } = copy;
362
+ const decided = chunk.approvalRequest?.status;
363
+ if (held.approvalRequest?.status === 'pending' && decided && decided !== 'pending') {
364
+ const resolvedAt = chunk.updatedAt ?? chunk.createdAt;
365
+ await into.dispatch('resolve-approval', {
366
+ id: chunk.id,
367
+ approved: decided === 'approved',
368
+ resolvedAt
369
+ });
370
+ return;
371
+ }
329
372
  if (chunk.runStatus) {
330
- if (newerEdit(chunk, held)) {
373
+ if (b4a.equals(chunk.originDeviceId, copy.deviceId) && newerEdit(chunk, held)) {
331
374
  const op = updateOp(chunk, { id: chunk.id, runStatus: chunk.runStatus });
332
375
  await into.dispatch('update-chunk', op);
333
376
  }
334
377
  return;
335
378
  }
379
+ if (!(await mayEditChunk(into.view, held, into.localWriterKey))) {
380
+ return;
381
+ }
336
382
  const attachment = await repointed(chunk, held, copy);
337
383
  if (attachment) {
338
384
  await into.dispatch('update-chunk', updateOp(chunk, { id: chunk.id, attachment }));
@@ -366,7 +412,11 @@ async function copyFiles(snap, copy) {
366
412
  const copied = chats ? rows.filter((row) => !!row.chatId && chats.has(row.chatId)) : rows;
367
413
  const ids = new Set(copied.map((row) => row.id));
368
414
  for (const row of copied) {
369
- if (await into.view.get(KNOWLEDGE, { id: row.id })) {
415
+ const held = await into.view.get(KNOWLEDGE, { id: row.id });
416
+ if (held && row.deletedAt && !held.deletedAt) {
417
+ batch.dispatch('remove-knowledge', { knowledgeId: row.id, at: row.deletedAt });
418
+ }
419
+ if (held) {
370
420
  continue;
371
421
  }
372
422
  const blobId = row.deletedAt ? removedReference(row) : await movedBlob(moveOf(copy), row);
@@ -1,4 +1,5 @@
1
1
  import { CoreError } from '../errors.js';
2
+ import { nextUpdatedAt } from './contract/chat.js';
2
3
  import { randomId } from './id.js';
3
4
  import { place } from './rank.js';
4
5
  import { ACTIVE_CHAT_GROUPS, CHAT_GROUPS } from './collections.js';
@@ -66,8 +67,12 @@ export async function createChatGroup(store, input) {
66
67
  }
67
68
  async function patchChatGroup(store, id, patch) {
68
69
  const current = await mustGetActiveChatGroup(store, id);
69
- const updated = { ...current, ...patch, updatedAt: Date.now() };
70
- await store.dispatch('put-chat-group', { group: updated });
70
+ const updated = {
71
+ ...current,
72
+ ...patch,
73
+ updatedAt: nextUpdatedAt(current.updatedAt, Date.now())
74
+ };
75
+ await store.dispatch('patch-chat-group', { group: updated, fields: Object.keys(patch) });
71
76
  return updated;
72
77
  }
73
78
  export async function renameChatGroup(store, id, name) {
@@ -102,15 +107,14 @@ export async function moveChatGroup(store, id, { afterId }) {
102
107
  await snap.close();
103
108
  }
104
109
  const stamp = (row, rank) => ({
105
- ...row,
106
- rank,
107
- updatedAt: Math.max(now, row.updatedAt)
110
+ group: { ...row, rank, updatedAt: nextUpdatedAt(row.updatedAt, now) },
111
+ fields: ['rank']
108
112
  });
109
113
  const batch = store.batch();
110
114
  for (const { row, rank } of placement.respaced) {
111
- batch.dispatch('put-chat-group', { group: stamp(row, rank) });
115
+ batch.dispatch('patch-chat-group', stamp(row, rank));
112
116
  }
113
- batch.dispatch('put-chat-group', { group: stamp(current, placement.rank) });
117
+ batch.dispatch('patch-chat-group', stamp(current, placement.rank));
114
118
  await batch.flush();
115
119
  return mustGetActiveChatGroup(store, id);
116
120
  }
@@ -120,8 +124,9 @@ export async function sortChatGroupsByName(store) {
120
124
  for (const group of await listChatGroups(store)) {
121
125
  if (!group.rank)
122
126
  continue;
123
- batch.dispatch('put-chat-group', {
124
- group: { ...group, rank: 0, updatedAt: Math.max(now, group.updatedAt) }
127
+ batch.dispatch('patch-chat-group', {
128
+ group: { ...group, rank: 0, updatedAt: nextUpdatedAt(group.updatedAt, now) },
129
+ fields: ['rank']
125
130
  });
126
131
  }
127
132
  await batch.flush();
@@ -28,6 +28,9 @@ export declare class ChatMoves {
28
28
  private readonly _listeners;
29
29
  private readonly _settling;
30
30
  private readonly _moving;
31
+ private readonly _followed;
32
+ private readonly _forwards;
33
+ private readonly _forwarding;
31
34
  private readonly _abort;
32
35
  private readonly _onChange;
33
36
  private _patching;
@@ -49,6 +52,12 @@ export declare class ChatMoves {
49
52
  private _settle;
50
53
  private _land;
51
54
  private _leave;
55
+ followSource(source: Mesh): Promise<void>;
56
+ private _follow;
57
+ private _forward;
58
+ private _forwardMoved;
59
+ private _unreachable;
60
+ private _carryLate;
52
61
  private _cancel;
53
62
  private _stopped;
54
63
  private _copy;
@@ -30,6 +30,9 @@ export class ChatMoves {
30
30
  _listeners = new Set();
31
31
  _settling = new Map();
32
32
  _moving = new Map();
33
+ _followed = new WeakSet();
34
+ _forwards = new Set();
35
+ _forwarding = new Set();
33
36
  _abort = new AbortController();
34
37
  _onChange = () => this._changed();
35
38
  _patching = Promise.resolve();
@@ -104,6 +107,12 @@ export class ChatMoves {
104
107
  if (!row.completedAt && !row.failure) {
105
108
  this._start(row).copied.catch(noop);
106
109
  }
110
+ else if (row.completedAt && !row.failure) {
111
+ const pair = await this._pairOf(row);
112
+ if (pair) {
113
+ this._follow(pair.source, new Set([row.chatId]));
114
+ }
115
+ }
107
116
  }
108
117
  }
109
118
  async close() {
@@ -111,6 +120,7 @@ export class ChatMoves {
111
120
  this._abort.abort(new AbortedError());
112
121
  this._changed();
113
122
  await Promise.allSettled([...this._settling.values()].map((settling) => settling.done));
123
+ await Promise.all(this._forwards);
114
124
  }
115
125
  async _begin({ meshId, chatId, targetMeshId, agentId }) {
116
126
  if (meshId === targetMeshId)
@@ -220,8 +230,8 @@ export class ChatMoves {
220
230
  await this._stopped(pair, row, moved.open);
221
231
  continue;
222
232
  }
223
- if (moved.copied === 0 && carry.pending === 0) {
224
- return this._leave(pair, row);
233
+ if (moved.copied === 0 && carry.pending === 0 && (await this._leave(pair, row))) {
234
+ return;
225
235
  }
226
236
  if (carry.pending > 0) {
227
237
  await this._changeOf(pair);
@@ -231,14 +241,95 @@ export class ChatMoves {
231
241
  async _leave(pair, row) {
232
242
  const gone = await this._gone(pair, row);
233
243
  if (gone) {
234
- return this._giveUp(pair, row, gone);
244
+ await this._giveUp(pair, row, gone);
245
+ return true;
246
+ }
247
+ const late = await this._copy(pair, row);
248
+ if (late.copied > 0 || late.open.size > 0 || late.withheld > 0) {
249
+ return false;
235
250
  }
236
251
  await this._restart(pair, row);
237
252
  const { agentId } = await deleteChat(pair.source, this._deviceId, row.chatId);
238
253
  await this._patch(row, { completedAt: Date.now() });
254
+ this._follow(pair.source, new Set([row.chatId]));
239
255
  await this._agents.unpick(pair.source, agentId).catch((err) => {
240
256
  this._log?.warn('chat-move', "letting go of the moved chat's agent failed", { err });
241
257
  });
258
+ return true;
259
+ }
260
+ async followSource(source) {
261
+ const rows = await this._local.listChatMovesFrom(source.key);
262
+ if (rows.some((row) => row.completedAt && !row.failure)) {
263
+ this._follow(source);
264
+ }
265
+ }
266
+ _follow(source, chatIds = null) {
267
+ if (!this._followed.has(source)) {
268
+ this._followed.add(source);
269
+ source.on('chats-edited', (edited) => this._forward(source, edited));
270
+ source.on('landed', () => this._forward(source, null));
271
+ }
272
+ this._forward(source, chatIds);
273
+ }
274
+ _forward(source, chatIds) {
275
+ if (this._closing || source.closing)
276
+ return;
277
+ const forwarding = this._forwardMoved(source, chatIds).catch((err) => {
278
+ if (this._closing || source.closing)
279
+ return;
280
+ this._log?.warn('chat-move', 'forwarding a write into the moved chat failed', { err });
281
+ });
282
+ this._forwards.add(forwarding);
283
+ void forwarding.then(() => this._forwards.delete(forwarding));
284
+ }
285
+ async _forwardMoved(source, chatIds) {
286
+ const rows = chatIds
287
+ ? await Promise.all([...chatIds].map((id) => this._local.readChatMove(source.key, id)))
288
+ : await this._local.listChatMovesFrom(source.key);
289
+ for (const row of rows) {
290
+ if (this._closing)
291
+ return;
292
+ if (!row?.completedAt || row.failure)
293
+ continue;
294
+ const [late] = await listChunks(source, { chatId: row.chatId, limit: 1 });
295
+ const pair = late ? await this._pairOf(row) : null;
296
+ if (!pair || (await this._unreachable(pair.target, row))) {
297
+ continue;
298
+ }
299
+ const snap = source.view.snapshot();
300
+ try {
301
+ await copyMovedChunks(snap, row.chatId, this._copyOf(pair, row));
302
+ }
303
+ finally {
304
+ await snap.close();
305
+ }
306
+ this._carryLate(pair, row);
307
+ }
308
+ }
309
+ async _unreachable(target, row) {
310
+ if ((await this._left(target)) || (await this._revoked(target))) {
311
+ return true;
312
+ }
313
+ const moved = await getChat(target, row.targetChatId);
314
+ return !moved || !!moved.deletedAt;
315
+ }
316
+ _carryLate(pair, row) {
317
+ const carry = { pair, row, signal: this._abort.signal };
318
+ void pendingFiles(pair.target, row.targetChatId).then((pending) => {
319
+ for (const file of pending) {
320
+ if (!file.chunk || !this._owns(file.chunk) || this._forwarding.has(file.id)) {
321
+ continue;
322
+ }
323
+ this._forwarding.add(file.id);
324
+ this._carryChunk(carry, file)
325
+ .catch((err) => {
326
+ if (err instanceof AbortedError)
327
+ return;
328
+ this._log?.warn('chat-move', 'carrying a late file failed', { id: file.id, err });
329
+ })
330
+ .finally(() => this._forwarding.delete(file.id));
331
+ }
332
+ }, noop);
242
333
  }
243
334
  async _cancel({ source }, row) {
244
335
  await cancelActiveRuns(source, row.chatId, { deviceId: this._deviceId, reason: CHAT_MOVED });