@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
@@ -1,3 +1,4 @@
1
+ import HypercoreModule from 'hypercore';
1
2
  import b4a from 'b4a';
2
3
  import crypto from 'hypercore-crypto';
3
4
  import BlindPeering from 'blind-peering';
@@ -19,26 +20,27 @@ import { requireOurAdmin, requireWorkspace } from './api/members.js';
19
20
  import { projectName } from './api/projects.js';
20
21
  import { createServedApi } from './api/served.js';
21
22
  import { firstEvent } from './core/first-event.js';
22
- import { departedMembers, isWorkspace, lastAdminAmongOthers, liveMemberIds, readMemberKey, upgradeMembers } from './core/contract/workspace.js';
23
+ import { departedMembers, isWorkspace, lastAdminAmongOthers, readMemberKey } from './core/contract/workspace.js';
23
24
  import { readPublicSeed } from './core/contract/public-seeds.js';
24
25
  import { openSealedToDevice, sealToDevice } from './core/device-seal.js';
25
26
  import { removedBy } from './core/contract/invites.js';
26
27
  import { meshKind } from './core/contract/mesh-kind.js';
27
- import { WORKSPACES_RULES, bornUnderRules, rulesVersion } from './core/contract/mesh-rules.js';
28
- import { inProject, listedProject, listsMember, liveMember, projectMember, projectOfGroup, projectKeys, readProject, UNGROUPED } from './core/contract/projects.js';
28
+ import { bornUnderRules } from './core/contract/mesh-rules.js';
29
+ import { inProject, listedProject, listsMember, liveMember, projectMember, projectKeys, readProject } from './core/contract/projects.js';
29
30
  import { requireMesh } from './api/current-mesh.js';
30
31
  import { CoreError, isCoreError } from './errors.js';
31
32
  import { ChatMoves } from './core/chat-move.js';
32
33
  import { purgeCores } from './core/purge.js';
33
34
  import { copyContent } from './core/chat-copy.js';
35
+ import { isRecord } from './guards.js';
34
36
  import { followAgentCopies, keepCopyChats } from './core/agent-copies.js';
35
37
  import { cancelDeletedAgentRuns } from './core/deleted-agent-runs.js';
36
38
  import { devicesOfMember } from './core/contract/activity.js';
37
39
  import { followPickedAgents, followSharedAgents, ownerOf, liveSharedAgent, pickAgent, shareAgent, unpickAgent } from './core/shared-agents.js';
38
40
  import { inRole, publicRoute } from './core/contract/roles.js';
39
- import { freshPublicRecord, isPublicRecord, leftPublicRecordKey, derivedSeed, oldChats, ourRows, publicMeshKey, publicRecordKey, publicRecordsOf, publicSeed, servesAgents, servesChat, servesChunk } from './core/public-mesh.js';
41
+ import { freshPublicRecord, isPublicRecord, leftPublicRecordKey, derivedSeed, firstWorkspace, oldChats, ourRows, publicMeshKey, publicRecordKey, publicRecordsOf, publicSeed, servesAgents, servesChat, servesChunk } from './core/public-mesh.js';
40
42
  import { memberBoxKeyPair, openSealedToMember } from './core/member-key.js';
41
- import { dueProjectDeviceKeys, dueProjectHostKey, dueProjectKey, seedFingerprint, seedOpens } from './core/project-keys.js';
43
+ import { dueProjectHostKey, sealMembers, seedFingerprint, seedOpens } from './core/project-keys.js';
42
44
  import { projectDeviceKeys } from './core/contract/project-device-keys.js';
43
45
  import { agentJoin, freshHostRecord, hostRecordKey, hostState, hostTargets, isHostRecord } from './core/agent-hosting.js';
44
46
  import { hostForWriter } from './core/contract/model-requests.js';
@@ -53,9 +55,9 @@ import { decodeMeshInvite, encodeMeshInvite } from './core/invite.js';
53
55
  import { fileKeyOf, knownFile, referencedFile } from './core/files/local-index.js';
54
56
  import { openLocalStore, recordedRole } from './core/local/index.js';
55
57
  import { backgroundSyncKey } from './core/local/files.js';
56
- import { FOLD_PENDING, GROUPS_PENDING, agentsFoldKey, groupBuckets, groupRecordKey, isGroupRecord, liveGroups, projectsByGroup, upgradeCheckedKey, UPGRADE_RECORDS } from './core/workspace-upgrade.js';
58
+ import { UPGRADE_RECORDS } from './core/workspace-upgrade.js';
57
59
  import { convertedHere, groupRecordsByWorkspace, ifClosing, migratedWorkspace, oldMessages, recordsGroup, upgradedWorkspace } from './core/upgrade-summary.js';
58
- import { foldAgents, ownCopies } from './core/upgrade-agents.js';
60
+ import { WorkspaceUpgrade } from './core/migrations/workspace-from-0-21/index.js';
59
61
  import { MAX_MESH_NAME_CHARS, meshNameWithinBounds } from '../schema/qvac.js';
60
62
  import { getDefaultAgent, mustGetActiveAgent, rehomeAgents } from './core/agent.js';
61
63
  import { pickRunner } from './core/run-requests.js';
@@ -106,6 +108,8 @@ const DIAGNOSTICS_SAMPLE_MS = 2000;
106
108
  export const PEER_STREAM_TIMEOUT_MS = 10_000;
107
109
  const PEER_KEEP_ALIVE_MS = 3_000;
108
110
  const PUBLIC_FOLLOW_ATTEMPTS = 3;
111
+ const recoveryPeers = HypercoreModule;
112
+ recoveryPeers.setRecoveryPeers([]);
109
113
  export class Core extends ReadyResource {
110
114
  _storagePath;
111
115
  store;
@@ -214,9 +218,7 @@ export class Core extends ReadyResource {
214
218
  _wakeup;
215
219
  _following = new Map();
216
220
  _kickedProjects = new Map();
217
- _upgrading = new Set();
218
- _groupPasses = new WeakSet();
219
- _groupPassesDue = new WeakSet();
221
+ _upgrade = new WorkspaceUpgrade(this._upgradeHost());
220
222
  _purging = new Map();
221
223
  _publicIds = new Map();
222
224
  _publicBoots = new Map();
@@ -229,9 +231,6 @@ export class Core extends ReadyResource {
229
231
  _publicFollow = new ScopeLock({ debounce: true });
230
232
  _publicMerges = new Map();
231
233
  _publicDrops = new Map();
232
- _publicMigration = null;
233
- _copyAgain = false;
234
- _accountCopied = null;
235
234
  _agentCopies = new ScopeLock({ debounce: true });
236
235
  _sharedAgents = new ScopeLock({ debounce: true });
237
236
  _memberKeyWrites = new WeakMap();
@@ -452,6 +451,7 @@ export class Core extends ReadyResource {
452
451
  await writeAutoDownloadModels(this.local, this._autoDownloadModels);
453
452
  }
454
453
  const swarm = this._openSwarm(this._deviceKeyPair);
454
+ recoveryPeers.setRecoveryPeers([...this._resolveBlindPeerKeys()]);
455
455
  this._departures = new PendingDepartures(this.store, this.local, {
456
456
  swarm,
457
457
  isPooled: (key) => this._pooled(b4a.toHex(key)) !== null,
@@ -772,7 +772,7 @@ export class Core extends ReadyResource {
772
772
  if (mesh) {
773
773
  this._bringMeshOnline(mesh);
774
774
  }
775
- this._upgradeBoot = Promise.race([this._openUpgradable(), this._closeSignal]).catch((err) => {
775
+ this._upgradeBoot = Promise.race([this._upgrade.boot(), this._closeSignal]).catch((err) => {
776
776
  if (!this.closing) {
777
777
  this._log?.error(['p2p', 'mesh'], 'opening shared meshes to upgrade failed', err);
778
778
  }
@@ -1334,6 +1334,7 @@ export class Core extends ReadyResource {
1334
1334
  wakeup: this._wakeup,
1335
1335
  attachmentUrl: (attachment) => this._linkAttachment(attachment, mesh),
1336
1336
  fileIndex: this._fileIndexFor(() => mesh),
1337
+ history: this._upgrade.history(() => mesh),
1337
1338
  pendingUpdates: this._pendingUpdatesFor(() => mesh),
1338
1339
  sidebarChats: (limit) => this.local.listChatOrder(mesh.key, { limit }),
1339
1340
  bootFrom: session.bootFrom
@@ -2415,31 +2416,35 @@ export class Core extends ReadyResource {
2415
2416
  this._retiredAccount !== this._meshIdOf(account)) {
2416
2417
  return;
2417
2418
  }
2419
+ const minted = this._ownWorkspaceId;
2418
2420
  this._retiredAccount = null;
2419
2421
  this._retiredMeshes.clear();
2420
2422
  await this.local.restoreMesh(account.key);
2423
+ if (fresh && fresh !== account) {
2424
+ await this.local.archiveMesh(fresh.key, { homeTo: account.key });
2425
+ }
2421
2426
  await this._restorePublicWriters(account);
2422
2427
  await this._adoptAccount(account);
2423
2428
  if (fresh && fresh !== account) {
2424
- await this._dropOwnMint(fresh);
2425
- await this._teardownMesh(fresh, { purge: true });
2426
- }
2427
- }
2428
- async _dropOwnMint(fresh) {
2429
- const minted = new Set((await this.local.listMeshes()).filter((row) => row.initial).map((row) => b4a.toHex(row.key)));
2430
- for (const joined of await fresh.view.find(JOINED_MESHES).toArray()) {
2431
- const id = b4a.toHex(joined.meshKey);
2432
- if (!minted.has(id)) {
2433
- continue;
2434
- }
2435
- const own = this._publicMesh(id);
2436
- if (own) {
2437
- await this._teardownMesh(own, { purge: true });
2429
+ const own = minted ? this._publicMesh(minted) : null;
2430
+ const kept = [
2431
+ fresh.key,
2432
+ ...(minted ? [b4a.from(minted, 'hex')] : []),
2433
+ ...(own ? [own.key] : [])
2434
+ ];
2435
+ await this._keepVoided(fresh.key, kept);
2436
+ }
2437
+ }
2438
+ async _keepVoided(fresh, keys) {
2439
+ await this.local.markRetired(keys, fresh);
2440
+ for (const key of keys) {
2441
+ const pooled = this._pooled(b4a.toHex(key));
2442
+ if (pooled) {
2443
+ await this._retirePooledMesh(pooled);
2438
2444
  }
2439
- await this._teardownMesh(this._pooled(id) ?? (await this._openPooledMesh(id, 'leave')), {
2440
- purge: true
2441
- });
2445
+ this._retiredMeshes.add(b4a.toHex(key));
2442
2446
  }
2447
+ this._notifyStatus();
2443
2448
  }
2444
2449
  async _replaceRevokedAccount(mesh) {
2445
2450
  this._minting = true;
@@ -2503,9 +2508,7 @@ export class Core extends ReadyResource {
2503
2508
  }
2504
2509
  this._kickedProjects.set(b4a.toHex(row.key), null);
2505
2510
  const project = await this._heldMesh(row.key, 'leave');
2506
- await (project
2507
- ? this._departRevokedMesh(project, { purge: true })
2508
- : this._teardownMesh(row.key));
2511
+ await (project ? this._departRevokedMesh(project) : this._teardownMesh(row.key));
2509
2512
  }
2510
2513
  }
2511
2514
  async _departRevokedMesh(mesh, opts = {}) {
@@ -2603,7 +2606,7 @@ export class Core extends ReadyResource {
2603
2606
  let pooled = this._pooled(meshId);
2604
2607
  if (this._isHidden(meshId, pooled?.workspaceKey)) {
2605
2608
  if (pooled) {
2606
- await this._teardownMesh(pooled, { depart: true, purge: true });
2609
+ await this._teardownMesh(pooled, { depart: true });
2607
2610
  }
2608
2611
  else {
2609
2612
  await this._dropKickedCopy(meshId);
@@ -2891,336 +2894,6 @@ export class Core extends ReadyResource {
2891
2894
  this._log?.warn(['p2p', 'mesh'], 'recording the joined mesh failed', error);
2892
2895
  }
2893
2896
  }
2894
- async _followUpgrade(mesh) {
2895
- const meshId = this._meshIdOf(mesh);
2896
- if (this.closing || this._upgrading.has(meshId)) {
2897
- return;
2898
- }
2899
- this._upgrading.add(meshId);
2900
- try {
2901
- let kind = await meshKind(mesh.view);
2902
- if (kind === 'shared' && (await this._upgradable(mesh))) {
2903
- await mesh.dispatch('upgrade-to-workspace', { upgradedAt: Date.now() });
2904
- kind = await meshKind(mesh.view);
2905
- }
2906
- if (!this._isPooled(mesh)) {
2907
- return;
2908
- }
2909
- if (kind === 'workspace' && mesh.writable) {
2910
- await this.local.set(upgradeCheckedKey(mesh.key), GROUPS_PENDING);
2911
- await this._markFoldDue(mesh.key);
2912
- await this._reopenInRole(mesh, 'workspace');
2913
- await this._markJoinedWorkspace(mesh.key);
2914
- }
2915
- else if (kind !== 'project' && (kind !== 'shared' || (await bornUnderRules(mesh.view)))) {
2916
- await this._markUpgradeChecked(mesh.key);
2917
- }
2918
- }
2919
- catch (err) {
2920
- if (!this.closing && !mesh.closing) {
2921
- this._log?.warn(['p2p', 'mesh'], 'upgrading a shared mesh to a workspace failed', err);
2922
- }
2923
- }
2924
- finally {
2925
- this._upgrading.delete(meshId);
2926
- }
2927
- }
2928
- _followGroups(workspace) {
2929
- if (this.closing || !workspace.writable) {
2930
- return;
2931
- }
2932
- if (this._groupPasses.has(workspace)) {
2933
- this._groupPassesDue.add(workspace);
2934
- return;
2935
- }
2936
- this._groupPasses.add(workspace);
2937
- void this._passGroups(workspace)
2938
- .catch((err) => {
2939
- if (!this.closing && this._isPooled(workspace)) {
2940
- this._log?.warn(['p2p', 'mesh'], 'moving chat-groups into projects failed', err);
2941
- }
2942
- })
2943
- .finally(() => {
2944
- this._groupPasses.delete(workspace);
2945
- if (this._groupPassesDue.delete(workspace)) {
2946
- this._followGroups(workspace);
2947
- }
2948
- });
2949
- }
2950
- async _passGroups(workspace) {
2951
- const checked = await this.local.get(upgradeCheckedKey(workspace.key));
2952
- if (checked === true) {
2953
- return this._passAgents(workspace);
2954
- }
2955
- if (checked !== GROUPS_PENDING) {
2956
- return;
2957
- }
2958
- const ours = workspace.ourMemberId ?? (await workspace.refreshOurMember())?.memberId;
2959
- if (!ours || !(await isWorkspace(workspace.view))) {
2960
- return;
2961
- }
2962
- const buckets = await this._groupBuckets(workspace);
2963
- const rows = await projectsByGroup(workspace.view);
2964
- const passes = await Promise.allSettled(buckets.map((bucket) => this._passGroup(workspace, ours, { bucket, row: rows.get(bucket.groupId) ?? null })));
2965
- const settled = [];
2966
- for (const pass of passes) {
2967
- if (pass.status === 'rejected') {
2968
- throw pass.reason;
2969
- }
2970
- settled.push(pass.value);
2971
- }
2972
- if (settled.every((state) => state === 'done') && this._isPooled(workspace)) {
2973
- await this.local.set(upgradeCheckedKey(workspace.key), true);
2974
- }
2975
- if (!settled.includes('pending')) {
2976
- await this._passAgents(workspace);
2977
- }
2978
- }
2979
- async _groupBuckets(workspace) {
2980
- const named = workspace.name?.name?.trim() || 'Workspace';
2981
- const groups = await liveGroups(workspace.view);
2982
- const ids = [...groups.map((group) => group.id), UNGROUPED];
2983
- const records = await Promise.all(ids.map((id) => this._groupRecord(workspace, id)));
2984
- if (!records.every((record) => record.copied || record.done)) {
2985
- return this._scanBuckets(workspace, named);
2986
- }
2987
- return ids.map((groupId, idx) => ({
2988
- groupId,
2989
- name: groups[idx]?.name ?? named,
2990
- chats: new Set()
2991
- }));
2992
- }
2993
- async _markFoldDue(workspaceKey) {
2994
- const key = agentsFoldKey(workspaceKey);
2995
- if ((await this.local.get(key)) !== true) {
2996
- await this.local.set(key, FOLD_PENDING);
2997
- }
2998
- }
2999
- async _passAgents(workspace) {
3000
- if ((await this.local.get(agentsFoldKey(workspace.key))) !== FOLD_PENDING) {
3001
- return;
3002
- }
3003
- const copies = await ownCopies(workspace, this._requireDeviceKeyPair().publicKey);
3004
- if (copies.length > 0) {
3005
- const account = await this._writableAccount();
3006
- const projects = await this._heldProjects(workspace);
3007
- if (!account || !projects) {
3008
- return;
3009
- }
3010
- await foldAgents({ account, workspace, projects, picks: this._agentPicks() }, copies);
3011
- }
3012
- if (this._isPooled(workspace)) {
3013
- await this.local.set(agentsFoldKey(workspace.key), true);
3014
- }
3015
- }
3016
- async _heldProjects(workspace) {
3017
- const ours = workspace.ourMemberId;
3018
- const held = [];
3019
- for (const row of await workspace.view.find(PROJECTS).toArray()) {
3020
- if (row.deletedAt || !ours || !listedProject(row, ours))
3021
- continue;
3022
- const project = await this._heldMesh(row.id);
3023
- if (!project?.writable) {
3024
- return null;
3025
- }
3026
- held.push(project);
3027
- }
3028
- return held;
3029
- }
3030
- async _groupRecord(workspace, groupId) {
3031
- const stored = await this.local.get(groupRecordKey(workspace.key, groupId));
3032
- return isGroupRecord(stored) ? stored : {};
3033
- }
3034
- async _scanBuckets(workspace, named) {
3035
- const buckets = await groupBuckets(workspace.view, named);
3036
- const ungrouped = buckets.some((bucket) => bucket.groupId === UNGROUPED);
3037
- if (!ungrouped && !(await this._groupRecord(workspace, UNGROUPED)).key) {
3038
- await this.local.set(groupRecordKey(workspace.key, UNGROUPED), { done: true });
3039
- }
3040
- return buckets;
3041
- }
3042
- async _passGroup(workspace, ours, { bucket, row: listed }) {
3043
- const recordKey = groupRecordKey(workspace.key, bucket.groupId);
3044
- let record = await this._groupRecord(workspace, bucket.groupId);
3045
- if (record.done) {
3046
- return 'done';
3047
- }
3048
- let row = listed;
3049
- if (!row) {
3050
- record = await this._foundGroupProject(workspace, bucket, { recordKey, record });
3051
- row = await projectOfGroup(workspace.view, bucket.groupId);
3052
- }
3053
- const live = !!row && !row.deletedAt;
3054
- if (row && live && record.key === b4a.toHex(row.id)) {
3055
- await this._listNewMembers(workspace, { row, recordKey, record });
3056
- return 'won';
3057
- }
3058
- if (row && live && !(await this._mergeGroupInto(workspace, ours, { row, bucket, record }))) {
3059
- return 'pending';
3060
- }
3061
- if (record.key) {
3062
- await this._purgeProject(b4a.from(record.key, 'hex'));
3063
- }
3064
- await this.local.set(recordKey, { ...record, done: true });
3065
- return 'done';
3066
- }
3067
- async _foundGroupProject(workspace, bucket, { recordKey, record }) {
3068
- const held = record.key ? await this._heldMesh(b4a.from(record.key, 'hex')) : null;
3069
- const name = projectName(bucket.name.trim() || 'Project');
3070
- const project = held ?? (await this._openNewProject(workspace, { name }));
3071
- let next = { ...record, key: this._meshIdOf(project) };
3072
- await this.local.set(recordKey, next);
3073
- await this._addWorkspaceMembers(workspace, project);
3074
- if (!next.copied) {
3075
- await this._copyGroup(workspace, project, bucket);
3076
- next = { ...next, copied: true };
3077
- await this.local.set(recordKey, next);
3078
- }
3079
- const now = Date.now();
3080
- await this._listProject(workspace, project, {
3081
- createdAt: now,
3082
- updatedAt: now,
3083
- groupId: bucket.groupId
3084
- });
3085
- this._notifyStatus();
3086
- return next;
3087
- }
3088
- async _mergeGroupInto(workspace, ours, { row, bucket, record }) {
3089
- const winner = listsMember(row, ours) ? await this._winnerOf(workspace, row) : null;
3090
- if (!winner) {
3091
- return false;
3092
- }
3093
- await this._addWorkspaceMembers(workspace, winner);
3094
- await this._listWorkspaceMembers(workspace, row);
3095
- const own = record.key ? await this._heldMesh(b4a.from(record.key, 'hex')) : null;
3096
- if (own) {
3097
- await copyContent({
3098
- from: own,
3099
- into: winner,
3100
- deviceId: this._requireDeviceKeyPair().publicKey
3101
- });
3102
- }
3103
- await this._copyGroup(workspace, winner, bucket);
3104
- this._log?.info(['p2p', 'mesh'], 'merged a chat-group into the project that won it', {
3105
- project: b4a.toHex(row.id).slice(0, 8)
3106
- });
3107
- return true;
3108
- }
3109
- async _winnerOf(workspace, row) {
3110
- const projectId = b4a.toHex(row.id);
3111
- if (!this._pooled(projectId) && !(await this.local.readMeshSession(row.id))) {
3112
- await this._followProjects(workspace);
3113
- await this._following.get(projectId);
3114
- }
3115
- const winner = this._pooled(projectId) ?? (await this._heldMesh(row.id));
3116
- return winner?.writable ? winner : null;
3117
- }
3118
- async _copyGroup(workspace, project, { chats }) {
3119
- const deviceId = this._requireDeviceKeyPair().publicKey;
3120
- await copyContent({ from: workspace, into: project, deviceId, chats, endsOpenRuns: true });
3121
- }
3122
- async _addWorkspaceMembers(workspace, project) {
3123
- for (const memberId of await this._workspaceMembers(workspace)) {
3124
- if (!(await inProject(project.view, memberId))) {
3125
- await project.dispatch('put-project-member', { memberId });
3126
- }
3127
- }
3128
- }
3129
- async _listNewMembers(workspace, { row, recordKey, record }) {
3130
- const listed = new Set(record.listedMembers ?? []);
3131
- const members = await this._workspaceMembers(workspace);
3132
- const added = members.filter((id) => !listed.has(b4a.toHex(id)));
3133
- const project = added.length > 0 ? await this._heldMesh(row.id) : null;
3134
- for (const memberId of added) {
3135
- if (project && !(await inProject(project.view, memberId))) {
3136
- await project.dispatch('put-project-member', { memberId });
3137
- }
3138
- if (!listsMember(row, memberId)) {
3139
- await workspace.dispatch('set-project-listing', { projectId: row.id, memberId });
3140
- }
3141
- }
3142
- if (added.length > 0 && this._isPooled(workspace)) {
3143
- const listedMembers = [...listed, ...added.map((id) => b4a.toHex(id))];
3144
- await this.local.set(recordKey, { ...record, listedMembers });
3145
- }
3146
- }
3147
- async _listWorkspaceMembers(workspace, row) {
3148
- for (const memberId of await this._workspaceMembers(workspace)) {
3149
- if (!listsMember(row, memberId)) {
3150
- await workspace.dispatch('set-project-listing', { projectId: row.id, memberId });
3151
- }
3152
- }
3153
- }
3154
- async _workspaceMembers(workspace) {
3155
- const live = await liveMemberIds(workspace.view);
3156
- return (await upgradeMembers(workspace.view)).filter((id) => live.has(b4a.toHex(id)));
3157
- }
3158
- async _openUpgradable() {
3159
- const account = await this._storedAccountKey();
3160
- if (this.closing) {
3161
- return;
3162
- }
3163
- for (const row of await this.local.listMeshes()) {
3164
- if (this.closing) {
3165
- return;
3166
- }
3167
- const skip = row.workspaceKey || (account && b4a.equals(row.key, account));
3168
- const role = recordedRole(row);
3169
- if (skip || (role !== 'project' && role !== 'workspace')) {
3170
- continue;
3171
- }
3172
- const checked = await this.local.get(upgradeCheckedKey(row.key));
3173
- if (this.closing) {
3174
- return;
3175
- }
3176
- const due = role === 'workspace'
3177
- ? checked === GROUPS_PENDING ||
3178
- (await this.local.get(agentsFoldKey(row.key))) === FOLD_PENDING
3179
- : checked !== true;
3180
- if (this.closing) {
3181
- return;
3182
- }
3183
- if (!due) {
3184
- continue;
3185
- }
3186
- await this._openPooledMesh(b4a.toHex(row.key)).catch((err) => {
3187
- if (!this.closing) {
3188
- this._log?.warn(['p2p', 'mesh'], 'opening a shared mesh to upgrade it failed', err);
3189
- }
3190
- });
3191
- }
3192
- }
3193
- async _markUpgradeChecked(meshKey) {
3194
- const key = upgradeCheckedKey(meshKey);
3195
- if ((await this.local.get(key)) !== true) {
3196
- await this.local.set(key, true);
3197
- }
3198
- }
3199
- async _upgradable(mesh) {
3200
- if (!mesh.writable || (await rulesVersion(mesh.view)) < WORKSPACES_RULES) {
3201
- return false;
3202
- }
3203
- return !(await bornUnderRules(mesh.view));
3204
- }
3205
- async _markJoinedWorkspace(meshKey) {
3206
- const registry = await this._writableAccount();
3207
- const joined = await registry?.view.get(JOINED_MESHES, { meshKey });
3208
- if (!registry || !joined || joined.workspace || joined.leftAt) {
3209
- return;
3210
- }
3211
- await registry.dispatch('put-joined-mesh', {
3212
- room: {
3213
- meshKey,
3214
- memberId: joined.memberId,
3215
- joinedAt: joined.joinedAt,
3216
- label: joined.label,
3217
- labeledAt: joined.labeledAt,
3218
- shared: joined.shared,
3219
- sharedAt: joined.sharedAt,
3220
- workspace: true
3221
- }
3222
- });
3223
- }
3224
2897
  async _reopenInRole(mesh, role) {
3225
2898
  await this.local.setMeshRole(mesh.key, role);
3226
2899
  await this._retirePooledMesh(mesh);
@@ -3463,14 +3136,14 @@ export class Core extends ReadyResource {
3463
3136
  }
3464
3137
  _onSharedWritable(mesh) {
3465
3138
  if (mesh.role === 'project') {
3466
- void this._followUpgrade(mesh);
3139
+ void this._upgrade.followShared(mesh);
3467
3140
  void this._followSharedAgents();
3468
3141
  void this._workspaceOf(mesh)
3469
- .then((workspace) => workspace && this._followGroups(workspace))
3142
+ .then((workspace) => workspace && this._upgrade.follow(workspace))
3470
3143
  .catch(noop);
3471
3144
  }
3472
3145
  else if (mesh.role === 'workspace') {
3473
- this._followGroups(mesh);
3146
+ this._upgrade.follow(mesh);
3474
3147
  void this._followSharedAgents();
3475
3148
  this._shareMemberKey(mesh);
3476
3149
  void this._followHosting();
@@ -3487,8 +3160,10 @@ export class Core extends ReadyResource {
3487
3160
  void this._followPublicMesh(mesh);
3488
3161
  void this._followSkillSecrets();
3489
3162
  }
3490
- if (this._isOwnPublic(mesh)) {
3163
+ const account = this._accountMesh();
3164
+ if (this._isOwnPublic(mesh) && account) {
3491
3165
  void this._followAgentCopies();
3166
+ this._upgrade.copyAccount(account);
3492
3167
  }
3493
3168
  this._notifyStatus();
3494
3169
  }
@@ -3529,6 +3204,9 @@ export class Core extends ReadyResource {
3529
3204
  entry.handlers = handlers;
3530
3205
  mesh.on('kick-dirty', () => this._reviewKick(mesh).catch((err) => this._log?.warn(['p2p', 'mesh'], 'reviewing a kick failed', err)));
3531
3206
  mesh.on('unattested-device', () => void this._settleMembership(mesh));
3207
+ this._chatMoves
3208
+ ?.followSource(mesh)
3209
+ .catch((err) => this._log?.warn('chat-move', 'following a moved chat source failed', { err }));
3532
3210
  if (mesh.writable) {
3533
3211
  this._onMeshSeedDue(mesh);
3534
3212
  this._onProjectWritable(mesh);
@@ -3571,6 +3249,8 @@ export class Core extends ReadyResource {
3571
3249
  void this._followAccountName(mesh);
3572
3250
  });
3573
3251
  mesh.on('meshes-dirty', (changed, groupsChanged = false, chatsChanged = true) => this._onMeshesDirty(entry, { changed, groupsChanged, chatsChanged }));
3252
+ mesh.on('files-pending', () => void this._upgrade.refill(mesh).catch(noop));
3253
+ mesh.on('landed', () => void this._upgrade.refill(mesh).catch(noop));
3574
3254
  mesh.on('joined', () => {
3575
3255
  if (entry.meshesPending) {
3576
3256
  void this._followJoinedMeshes(mesh);
@@ -3615,18 +3295,17 @@ export class Core extends ReadyResource {
3615
3295
  void this._followHosting();
3616
3296
  }
3617
3297
  if (mesh.role === 'project') {
3618
- void this._followUpgrade(mesh);
3298
+ void this._upgrade.followShared(mesh);
3619
3299
  }
3620
- if ((changed || groupsChanged) && mesh.role === 'workspace') {
3621
- this._followGroups(mesh);
3300
+ if ((changed || groupsChanged || chatsChanged) && mesh.role === 'workspace') {
3301
+ this._upgrade.follow(mesh);
3622
3302
  }
3623
3303
  const opening = this._ownWorkspaceId !== null && !this._publicMesh();
3624
3304
  if (this._isAccount(mesh) && (changed || entry.publicPending || opening)) {
3625
3305
  void this._followPublicMesh(mesh);
3626
3306
  }
3627
- const copying = chatsChanged || this._accountCopied === null;
3628
- if (copying && this._isAccount(mesh) && this._ownWorkspaceId && this._publicMesh()) {
3629
- this._migrateIntoPublic(mesh, b4a.from(this._ownWorkspaceId, 'hex'));
3307
+ if ((chatsChanged || this._upgrade.accountUnread) && this._isAccount(mesh)) {
3308
+ this._upgrade.copyAccount(mesh);
3630
3309
  }
3631
3310
  }
3632
3311
  _listenMesh(mesh, handlers) {
@@ -4043,9 +3722,7 @@ export class Core extends ReadyResource {
4043
3722
  else if (key && !b4a.equals(key, held.key)) {
4044
3723
  this._mergeLostMint(account, held, key);
4045
3724
  }
4046
- if (workspaceId === this._ownWorkspaceId) {
4047
- this._migrateIntoPublic(account, workspaceKey);
4048
- }
3725
+ this._upgrade.copyAccount(account);
4049
3726
  }
4050
3727
  async _publicRecord(account, workspaceKey) {
4051
3728
  const value = await this.local.get(publicRecordKey(account.key, workspaceKey));
@@ -4125,7 +3802,7 @@ export class Core extends ReadyResource {
4125
3802
  }
4126
3803
  this._publicIds.set(workspaceId, this._meshIdOf(winner));
4127
3804
  this._notifyStatus();
4128
- await this._publicMigration;
3805
+ await this._upgrade.accountCopy;
4129
3806
  await copyContent({
4130
3807
  from: lost,
4131
3808
  into: winner,
@@ -4150,77 +3827,6 @@ export class Core extends ReadyResource {
4150
3827
  });
4151
3828
  this._publicMerges.set(workspaceId, merge);
4152
3829
  }
4153
- _migrateIntoPublic(account, workspaceKey) {
4154
- if (this._publicMigration) {
4155
- this._copyAgain = true;
4156
- return;
4157
- }
4158
- this._publicMigration = (async () => {
4159
- const into = this._publicMesh(b4a.toHex(workspaceKey));
4160
- const workspace = into && (await this._heldMesh(workspaceKey));
4161
- if (!into || !workspace?.minted) {
4162
- return;
4163
- }
4164
- const chats = await oldChats(account);
4165
- const copied = `${this._meshIdOf(account)}:${chats?.mark}`;
4166
- if (this._accountCopied === copied) {
4167
- return;
4168
- }
4169
- if (!chats) {
4170
- await this._recordCopied(account, workspaceKey);
4171
- this._accountCopied = copied;
4172
- return;
4173
- }
4174
- await into.waitForWritable();
4175
- if (!this._isAccount(account) || !this._isPooled(into)) {
4176
- return;
4177
- }
4178
- await copyContent({
4179
- from: account,
4180
- into,
4181
- deviceId: this._requireDeviceKeyPair().publicKey,
4182
- endsOpenRuns: true
4183
- });
4184
- await this._recordCopied(account, workspaceKey);
4185
- if (await this._markCopied(account, workspaceKey)) {
4186
- this._accountCopied = copied;
4187
- }
4188
- this._log?.info(['p2p', 'mesh'], 'copied the account into its public mesh', {
4189
- mesh: this._meshIdOf(into).slice(0, 8)
4190
- });
4191
- })()
4192
- .catch((err) => {
4193
- if (!this.closing) {
4194
- this._log?.warn(['p2p', 'mesh'], 'copying the account into its public mesh failed', err);
4195
- }
4196
- })
4197
- .finally(() => {
4198
- this._publicMigration = null;
4199
- if (this._copyAgain && !this.closing) {
4200
- this._copyAgain = false;
4201
- this._migrateIntoPublic(account, workspaceKey);
4202
- }
4203
- });
4204
- }
4205
- async _recordCopied(account, workspaceKey) {
4206
- const record = await this._publicRecord(account, workspaceKey);
4207
- if (record && !record.migrated) {
4208
- await this._savePublicRecord(account, workspaceKey, { ...record, migrated: true });
4209
- }
4210
- }
4211
- async _markCopied(account, workspaceKey) {
4212
- const row = await account.view.get(JOINED_MESHES, { meshKey: workspaceKey });
4213
- if (!row || !account.writable) {
4214
- return false;
4215
- }
4216
- if (!row.initial) {
4217
- const { meshKey, memberId, joinedAt } = row;
4218
- await account.dispatch('put-joined-mesh', {
4219
- room: { meshKey, memberId, joinedAt, initial: true }
4220
- });
4221
- }
4222
- return true;
4223
- }
4224
3830
  async _followAgentCopies(agentId) {
4225
3831
  if (!agentId && !(await this._agentCopies.lock()))
4226
3832
  return;
@@ -4482,7 +4088,7 @@ export class Core extends ReadyResource {
4482
4088
  await this._rotatePublicWriters(account);
4483
4089
  }
4484
4090
  async _dropKickedCopy(meshId) {
4485
- await this._teardownMesh(await this._openPooledMesh(meshId, 'leave'), { purge: true });
4091
+ await this._teardownMesh(this._pooled(meshId) ?? b4a.from(meshId, 'hex'));
4486
4092
  }
4487
4093
  async _rotatePublicWriters(account) {
4488
4094
  for (const workspaceKey of await this._publicWorkspaces(account)) {
@@ -4527,6 +4133,45 @@ export class Core extends ReadyResource {
4527
4133
  }
4528
4134
  }
4529
4135
  }
4136
+ _upgradeHost() {
4137
+ const core = this;
4138
+ return {
4139
+ get closing() {
4140
+ return core.closing;
4141
+ },
4142
+ get local() {
4143
+ return core.local;
4144
+ },
4145
+ get log() {
4146
+ return core._log;
4147
+ },
4148
+ get ownWorkspaceId() {
4149
+ return core._ownWorkspaceId;
4150
+ },
4151
+ accountMesh: () => this._accountMesh(),
4152
+ agentPicks: () => this._agentPicks(),
4153
+ followProjects: (workspace) => this._followProjects(workspace),
4154
+ following: (meshId) => this._following.get(meshId),
4155
+ heldMesh: (key) => this._heldMesh(key),
4156
+ isAccount: (mesh) => this._isAccount(mesh),
4157
+ isPooled: (mesh) => this._isPooled(mesh),
4158
+ listProject: (workspace, project, listing) => this._listProject(workspace, project, listing),
4159
+ meshIdOf: (mesh) => this._meshIdOf(mesh),
4160
+ notifyStatus: () => this._notifyStatus(),
4161
+ openNewProject: (workspace, project) => this._openNewProject(workspace, project),
4162
+ openPooledMesh: (meshId) => this._openPooledMesh(meshId),
4163
+ pooled: (meshId) => this._pooled(meshId),
4164
+ publicMesh: (workspaceId) => this._publicMesh(workspaceId),
4165
+ publicRecord: (account, workspaceKey) => this._publicRecord(account, workspaceKey),
4166
+ publicWriter: (mesh) => this._publicWriters.has(mesh),
4167
+ purgeProject: (key) => this._purgeProject(key),
4168
+ reopenInRole: (mesh, role) => this._reopenInRole(mesh, role),
4169
+ requireDeviceKeyPair: () => this._requireDeviceKeyPair(),
4170
+ savePublicRecord: (account, workspaceKey, record) => this._savePublicRecord(account, workspaceKey, record),
4171
+ storedAccountKey: () => this._storedAccountKey(),
4172
+ writableAccount: () => this._writableAccount()
4173
+ };
4174
+ }
4530
4175
  _accountMesh() {
4531
4176
  return this._pooled(this._accountId);
4532
4177
  }
@@ -5463,13 +5108,13 @@ export class Core extends ReadyResource {
5463
5108
  }
5464
5109
  async _listProject(workspace, project, listing) {
5465
5110
  const batch = workspace.batch();
5466
- const listed = { id: project.key, ...listing, seedFingerprint: seedFingerprint(project.seed) };
5111
+ const { memberIds, ...fields } = listing;
5112
+ const listed = { id: project.key, ...fields, seedFingerprint: seedFingerprint(project.seed) };
5467
5113
  batch.dispatch('put-project', { project: listed });
5468
5114
  const ours = workspace.ourMemberId;
5469
- const sealing = ours && { projectId: project.key, seed: project.seed, sealedBy: ours };
5470
- const own = sealing ? await dueProjectKey(workspace.view, ours, sealing) : null;
5471
- if (own) {
5472
- batch.dispatch('put-project-key', own);
5115
+ if (ours) {
5116
+ const row = { ...listed, memberIds: memberIds ?? [ours] };
5117
+ await sealMembers(batch, { view: workspace.view, row, seed: project.seed, sealedBy: ours });
5473
5118
  }
5474
5119
  await batch.flush();
5475
5120
  }
@@ -6473,8 +6118,23 @@ export class Core extends ReadyResource {
6473
6118
  }
6474
6119
  });
6475
6120
  }
6121
+ await this._dropUnlistedFollows(workspace);
6476
6122
  void this._sealProjectKeys();
6477
6123
  }
6124
+ async _dropUnlistedFollows(workspace) {
6125
+ for (const row of await this.local.listMeshes()) {
6126
+ const meshId = b4a.toHex(row.key);
6127
+ if (row.role !== 'project' ||
6128
+ !row.awaitingAdmission ||
6129
+ !row.workspaceKey ||
6130
+ !b4a.equals(row.workspaceKey, workspace.key) ||
6131
+ this._following.has(meshId) ||
6132
+ (await readProject(workspace.view, row.key))) {
6133
+ continue;
6134
+ }
6135
+ await this._trackFollow(meshId, () => this._purgeProject(row.key));
6136
+ }
6137
+ }
6478
6138
  _mayFollowProjects(workspace) {
6479
6139
  const leaving = this._leaving !== null &&
6480
6140
  (this._leaving === this._accountId || this._leaving === this._meshIdOf(workspace));
@@ -6521,19 +6181,7 @@ export class Core extends ReadyResource {
6521
6181
  return;
6522
6182
  }
6523
6183
  const batch = workspace.batch();
6524
- for (const memberId of row.memberIds ?? []) {
6525
- const due = await dueProjectKey(workspace.view, memberId, {
6526
- projectId: row.id,
6527
- seed,
6528
- sealedBy
6529
- });
6530
- if (due) {
6531
- batch.dispatch('put-project-key', due);
6532
- }
6533
- }
6534
- for (const due of await dueProjectDeviceKeys(workspace.view, row, { seed, sealedBy })) {
6535
- batch.dispatch('put-project-device-key', due);
6536
- }
6184
+ await sealMembers(batch, { view: workspace.view, row, seed, sealedBy });
6537
6185
  await batch.flush();
6538
6186
  }
6539
6187
  async _sealProjectHosts(workspace, projectId, seed) {