@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
@@ -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';
@@ -71,7 +73,7 @@ import { claimOwnMembership, legacyNameStamp, namedAt, renameFoundedMember } fro
71
73
  import { runMigrations } from './core/migrations/index.js';
72
74
  import { backfillFileIndex } from './core/migrations/file-index.js';
73
75
  import { REINDEX_FROM_APP_VERSION, backfillChatChunks, floorExcludesBefore } from './core/migrations/chat-chunks.js';
74
- import { REINDEX_WRITERS_FROM_APP_VERSION, backfillWriterLinks } from './core/migrations/writer-links.js';
76
+ import { backfillWriterLinks } from './core/migrations/writer-links.js';
75
77
  import { advertiseDeviceVersion, retractInventory } from './core/devices.js';
76
78
  import { DeviceControl } from './core/device-control.js';
77
79
  import { forwardToDevices, servedToSiblings } from './api/device-routes.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,
@@ -746,7 +746,8 @@ export class Core extends ReadyResource {
746
746
  const mesh = await this._switchToMesh({
747
747
  seed: this._meshSeed,
748
748
  baseKey: this._meshKey,
749
- account: true
749
+ account: true,
750
+ boot: true
750
751
  });
751
752
  }
752
753
  catch (err) {
@@ -771,7 +772,7 @@ export class Core extends ReadyResource {
771
772
  if (mesh) {
772
773
  this._bringMeshOnline(mesh);
773
774
  }
774
- this._upgradeBoot = Promise.race([this._openUpgradable(), this._closeSignal]).catch((err) => {
775
+ this._upgradeBoot = Promise.race([this._upgrade.boot(), this._closeSignal]).catch((err) => {
775
776
  if (!this.closing) {
776
777
  this._log?.error(['p2p', 'mesh'], 'opening shared meshes to upgrade failed', err);
777
778
  }
@@ -942,7 +943,6 @@ export class Core extends ReadyResource {
942
943
  }
943
944
  for (const { mesh } of this._meshes.values()) {
944
945
  void this._backfillChatChunks(mesh);
945
- void this._backfillWriterLinks(mesh);
946
946
  }
947
947
  return;
948
948
  }
@@ -1266,13 +1266,18 @@ export class Core extends ReadyResource {
1266
1266
  throw halted;
1267
1267
  }
1268
1268
  await this._adoptAccount(mesh);
1269
+ const late = mesh.interrupted;
1270
+ if (late) {
1271
+ this._withdrawFromMesh(mesh, late);
1272
+ throw late;
1273
+ }
1269
1274
  return mesh;
1270
1275
  }
1271
1276
  async _openMesh(opts = {}) {
1272
1277
  const session = await this.local.resolveSession(this._requireDeviceKeyPair().publicKey, opts);
1273
1278
  return this._openMeshSession(session, opts);
1274
1279
  }
1275
- async _openMeshSession(session, { blindPeerKeys, account, initial, sharing, workspace, project } = {}) {
1280
+ async _openMeshSession(session, { blindPeerKeys, account, initial, sharing, workspace, project, boot } = {}) {
1276
1281
  this._refuseClosing();
1277
1282
  const deviceId = this._requireDeviceKeyPair().publicKey;
1278
1283
  const mesh = this._newMesh(session, session.role ?? (account ? 'account' : 'project'), blindPeerKeys);
@@ -1297,7 +1302,8 @@ export class Core extends ReadyResource {
1297
1302
  initial,
1298
1303
  sharing,
1299
1304
  workspace,
1300
- project
1305
+ project,
1306
+ boot
1301
1307
  });
1302
1308
  }
1303
1309
  this._refuseClosing();
@@ -1328,13 +1334,19 @@ export class Core extends ReadyResource {
1328
1334
  wakeup: this._wakeup,
1329
1335
  attachmentUrl: (attachment) => this._linkAttachment(attachment, mesh),
1330
1336
  fileIndex: this._fileIndexFor(() => mesh),
1337
+ history: this._upgrade.history(() => mesh),
1331
1338
  pendingUpdates: this._pendingUpdatesFor(() => mesh),
1332
1339
  sidebarChats: (limit) => this.local.listChatOrder(mesh.key, { limit }),
1333
1340
  bootFrom: session.bootFrom
1334
1341
  });
1335
1342
  return mesh;
1336
1343
  }
1337
- async _appendOwnBirth(mesh, session, { deviceId, account, initial, sharing, workspace, project }) {
1344
+ async _appendOwnBirth(mesh, session, { deviceId, account, initial, sharing, workspace, project, boot }) {
1345
+ if (boot && mesh.localLength > 0) {
1346
+ await this.local.recordMesh(mesh.key, session, { deviceId, account, initial });
1347
+ void this._appendJoinLater(mesh, () => Promise.resolve(undefined), sharing);
1348
+ return;
1349
+ }
1338
1350
  await raceResource(mesh.autobee, (async () => {
1339
1351
  const birth = sharing
1340
1352
  ? await this._accountBirthMember(mesh, session)
@@ -2404,31 +2416,35 @@ export class Core extends ReadyResource {
2404
2416
  this._retiredAccount !== this._meshIdOf(account)) {
2405
2417
  return;
2406
2418
  }
2419
+ const minted = this._ownWorkspaceId;
2407
2420
  this._retiredAccount = null;
2408
2421
  this._retiredMeshes.clear();
2409
2422
  await this.local.restoreMesh(account.key);
2423
+ if (fresh && fresh !== account) {
2424
+ await this.local.archiveMesh(fresh.key, { homeTo: account.key });
2425
+ }
2410
2426
  await this._restorePublicWriters(account);
2411
2427
  await this._adoptAccount(account);
2412
2428
  if (fresh && fresh !== account) {
2413
- await this._dropOwnMint(fresh);
2414
- await this._teardownMesh(fresh, { purge: true });
2415
- }
2416
- }
2417
- async _dropOwnMint(fresh) {
2418
- const minted = new Set((await this.local.listMeshes()).filter((row) => row.initial).map((row) => b4a.toHex(row.key)));
2419
- for (const joined of await fresh.view.find(JOINED_MESHES).toArray()) {
2420
- const id = b4a.toHex(joined.meshKey);
2421
- if (!minted.has(id)) {
2422
- continue;
2423
- }
2424
- const own = this._publicMesh(id);
2425
- if (own) {
2426
- 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);
2427
2444
  }
2428
- await this._teardownMesh(this._pooled(id) ?? (await this._openPooledMesh(id, 'leave')), {
2429
- purge: true
2430
- });
2445
+ this._retiredMeshes.add(b4a.toHex(key));
2431
2446
  }
2447
+ this._notifyStatus();
2432
2448
  }
2433
2449
  async _replaceRevokedAccount(mesh) {
2434
2450
  this._minting = true;
@@ -2492,9 +2508,7 @@ export class Core extends ReadyResource {
2492
2508
  }
2493
2509
  this._kickedProjects.set(b4a.toHex(row.key), null);
2494
2510
  const project = await this._heldMesh(row.key, 'leave');
2495
- await (project
2496
- ? this._departRevokedMesh(project, { purge: true })
2497
- : this._teardownMesh(row.key));
2511
+ await (project ? this._departRevokedMesh(project) : this._teardownMesh(row.key));
2498
2512
  }
2499
2513
  }
2500
2514
  async _departRevokedMesh(mesh, opts = {}) {
@@ -2592,7 +2606,7 @@ export class Core extends ReadyResource {
2592
2606
  let pooled = this._pooled(meshId);
2593
2607
  if (this._isHidden(meshId, pooled?.workspaceKey)) {
2594
2608
  if (pooled) {
2595
- await this._teardownMesh(pooled, { depart: true, purge: true });
2609
+ await this._teardownMesh(pooled, { depart: true });
2596
2610
  }
2597
2611
  else {
2598
2612
  await this._dropKickedCopy(meshId);
@@ -2773,14 +2787,15 @@ export class Core extends ReadyResource {
2773
2787
  if (this._joinCancellation) {
2774
2788
  this._joinCancellation.accepting = false;
2775
2789
  }
2790
+ let member;
2776
2791
  try {
2792
+ member = await this._candidateBirthMember(candidate, invite);
2777
2793
  await this.local.recordMesh(candidate.key, session, { deviceId });
2778
2794
  }
2779
2795
  catch (error) {
2780
2796
  await this._discardCandidateMesh();
2781
2797
  throw error;
2782
2798
  }
2783
- const member = await this._candidateBirthMember(candidate, invite);
2784
2799
  const appended = this._appendJoinLater(candidate, async () => member);
2785
2800
  return this._landJoinedMesh(candidate, {
2786
2801
  source,
@@ -2879,336 +2894,6 @@ export class Core extends ReadyResource {
2879
2894
  this._log?.warn(['p2p', 'mesh'], 'recording the joined mesh failed', error);
2880
2895
  }
2881
2896
  }
2882
- async _followUpgrade(mesh) {
2883
- const meshId = this._meshIdOf(mesh);
2884
- if (this.closing || this._upgrading.has(meshId)) {
2885
- return;
2886
- }
2887
- this._upgrading.add(meshId);
2888
- try {
2889
- let kind = await meshKind(mesh.view);
2890
- if (kind === 'shared' && (await this._upgradable(mesh))) {
2891
- await mesh.dispatch('upgrade-to-workspace', { upgradedAt: Date.now() });
2892
- kind = await meshKind(mesh.view);
2893
- }
2894
- if (!this._isPooled(mesh)) {
2895
- return;
2896
- }
2897
- if (kind === 'workspace' && mesh.writable) {
2898
- await this.local.set(upgradeCheckedKey(mesh.key), GROUPS_PENDING);
2899
- await this._markFoldDue(mesh.key);
2900
- await this._reopenInRole(mesh, 'workspace');
2901
- await this._markJoinedWorkspace(mesh.key);
2902
- }
2903
- else if (kind !== 'project' && (kind !== 'shared' || (await bornUnderRules(mesh.view)))) {
2904
- await this._markUpgradeChecked(mesh.key);
2905
- }
2906
- }
2907
- catch (err) {
2908
- if (!this.closing && !mesh.closing) {
2909
- this._log?.warn(['p2p', 'mesh'], 'upgrading a shared mesh to a workspace failed', err);
2910
- }
2911
- }
2912
- finally {
2913
- this._upgrading.delete(meshId);
2914
- }
2915
- }
2916
- _followGroups(workspace) {
2917
- if (this.closing || !workspace.writable) {
2918
- return;
2919
- }
2920
- if (this._groupPasses.has(workspace)) {
2921
- this._groupPassesDue.add(workspace);
2922
- return;
2923
- }
2924
- this._groupPasses.add(workspace);
2925
- void this._passGroups(workspace)
2926
- .catch((err) => {
2927
- if (!this.closing && this._isPooled(workspace)) {
2928
- this._log?.warn(['p2p', 'mesh'], 'moving chat-groups into projects failed', err);
2929
- }
2930
- })
2931
- .finally(() => {
2932
- this._groupPasses.delete(workspace);
2933
- if (this._groupPassesDue.delete(workspace)) {
2934
- this._followGroups(workspace);
2935
- }
2936
- });
2937
- }
2938
- async _passGroups(workspace) {
2939
- const checked = await this.local.get(upgradeCheckedKey(workspace.key));
2940
- if (checked === true) {
2941
- return this._passAgents(workspace);
2942
- }
2943
- if (checked !== GROUPS_PENDING) {
2944
- return;
2945
- }
2946
- const ours = workspace.ourMemberId ?? (await workspace.refreshOurMember())?.memberId;
2947
- if (!ours || !(await isWorkspace(workspace.view))) {
2948
- return;
2949
- }
2950
- const buckets = await this._groupBuckets(workspace);
2951
- const rows = await projectsByGroup(workspace.view);
2952
- const passes = await Promise.allSettled(buckets.map((bucket) => this._passGroup(workspace, ours, { bucket, row: rows.get(bucket.groupId) ?? null })));
2953
- const settled = [];
2954
- for (const pass of passes) {
2955
- if (pass.status === 'rejected') {
2956
- throw pass.reason;
2957
- }
2958
- settled.push(pass.value);
2959
- }
2960
- if (settled.every((state) => state === 'done') && this._isPooled(workspace)) {
2961
- await this.local.set(upgradeCheckedKey(workspace.key), true);
2962
- }
2963
- if (!settled.includes('pending')) {
2964
- await this._passAgents(workspace);
2965
- }
2966
- }
2967
- async _groupBuckets(workspace) {
2968
- const named = workspace.name?.name?.trim() || 'Workspace';
2969
- const groups = await liveGroups(workspace.view);
2970
- const ids = [...groups.map((group) => group.id), UNGROUPED];
2971
- const records = await Promise.all(ids.map((id) => this._groupRecord(workspace, id)));
2972
- if (!records.every((record) => record.copied || record.done)) {
2973
- return this._scanBuckets(workspace, named);
2974
- }
2975
- return ids.map((groupId, idx) => ({
2976
- groupId,
2977
- name: groups[idx]?.name ?? named,
2978
- chats: new Set()
2979
- }));
2980
- }
2981
- async _markFoldDue(workspaceKey) {
2982
- const key = agentsFoldKey(workspaceKey);
2983
- if ((await this.local.get(key)) !== true) {
2984
- await this.local.set(key, FOLD_PENDING);
2985
- }
2986
- }
2987
- async _passAgents(workspace) {
2988
- if ((await this.local.get(agentsFoldKey(workspace.key))) !== FOLD_PENDING) {
2989
- return;
2990
- }
2991
- const copies = await ownCopies(workspace, this._requireDeviceKeyPair().publicKey);
2992
- if (copies.length > 0) {
2993
- const account = await this._writableAccount();
2994
- const projects = await this._heldProjects(workspace);
2995
- if (!account || !projects) {
2996
- return;
2997
- }
2998
- await foldAgents({ account, workspace, projects, picks: this._agentPicks() }, copies);
2999
- }
3000
- if (this._isPooled(workspace)) {
3001
- await this.local.set(agentsFoldKey(workspace.key), true);
3002
- }
3003
- }
3004
- async _heldProjects(workspace) {
3005
- const ours = workspace.ourMemberId;
3006
- const held = [];
3007
- for (const row of await workspace.view.find(PROJECTS).toArray()) {
3008
- if (row.deletedAt || !ours || !listedProject(row, ours))
3009
- continue;
3010
- const project = await this._heldMesh(row.id);
3011
- if (!project?.writable) {
3012
- return null;
3013
- }
3014
- held.push(project);
3015
- }
3016
- return held;
3017
- }
3018
- async _groupRecord(workspace, groupId) {
3019
- const stored = await this.local.get(groupRecordKey(workspace.key, groupId));
3020
- return isGroupRecord(stored) ? stored : {};
3021
- }
3022
- async _scanBuckets(workspace, named) {
3023
- const buckets = await groupBuckets(workspace.view, named);
3024
- const ungrouped = buckets.some((bucket) => bucket.groupId === UNGROUPED);
3025
- if (!ungrouped && !(await this._groupRecord(workspace, UNGROUPED)).key) {
3026
- await this.local.set(groupRecordKey(workspace.key, UNGROUPED), { done: true });
3027
- }
3028
- return buckets;
3029
- }
3030
- async _passGroup(workspace, ours, { bucket, row: listed }) {
3031
- const recordKey = groupRecordKey(workspace.key, bucket.groupId);
3032
- let record = await this._groupRecord(workspace, bucket.groupId);
3033
- if (record.done) {
3034
- return 'done';
3035
- }
3036
- let row = listed;
3037
- if (!row) {
3038
- record = await this._foundGroupProject(workspace, bucket, { recordKey, record });
3039
- row = await projectOfGroup(workspace.view, bucket.groupId);
3040
- }
3041
- const live = !!row && !row.deletedAt;
3042
- if (row && live && record.key === b4a.toHex(row.id)) {
3043
- await this._listNewMembers(workspace, { row, recordKey, record });
3044
- return 'won';
3045
- }
3046
- if (row && live && !(await this._mergeGroupInto(workspace, ours, { row, bucket, record }))) {
3047
- return 'pending';
3048
- }
3049
- if (record.key) {
3050
- await this._purgeProject(b4a.from(record.key, 'hex'));
3051
- }
3052
- await this.local.set(recordKey, { ...record, done: true });
3053
- return 'done';
3054
- }
3055
- async _foundGroupProject(workspace, bucket, { recordKey, record }) {
3056
- const held = record.key ? await this._heldMesh(b4a.from(record.key, 'hex')) : null;
3057
- const name = projectName(bucket.name.trim() || 'Project');
3058
- const project = held ?? (await this._openNewProject(workspace, { name }));
3059
- let next = { ...record, key: this._meshIdOf(project) };
3060
- await this.local.set(recordKey, next);
3061
- await this._addWorkspaceMembers(workspace, project);
3062
- if (!next.copied) {
3063
- await this._copyGroup(workspace, project, bucket);
3064
- next = { ...next, copied: true };
3065
- await this.local.set(recordKey, next);
3066
- }
3067
- const now = Date.now();
3068
- await this._listProject(workspace, project, {
3069
- createdAt: now,
3070
- updatedAt: now,
3071
- groupId: bucket.groupId
3072
- });
3073
- this._notifyStatus();
3074
- return next;
3075
- }
3076
- async _mergeGroupInto(workspace, ours, { row, bucket, record }) {
3077
- const winner = listsMember(row, ours) ? await this._winnerOf(workspace, row) : null;
3078
- if (!winner) {
3079
- return false;
3080
- }
3081
- await this._addWorkspaceMembers(workspace, winner);
3082
- await this._listWorkspaceMembers(workspace, row);
3083
- const own = record.key ? await this._heldMesh(b4a.from(record.key, 'hex')) : null;
3084
- if (own) {
3085
- await copyContent({
3086
- from: own,
3087
- into: winner,
3088
- deviceId: this._requireDeviceKeyPair().publicKey
3089
- });
3090
- }
3091
- await this._copyGroup(workspace, winner, bucket);
3092
- this._log?.info(['p2p', 'mesh'], 'merged a chat-group into the project that won it', {
3093
- project: b4a.toHex(row.id).slice(0, 8)
3094
- });
3095
- return true;
3096
- }
3097
- async _winnerOf(workspace, row) {
3098
- const projectId = b4a.toHex(row.id);
3099
- if (!this._pooled(projectId) && !(await this.local.readMeshSession(row.id))) {
3100
- await this._followProjects(workspace);
3101
- await this._following.get(projectId);
3102
- }
3103
- const winner = this._pooled(projectId) ?? (await this._heldMesh(row.id));
3104
- return winner?.writable ? winner : null;
3105
- }
3106
- async _copyGroup(workspace, project, { chats }) {
3107
- const deviceId = this._requireDeviceKeyPair().publicKey;
3108
- await copyContent({ from: workspace, into: project, deviceId, chats, endsOpenRuns: true });
3109
- }
3110
- async _addWorkspaceMembers(workspace, project) {
3111
- for (const memberId of await this._workspaceMembers(workspace)) {
3112
- if (!(await inProject(project.view, memberId))) {
3113
- await project.dispatch('put-project-member', { memberId });
3114
- }
3115
- }
3116
- }
3117
- async _listNewMembers(workspace, { row, recordKey, record }) {
3118
- const listed = new Set(record.listedMembers ?? []);
3119
- const members = await this._workspaceMembers(workspace);
3120
- const added = members.filter((id) => !listed.has(b4a.toHex(id)));
3121
- const project = added.length > 0 ? await this._heldMesh(row.id) : null;
3122
- for (const memberId of added) {
3123
- if (project && !(await inProject(project.view, memberId))) {
3124
- await project.dispatch('put-project-member', { memberId });
3125
- }
3126
- if (!listsMember(row, memberId)) {
3127
- await workspace.dispatch('set-project-listing', { projectId: row.id, memberId });
3128
- }
3129
- }
3130
- if (added.length > 0 && this._isPooled(workspace)) {
3131
- const listedMembers = [...listed, ...added.map((id) => b4a.toHex(id))];
3132
- await this.local.set(recordKey, { ...record, listedMembers });
3133
- }
3134
- }
3135
- async _listWorkspaceMembers(workspace, row) {
3136
- for (const memberId of await this._workspaceMembers(workspace)) {
3137
- if (!listsMember(row, memberId)) {
3138
- await workspace.dispatch('set-project-listing', { projectId: row.id, memberId });
3139
- }
3140
- }
3141
- }
3142
- async _workspaceMembers(workspace) {
3143
- const live = await liveMemberIds(workspace.view);
3144
- return (await upgradeMembers(workspace.view)).filter((id) => live.has(b4a.toHex(id)));
3145
- }
3146
- async _openUpgradable() {
3147
- const account = await this._storedAccountKey();
3148
- if (this.closing) {
3149
- return;
3150
- }
3151
- for (const row of await this.local.listMeshes()) {
3152
- if (this.closing) {
3153
- return;
3154
- }
3155
- const skip = row.workspaceKey || (account && b4a.equals(row.key, account));
3156
- const role = recordedRole(row);
3157
- if (skip || (role !== 'project' && role !== 'workspace')) {
3158
- continue;
3159
- }
3160
- const checked = await this.local.get(upgradeCheckedKey(row.key));
3161
- if (this.closing) {
3162
- return;
3163
- }
3164
- const due = role === 'workspace'
3165
- ? checked === GROUPS_PENDING ||
3166
- (await this.local.get(agentsFoldKey(row.key))) === FOLD_PENDING
3167
- : checked !== true;
3168
- if (this.closing) {
3169
- return;
3170
- }
3171
- if (!due) {
3172
- continue;
3173
- }
3174
- await this._openPooledMesh(b4a.toHex(row.key)).catch((err) => {
3175
- if (!this.closing) {
3176
- this._log?.warn(['p2p', 'mesh'], 'opening a shared mesh to upgrade it failed', err);
3177
- }
3178
- });
3179
- }
3180
- }
3181
- async _markUpgradeChecked(meshKey) {
3182
- const key = upgradeCheckedKey(meshKey);
3183
- if ((await this.local.get(key)) !== true) {
3184
- await this.local.set(key, true);
3185
- }
3186
- }
3187
- async _upgradable(mesh) {
3188
- if (!mesh.writable || (await rulesVersion(mesh.view)) < WORKSPACES_RULES) {
3189
- return false;
3190
- }
3191
- return !(await bornUnderRules(mesh.view));
3192
- }
3193
- async _markJoinedWorkspace(meshKey) {
3194
- const registry = await this._writableAccount();
3195
- const joined = await registry?.view.get(JOINED_MESHES, { meshKey });
3196
- if (!registry || !joined || joined.workspace || joined.leftAt) {
3197
- return;
3198
- }
3199
- await registry.dispatch('put-joined-mesh', {
3200
- room: {
3201
- meshKey,
3202
- memberId: joined.memberId,
3203
- joinedAt: joined.joinedAt,
3204
- label: joined.label,
3205
- labeledAt: joined.labeledAt,
3206
- shared: joined.shared,
3207
- sharedAt: joined.sharedAt,
3208
- workspace: true
3209
- }
3210
- });
3211
- }
3212
2897
  async _reopenInRole(mesh, role) {
3213
2898
  await this.local.setMeshRole(mesh.key, role);
3214
2899
  await this._retirePooledMesh(mesh);
@@ -3451,14 +3136,14 @@ export class Core extends ReadyResource {
3451
3136
  }
3452
3137
  _onSharedWritable(mesh) {
3453
3138
  if (mesh.role === 'project') {
3454
- void this._followUpgrade(mesh);
3139
+ void this._upgrade.followShared(mesh);
3455
3140
  void this._followSharedAgents();
3456
3141
  void this._workspaceOf(mesh)
3457
- .then((workspace) => workspace && this._followGroups(workspace))
3142
+ .then((workspace) => workspace && this._upgrade.follow(workspace))
3458
3143
  .catch(noop);
3459
3144
  }
3460
3145
  else if (mesh.role === 'workspace') {
3461
- this._followGroups(mesh);
3146
+ this._upgrade.follow(mesh);
3462
3147
  void this._followSharedAgents();
3463
3148
  this._shareMemberKey(mesh);
3464
3149
  void this._followHosting();
@@ -3475,8 +3160,10 @@ export class Core extends ReadyResource {
3475
3160
  void this._followPublicMesh(mesh);
3476
3161
  void this._followSkillSecrets();
3477
3162
  }
3478
- if (this._isOwnPublic(mesh)) {
3163
+ const account = this._accountMesh();
3164
+ if (this._isOwnPublic(mesh) && account) {
3479
3165
  void this._followAgentCopies();
3166
+ this._upgrade.copyAccount(account);
3480
3167
  }
3481
3168
  this._notifyStatus();
3482
3169
  }
@@ -3517,6 +3204,9 @@ export class Core extends ReadyResource {
3517
3204
  entry.handlers = handlers;
3518
3205
  mesh.on('kick-dirty', () => this._reviewKick(mesh).catch((err) => this._log?.warn(['p2p', 'mesh'], 'reviewing a kick failed', err)));
3519
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 }));
3520
3210
  if (mesh.writable) {
3521
3211
  this._onMeshSeedDue(mesh);
3522
3212
  this._onProjectWritable(mesh);
@@ -3559,6 +3249,8 @@ export class Core extends ReadyResource {
3559
3249
  void this._followAccountName(mesh);
3560
3250
  });
3561
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));
3562
3254
  mesh.on('joined', () => {
3563
3255
  if (entry.meshesPending) {
3564
3256
  void this._followJoinedMeshes(mesh);
@@ -3603,18 +3295,17 @@ export class Core extends ReadyResource {
3603
3295
  void this._followHosting();
3604
3296
  }
3605
3297
  if (mesh.role === 'project') {
3606
- void this._followUpgrade(mesh);
3298
+ void this._upgrade.followShared(mesh);
3607
3299
  }
3608
- if ((changed || groupsChanged) && mesh.role === 'workspace') {
3609
- this._followGroups(mesh);
3300
+ if ((changed || groupsChanged || chatsChanged) && mesh.role === 'workspace') {
3301
+ this._upgrade.follow(mesh);
3610
3302
  }
3611
3303
  const opening = this._ownWorkspaceId !== null && !this._publicMesh();
3612
3304
  if (this._isAccount(mesh) && (changed || entry.publicPending || opening)) {
3613
3305
  void this._followPublicMesh(mesh);
3614
3306
  }
3615
- const copying = chatsChanged || this._accountCopied === null;
3616
- if (copying && this._isAccount(mesh) && this._ownWorkspaceId && this._publicMesh()) {
3617
- this._migrateIntoPublic(mesh, b4a.from(this._ownWorkspaceId, 'hex'));
3307
+ if ((chatsChanged || this._upgrade.accountUnread) && this._isAccount(mesh)) {
3308
+ this._upgrade.copyAccount(mesh);
3618
3309
  }
3619
3310
  }
3620
3311
  _listenMesh(mesh, handlers) {
@@ -3832,7 +3523,7 @@ export class Core extends ReadyResource {
3832
3523
  async _settlePublicMeshes(account) {
3833
3524
  let settled = true;
3834
3525
  try {
3835
- await this._settleOwnWorkspace(account);
3526
+ settled = await this._settleOwnWorkspace(account);
3836
3527
  }
3837
3528
  catch (err) {
3838
3529
  settled = false;
@@ -3859,7 +3550,7 @@ export class Core extends ReadyResource {
3859
3550
  }
3860
3551
  async _settleOwnWorkspace(account) {
3861
3552
  if (this._localDevice && !account.ourMemberId) {
3862
- return;
3553
+ return true;
3863
3554
  }
3864
3555
  const local = (await this.local.listMeshes()).filter((row) => !b4a.equals(row.key, account.key) &&
3865
3556
  !row.workspaceKey &&
@@ -3869,11 +3560,14 @@ export class Core extends ReadyResource {
3869
3560
  const own = ours
3870
3561
  .filter((row) => row.initial && !row.kicked)
3871
3562
  .sort((a, b) => (b.joinedAt || Infinity) - (a.joinedAt || Infinity))[0];
3563
+ if (!own && !account.joined) {
3564
+ return false;
3565
+ }
3872
3566
  const mints = !own &&
3873
3567
  !(await this._markDerivedInitial(account, rows)) &&
3874
3568
  (await this._mintsOwnWorkspace(account, { rows, local: ours }));
3875
3569
  if (!this._isAccount(account)) {
3876
- return;
3570
+ return true;
3877
3571
  }
3878
3572
  this._setOwnWorkspace(own ? b4a.toHex(own.key) : null);
3879
3573
  if (mints) {
@@ -3883,6 +3577,7 @@ export class Core extends ReadyResource {
3883
3577
  this._ownDecided = account;
3884
3578
  this._notifyStatus();
3885
3579
  }
3580
+ return true;
3886
3581
  }
3887
3582
  async _mintsOwnWorkspace(account, { rows, local }) {
3888
3583
  if (!rows.length && !local.length) {
@@ -4027,9 +3722,7 @@ export class Core extends ReadyResource {
4027
3722
  else if (key && !b4a.equals(key, held.key)) {
4028
3723
  this._mergeLostMint(account, held, key);
4029
3724
  }
4030
- if (workspaceId === this._ownWorkspaceId) {
4031
- this._migrateIntoPublic(account, workspaceKey);
4032
- }
3725
+ this._upgrade.copyAccount(account);
4033
3726
  }
4034
3727
  async _publicRecord(account, workspaceKey) {
4035
3728
  const value = await this.local.get(publicRecordKey(account.key, workspaceKey));
@@ -4109,7 +3802,7 @@ export class Core extends ReadyResource {
4109
3802
  }
4110
3803
  this._publicIds.set(workspaceId, this._meshIdOf(winner));
4111
3804
  this._notifyStatus();
4112
- await this._publicMigration;
3805
+ await this._upgrade.accountCopy;
4113
3806
  await copyContent({
4114
3807
  from: lost,
4115
3808
  into: winner,
@@ -4134,77 +3827,6 @@ export class Core extends ReadyResource {
4134
3827
  });
4135
3828
  this._publicMerges.set(workspaceId, merge);
4136
3829
  }
4137
- _migrateIntoPublic(account, workspaceKey) {
4138
- if (this._publicMigration) {
4139
- this._copyAgain = true;
4140
- return;
4141
- }
4142
- this._publicMigration = (async () => {
4143
- const into = this._publicMesh(b4a.toHex(workspaceKey));
4144
- const workspace = into && (await this._heldMesh(workspaceKey));
4145
- if (!into || !workspace?.minted) {
4146
- return;
4147
- }
4148
- const chats = await oldChats(account);
4149
- const copied = `${this._meshIdOf(account)}:${chats?.mark}`;
4150
- if (this._accountCopied === copied) {
4151
- return;
4152
- }
4153
- if (!chats) {
4154
- await this._recordCopied(account, workspaceKey);
4155
- this._accountCopied = copied;
4156
- return;
4157
- }
4158
- await into.waitForWritable();
4159
- if (!this._isAccount(account) || !this._isPooled(into)) {
4160
- return;
4161
- }
4162
- await copyContent({
4163
- from: account,
4164
- into,
4165
- deviceId: this._requireDeviceKeyPair().publicKey,
4166
- endsOpenRuns: true
4167
- });
4168
- await this._recordCopied(account, workspaceKey);
4169
- if (await this._markCopied(account, workspaceKey)) {
4170
- this._accountCopied = copied;
4171
- }
4172
- this._log?.info(['p2p', 'mesh'], 'copied the account into its public mesh', {
4173
- mesh: this._meshIdOf(into).slice(0, 8)
4174
- });
4175
- })()
4176
- .catch((err) => {
4177
- if (!this.closing) {
4178
- this._log?.warn(['p2p', 'mesh'], 'copying the account into its public mesh failed', err);
4179
- }
4180
- })
4181
- .finally(() => {
4182
- this._publicMigration = null;
4183
- if (this._copyAgain && !this.closing) {
4184
- this._copyAgain = false;
4185
- this._migrateIntoPublic(account, workspaceKey);
4186
- }
4187
- });
4188
- }
4189
- async _recordCopied(account, workspaceKey) {
4190
- const record = await this._publicRecord(account, workspaceKey);
4191
- if (record && !record.migrated) {
4192
- await this._savePublicRecord(account, workspaceKey, { ...record, migrated: true });
4193
- }
4194
- }
4195
- async _markCopied(account, workspaceKey) {
4196
- const row = await account.view.get(JOINED_MESHES, { meshKey: workspaceKey });
4197
- if (!row || !account.writable) {
4198
- return false;
4199
- }
4200
- if (!row.initial) {
4201
- const { meshKey, memberId, joinedAt } = row;
4202
- await account.dispatch('put-joined-mesh', {
4203
- room: { meshKey, memberId, joinedAt, initial: true }
4204
- });
4205
- }
4206
- return true;
4207
- }
4208
3830
  async _followAgentCopies(agentId) {
4209
3831
  if (!agentId && !(await this._agentCopies.lock()))
4210
3832
  return;
@@ -4466,7 +4088,7 @@ export class Core extends ReadyResource {
4466
4088
  await this._rotatePublicWriters(account);
4467
4089
  }
4468
4090
  async _dropKickedCopy(meshId) {
4469
- await this._teardownMesh(await this._openPooledMesh(meshId, 'leave'), { purge: true });
4091
+ await this._teardownMesh(this._pooled(meshId) ?? b4a.from(meshId, 'hex'));
4470
4092
  }
4471
4093
  async _rotatePublicWriters(account) {
4472
4094
  for (const workspaceKey of await this._publicWorkspaces(account)) {
@@ -4494,9 +4116,6 @@ export class Core extends ReadyResource {
4494
4116
  return this._meshes.get(meshKey)?.mesh ?? null;
4495
4117
  }
4496
4118
  async _backfillWriterLinks(mesh) {
4497
- if (!floorExcludesBefore(this._minSupportedVersion, REINDEX_WRITERS_FROM_APP_VERSION)) {
4498
- return;
4499
- }
4500
4119
  try {
4501
4120
  await mesh.waitForWritable();
4502
4121
  await mesh.autobee.update();
@@ -4514,6 +4133,45 @@ export class Core extends ReadyResource {
4514
4133
  }
4515
4134
  }
4516
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
+ }
4517
4175
  _accountMesh() {
4518
4176
  return this._pooled(this._accountId);
4519
4177
  }
@@ -5450,13 +5108,13 @@ export class Core extends ReadyResource {
5450
5108
  }
5451
5109
  async _listProject(workspace, project, listing) {
5452
5110
  const batch = workspace.batch();
5453
- 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) };
5454
5113
  batch.dispatch('put-project', { project: listed });
5455
5114
  const ours = workspace.ourMemberId;
5456
- const sealing = ours && { projectId: project.key, seed: project.seed, sealedBy: ours };
5457
- const own = sealing ? await dueProjectKey(workspace.view, ours, sealing) : null;
5458
- if (own) {
5459
- 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 });
5460
5118
  }
5461
5119
  await batch.flush();
5462
5120
  }
@@ -6460,8 +6118,23 @@ export class Core extends ReadyResource {
6460
6118
  }
6461
6119
  });
6462
6120
  }
6121
+ await this._dropUnlistedFollows(workspace);
6463
6122
  void this._sealProjectKeys();
6464
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
+ }
6465
6138
  _mayFollowProjects(workspace) {
6466
6139
  const leaving = this._leaving !== null &&
6467
6140
  (this._leaving === this._accountId || this._leaving === this._meshIdOf(workspace));
@@ -6508,19 +6181,7 @@ export class Core extends ReadyResource {
6508
6181
  return;
6509
6182
  }
6510
6183
  const batch = workspace.batch();
6511
- for (const memberId of row.memberIds ?? []) {
6512
- const due = await dueProjectKey(workspace.view, memberId, {
6513
- projectId: row.id,
6514
- seed,
6515
- sealedBy
6516
- });
6517
- if (due) {
6518
- batch.dispatch('put-project-key', due);
6519
- }
6520
- }
6521
- for (const due of await dueProjectDeviceKeys(workspace.view, row, { seed, sealedBy })) {
6522
- batch.dispatch('put-project-device-key', due);
6523
- }
6184
+ await sealMembers(batch, { view: workspace.view, row, seed, sealedBy });
6524
6185
  await batch.flush();
6525
6186
  }
6526
6187
  async _sealProjectHosts(workspace, projectId, seed) {