@qvac/core 1.0.0-rc.12 → 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 (63) hide show
  1. package/CHANGELOG.md +19 -1
  2. package/dist-lib/lib/assistant.js +2 -1
  3. package/dist-lib/lib/core/archived-mesh.js +7 -3
  4. package/dist-lib/lib/core/chat-copy.d.ts +18 -3
  5. package/dist-lib/lib/core/chat-copy.js +73 -23
  6. package/dist-lib/lib/core/chat-group.js +14 -9
  7. package/dist-lib/lib/core/chat-move.d.ts +9 -0
  8. package/dist-lib/lib/core/chat-move.js +94 -3
  9. package/dist-lib/lib/core/chat.js +21 -8
  10. package/dist-lib/lib/core/collections.d.ts +1 -0
  11. package/dist-lib/lib/core/collections.js +1 -0
  12. package/dist-lib/lib/core/contract/activity.js +2 -2
  13. package/dist-lib/lib/core/contract/chat-group.d.ts +1 -0
  14. package/dist-lib/lib/core/contract/chat-group.js +21 -1
  15. package/dist-lib/lib/core/contract/chat.d.ts +9 -0
  16. package/dist-lib/lib/core/contract/chat.js +127 -5
  17. package/dist-lib/lib/core/contract/chunks.d.ts +1 -0
  18. package/dist-lib/lib/core/contract/chunks.js +37 -6
  19. package/dist-lib/lib/core/contract/features.d.ts +21 -0
  20. package/dist-lib/lib/core/contract/features.js +22 -0
  21. package/dist-lib/lib/core/contract/indexing.js +4 -2
  22. package/dist-lib/lib/core/contract/knowledge.js +5 -4
  23. package/dist-lib/lib/core/contract/members.js +2 -2
  24. package/dist-lib/lib/core/contract/projects.d.ts +5 -0
  25. package/dist-lib/lib/core/contract/projects.js +12 -4
  26. package/dist-lib/lib/core/contract/roles.js +30 -4
  27. package/dist-lib/lib/core/contract/run-requests.js +2 -2
  28. package/dist-lib/lib/core/contract/seed-defaults.js +2 -2
  29. package/dist-lib/lib/core/contract/tool-configs.js +3 -2
  30. package/dist-lib/lib/core/contract/workspace.d.ts +2 -0
  31. package/dist-lib/lib/core/contract/workspace.js +40 -7
  32. package/dist-lib/lib/core/contract/writers.js +2 -2
  33. package/dist-lib/lib/core/files/attachments.js +4 -1
  34. package/dist-lib/lib/core/local/index.d.ts +6 -2
  35. package/dist-lib/lib/core/local/index.js +12 -3
  36. package/dist-lib/lib/core/mesh.d.ts +5 -0
  37. package/dist-lib/lib/core/mesh.js +3 -0
  38. package/dist-lib/lib/core/migrations/workspace-from-0-21/index.d.ts +85 -0
  39. package/dist-lib/lib/core/migrations/workspace-from-0-21/index.js +690 -0
  40. package/dist-lib/lib/core/project-keys.d.ts +6 -0
  41. package/dist-lib/lib/core/project-keys.js +11 -0
  42. package/dist-lib/lib/core/public-mesh.d.ts +2 -0
  43. package/dist-lib/lib/core/public-mesh.js +5 -0
  44. package/dist-lib/lib/core/sync/changed.d.ts +4 -2
  45. package/dist-lib/lib/core/sync/changed.js +1 -1
  46. package/dist-lib/lib/core/sync/index.js +27 -2
  47. package/dist-lib/lib/core/workspace-upgrade.d.ts +6 -0
  48. package/dist-lib/lib/core/workspace-upgrade.js +26 -16
  49. package/dist-lib/lib/core.d.ts +4 -32
  50. package/dist-lib/lib/core.js +117 -469
  51. package/dist-lib/lib/run-transcript.d.ts +2 -1
  52. package/dist-lib/lib/run-transcript.js +11 -5
  53. package/dist-lib/schema/qvac.js +26 -1
  54. package/dist-lib/spec/mesh/hyperdb/db.json +15 -0
  55. package/dist-lib/spec/mesh/hyperdb/index.js +70 -1
  56. package/dist-lib/spec/mesh/hyperdb/messages.js +806 -438
  57. package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +18 -0
  58. package/dist-lib/spec/mesh/hyperdispatch/index.js +52 -1
  59. package/dist-lib/spec/mesh/hyperdispatch/messages.js +303 -3
  60. package/dist-lib/spec/mesh/hyperschema/index.js +303 -3
  61. package/dist-lib/spec/mesh/hyperschema/schema.json +146 -0
  62. package/dist-lib/spec/mesh/hyperschema/types.d.ts +39 -0
  63. package/package.json +6 -4
package/CHANGELOG.md CHANGED
@@ -13,6 +13,23 @@ 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
+
16
33
  ## [1.0.0-rc.12] - 2026-11-13
17
34
 
18
35
  Since 1.0.0-rc.11.
@@ -2386,7 +2403,8 @@ Published under the `release-0.1` dist-tag.
2386
2403
  await assistant.ready()
2387
2404
  ```
2388
2405
 
2389
- [Unreleased]: https://github.com/tetherto/qvac-core/compare/v1.0.0-rc.12...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
2390
2408
  [1.0.0-rc.12]: https://github.com/tetherto/qvac-core/compare/v1.0.0-rc.11...v1.0.0-rc.12
2391
2409
  [1.0.0-rc.11]: https://github.com/tetherto/qvac-core/compare/v0.11.4...v1.0.0-rc.11
2392
2410
  [0.11.4]: https://github.com/tetherto/qvac-core/compare/v0.11.3...v0.11.4
@@ -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 });
@@ -1,5 +1,6 @@
1
1
  import { CoreError } from '../errors.js';
2
2
  import { TERMINAL_RUN_STATES } from './contract/run-requests.js';
3
+ import { nextUpdatedAt } from './contract/chat.js';
3
4
  import { mustGetActiveAgent } from './agent.js';
4
5
  import { listChatGroupsIn, mustGetActiveChatGroup } from './chat-group.js';
5
6
  import { appendChunk, listChunks, updateOp } from './chunk.js';
@@ -132,8 +133,12 @@ async function patchChat(store, id, patch) {
132
133
  return patchChatRow(store, await mustGetActiveChat(store, id), patch);
133
134
  }
134
135
  async function patchChatRow(store, current, patch) {
135
- const updated = { ...current, ...patch, updatedAt: Date.now() };
136
- await store.dispatch('put-chat', { chat: updated });
136
+ const updated = {
137
+ ...current,
138
+ ...patch,
139
+ updatedAt: nextUpdatedAt(current.updatedAt, Date.now())
140
+ };
141
+ await store.dispatch('patch-chat', { chat: updated, fields: Object.keys(patch) });
137
142
  return enrichChat(store, updated);
138
143
  }
139
144
  export function renameChat(store, id, title) {
@@ -156,7 +161,11 @@ export async function switchChatAgent(store, id, { deviceId, agentId }) {
156
161
  if (current.agentId === agentId) {
157
162
  return enrichChat(store, current);
158
163
  }
159
- const switched = { ...current, agentId, updatedAt: Date.now() };
164
+ const switched = {
165
+ ...current,
166
+ agentId,
167
+ updatedAt: nextUpdatedAt(current.updatedAt, Date.now())
168
+ };
160
169
  const batch = store.batch();
161
170
  await appendChunk(store, {
162
171
  chatId: id,
@@ -164,7 +173,7 @@ export async function switchChatAgent(store, id, { deviceId, agentId }) {
164
173
  originDeviceId: deviceId,
165
174
  agentSwitch: { agentId, name: agent.name }
166
175
  }, batch);
167
- batch.dispatch('put-chat', { chat: switched });
176
+ batch.dispatch('patch-chat', { chat: switched, fields: ['agentId'] });
168
177
  await batch.flush();
169
178
  await cancelActiveRuns(store, id, {
170
179
  deviceId,
@@ -257,14 +266,18 @@ export async function moveChat(store, id, { groupId, afterId }) {
257
266
  ...chat,
258
267
  rank,
259
268
  rankedAt: Math.max(now, lastActivityAt),
260
- updatedAt: Math.max(now, chat.updatedAt ?? 0)
269
+ updatedAt: nextUpdatedAt(chat.updatedAt, now)
261
270
  });
262
271
  const batch = store.batch();
263
272
  for (const { chat, lastActivityAt, rank } of respaced) {
264
- batch.dispatch('put-chat', { chat: stamp(chat, lastActivityAt, rank) });
273
+ batch.dispatch('patch-chat', {
274
+ chat: stamp(chat, lastActivityAt, rank),
275
+ fields: ['rank', 'rankedAt']
276
+ });
265
277
  }
266
- batch.dispatch('put-chat', {
267
- chat: { ...stamp(moved.chat, moved.lastActivityAt, placement.rank), groupId: target }
278
+ batch.dispatch('patch-chat', {
279
+ chat: { ...stamp(moved.chat, moved.lastActivityAt, placement.rank), groupId: target },
280
+ fields: ['rank', 'rankedAt', 'groupId']
268
281
  });
269
282
  await batch.flush();
270
283
  return enrichChat(store, await mustGetActiveChat(store, id));
@@ -108,3 +108,4 @@ export declare const PROJECT_MEMBER_SEALS = "@qvac/project-member-seals";
108
108
  export declare const PROJECT_HOST_SEALS = "@qvac/project-host-seals";
109
109
  export declare const PROJECT_DEVICE_SEALS = "@qvac/project-device-seals";
110
110
  export declare const SKILL_SECRETS = "@qvac/skill-secrets";
111
+ export declare const MOVED_OUT_CHATS = "@qvac/moved-out-chats";
@@ -108,3 +108,4 @@ export const PROJECT_MEMBER_SEALS = '@qvac/project-member-seals';
108
108
  export const PROJECT_HOST_SEALS = '@qvac/project-host-seals';
109
109
  export const PROJECT_DEVICE_SEALS = '@qvac/project-device-seals';
110
110
  export const SKILL_SECRETS = '@qvac/skill-secrets';
111
+ export const MOVED_OUT_CHATS = '@qvac/moved-out-chats';
@@ -1,5 +1,5 @@
1
1
  import b4a from 'b4a';
2
- import { WORKSPACES_RULES, rulesVersion } from './mesh-rules.js';
2
+ import { FEATURES, isFeatureSupported } from './features.js';
3
3
  import { DEVICES, DEVICES_BY_WRITER_KEY, DEVICES_BY_MEMBER, LIVE_DEVICES_BY_MEMBER, LIVE_DEVICES_BY_WRITER_KEY, WRITER_ACTIVITY } from '../collections.js';
4
4
  export function trackActivity(handler) {
5
5
  return async (op, ctx) => {
@@ -23,7 +23,7 @@ export function liveDeviceForWriter(db, writerKey) {
23
23
  });
24
24
  }
25
25
  async function indexed(db) {
26
- return (await rulesVersion(db)) >= WORKSPACES_RULES;
26
+ return isFeatureSupported(db, FEATURES.SHIPPED_ROWS_INDEXED);
27
27
  }
28
28
  function ownerOf(device) {
29
29
  return device.memberId ?? device.claimedMemberId ?? null;
@@ -1,2 +1,3 @@
1
1
  import type { Router } from '../../../spec/mesh/hyperdispatch/index.js';
2
+ export declare const PATCHABLE_GROUP: readonly ["name", "rank"];
2
3
  export declare function registerChatGroupRoutes(router: Router): void;
@@ -1,5 +1,8 @@
1
1
  import { trackActivity } from './activity.js';
2
+ import { nextUpdatedAt, patched } from './chat.js';
3
+ import { FEATURES, isFeatureSupported } from './features.js';
2
4
  import { CHAT_GROUPS } from '../collections.js';
5
+ export const PATCHABLE_GROUP = ['name', 'rank'];
3
6
  export function registerChatGroupRoutes(router) {
4
7
  router.add('@qvac/put-chat-group', trackActivity(async ({ group }, { tx, log }) => {
5
8
  const existing = await tx.get(CHAT_GROUPS, { id: group.id });
@@ -7,7 +10,10 @@ export function registerChatGroupRoutes(router) {
7
10
  log?.debug('contract', 'put-chat-group dropped: tombstone is sticky', { id: group.id });
8
11
  return;
9
12
  }
10
- if (existing && !group.deletedAt && existing.updatedAt > group.updatedAt) {
13
+ if (existing &&
14
+ !group.deletedAt &&
15
+ existing.updatedAt > group.updatedAt &&
16
+ !(await isFeatureSupported(tx, FEATURES.KEEP_STALE_PUTS))) {
11
17
  log?.debug('contract', 'put-chat-group dropped: stale updatedAt', { id: group.id });
12
18
  return;
13
19
  }
@@ -17,4 +23,18 @@ export function registerChatGroupRoutes(router) {
17
23
  });
18
24
  return group;
19
25
  }));
26
+ router.add('@qvac/patch-chat-group', trackActivity(async ({ group, fields }, { tx, log }) => {
27
+ const existing = await tx.get(CHAT_GROUPS, { id: group.id });
28
+ if (existing?.deletedAt) {
29
+ log?.debug('contract', 'patch-chat-group dropped: group is deleted', { id: group.id });
30
+ return;
31
+ }
32
+ const row = {
33
+ ...patched(existing ?? group, { from: group, fields, patchable: PATCHABLE_GROUP }),
34
+ updatedAt: nextUpdatedAt(existing?.updatedAt, group.updatedAt)
35
+ };
36
+ await tx.insert(CHAT_GROUPS, row);
37
+ log?.debug('contract', 'chat group patched', { id: group.id });
38
+ return row;
39
+ }));
20
40
  }