@qvac/core 1.0.0-rc.7 → 1.0.0
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.
- package/CHANGELOG.md +241 -3
- package/README.md +1 -0
- package/dist-lib/lib/api/attachments.d.ts +1 -1
- package/dist-lib/lib/api/attachments.js +11 -0
- package/dist-lib/lib/api/chunks.d.ts +4 -2
- package/dist-lib/lib/api/chunks.js +6 -3
- package/dist-lib/lib/api/device-routes.d.ts +6 -1
- package/dist-lib/lib/api/device-routes.js +20 -1
- package/dist-lib/lib/api/devices.js +6 -18
- package/dist-lib/lib/api/execution.d.ts +6 -2
- package/dist-lib/lib/api/execution.js +29 -3
- package/dist-lib/lib/api/host-run-log.d.ts +7 -1
- package/dist-lib/lib/api/host-run-log.js +3 -1
- package/dist-lib/lib/api/index.d.ts +3 -1
- package/dist-lib/lib/api/index.js +17 -6
- package/dist-lib/lib/api/members.js +13 -29
- package/dist-lib/lib/api/mesh.d.ts +2 -1
- package/dist-lib/lib/api/projects.d.ts +1 -1
- package/dist-lib/lib/api/projects.js +9 -5
- package/dist-lib/lib/api/rag-label.d.ts +4 -0
- package/dist-lib/lib/api/rag-label.js +21 -0
- package/dist-lib/lib/api/rag-lexical.d.ts +22 -0
- package/dist-lib/lib/api/rag-lexical.js +1058 -0
- package/dist-lib/lib/api/rag-manifest.d.ts +13 -0
- package/dist-lib/lib/api/rag-manifest.js +46 -0
- package/dist-lib/lib/api/rag-outline.d.ts +36 -0
- package/dist-lib/lib/api/rag-outline.js +426 -0
- package/dist-lib/lib/api/rag-plugin.d.ts +20 -0
- package/dist-lib/lib/api/rag-plugin.js +70 -0
- package/dist-lib/lib/api/rag-synthesis.d.ts +7 -0
- package/dist-lib/lib/api/rag-synthesis.js +102 -0
- package/dist-lib/lib/api/rag-targets.d.ts +23 -0
- package/dist-lib/lib/api/rag-targets.js +215 -0
- package/dist-lib/lib/api/rag-window.d.ts +5 -0
- package/dist-lib/lib/api/rag-window.js +46 -0
- package/dist-lib/lib/api/rag.d.ts +3 -2
- package/dist-lib/lib/api/rag.js +421 -184
- package/dist-lib/lib/api/served.js +8 -14
- package/dist-lib/lib/api/workspace.d.ts +8 -1
- package/dist-lib/lib/api/workspace.js +27 -13
- package/dist-lib/lib/assistant/harness.d.ts +9 -1
- package/dist-lib/lib/assistant/history.d.ts +11 -2
- package/dist-lib/lib/assistant/history.js +74 -4
- package/dist-lib/lib/assistant/media-round.d.ts +61 -0
- package/dist-lib/lib/assistant/media-round.js +111 -0
- package/dist-lib/lib/assistant/message.d.ts +7 -0
- package/dist-lib/lib/assistant/model-round.d.ts +19 -1
- package/dist-lib/lib/assistant/model-round.js +163 -28
- package/dist-lib/lib/assistant/pdf-pages.d.ts +16 -1
- package/dist-lib/lib/assistant/pdf-pages.js +67 -3
- package/dist-lib/lib/assistant/round-host.d.ts +22 -0
- package/dist-lib/lib/assistant/round-host.js +142 -8
- package/dist-lib/lib/assistant/upload.d.ts +12 -0
- package/dist-lib/lib/assistant/upload.js +36 -0
- package/dist-lib/lib/assistant-client.d.ts +1 -0
- package/dist-lib/lib/assistant-client.js +40 -19
- package/dist-lib/lib/assistant-tree.d.ts +2 -0
- package/dist-lib/lib/assistant-tree.js +48 -1
- package/dist-lib/lib/assistant.d.ts +18 -1
- package/dist-lib/lib/assistant.js +484 -95
- package/dist-lib/lib/client.d.ts +1 -0
- package/dist-lib/lib/client.js +4 -1
- package/dist-lib/lib/compaction.js +2 -2
- package/dist-lib/lib/core/agent.js +12 -45
- package/dist-lib/lib/core/approval-rules.js +1 -8
- package/dist-lib/lib/core/archived-mesh.d.ts +2 -1
- package/dist-lib/lib/core/archived-mesh.js +12 -8
- package/dist-lib/lib/core/chat-copy.d.ts +18 -3
- package/dist-lib/lib/core/chat-copy.js +74 -25
- package/dist-lib/lib/core/chat-fork.js +15 -6
- package/dist-lib/lib/core/chat-group.js +16 -24
- package/dist-lib/lib/core/chat-move.d.ts +9 -0
- package/dist-lib/lib/core/chat-move.js +94 -3
- package/dist-lib/lib/core/chat-order.js +12 -18
- package/dist-lib/lib/core/chat.js +38 -31
- package/dist-lib/lib/core/chunk.d.ts +2 -2
- package/dist-lib/lib/core/chunk.js +18 -25
- package/dist-lib/lib/core/collections.d.ts +2 -0
- package/dist-lib/lib/core/collections.js +2 -0
- package/dist-lib/lib/core/contract/activity.d.ts +4 -0
- package/dist-lib/lib/core/contract/activity.js +34 -4
- package/dist-lib/lib/core/contract/chat-group.d.ts +1 -0
- package/dist-lib/lib/core/contract/chat-group.js +21 -1
- package/dist-lib/lib/core/contract/chat.d.ts +9 -0
- package/dist-lib/lib/core/contract/chat.js +127 -5
- package/dist-lib/lib/core/contract/chunks.d.ts +1 -0
- package/dist-lib/lib/core/contract/chunks.js +37 -6
- package/dist-lib/lib/core/contract/features.d.ts +21 -0
- package/dist-lib/lib/core/contract/features.js +22 -0
- package/dist-lib/lib/core/contract/index.js +2 -0
- package/dist-lib/lib/core/contract/indexing.js +4 -2
- package/dist-lib/lib/core/contract/knowledge.js +5 -4
- package/dist-lib/lib/core/contract/members.js +16 -10
- package/dist-lib/lib/core/contract/mesh-kind.d.ts +1 -0
- package/dist-lib/lib/core/contract/mesh-kind.js +3 -1
- package/dist-lib/lib/core/contract/projects.d.ts +5 -0
- package/dist-lib/lib/core/contract/projects.js +13 -5
- package/dist-lib/lib/core/contract/roles.js +32 -4
- package/dist-lib/lib/core/contract/run-requests.d.ts +1 -0
- package/dist-lib/lib/core/contract/run-requests.js +35 -2
- package/dist-lib/lib/core/contract/seed-defaults.js +2 -2
- package/dist-lib/lib/core/contract/tool-configs.js +3 -2
- package/dist-lib/lib/core/contract/workspace.d.ts +2 -0
- package/dist-lib/lib/core/contract/workspace.js +40 -7
- package/dist-lib/lib/core/contract/writers.js +2 -2
- package/dist-lib/lib/core/departures.d.ts +3 -1
- package/dist-lib/lib/core/departures.js +3 -0
- package/dist-lib/lib/core/devices.js +1 -8
- package/dist-lib/lib/core/files/attachments.d.ts +2 -0
- package/dist-lib/lib/core/files/attachments.js +19 -1
- package/dist-lib/lib/core/files/index.d.ts +20 -4
- package/dist-lib/lib/core/files/index.js +132 -21
- package/dist-lib/lib/core/files/processing/document.d.ts +4 -0
- package/dist-lib/lib/core/files/processing/document.js +7 -0
- package/dist-lib/lib/core/files/processing/index.d.ts +13 -0
- package/dist-lib/lib/core/files/processing/index.js +144 -12
- package/dist-lib/lib/core/files/processing/protocol.js +13 -34
- package/dist-lib/lib/core/files/processing/state.d.ts +2 -0
- package/dist-lib/lib/core/files/rag/build.d.ts +19 -2
- package/dist-lib/lib/core/files/rag/build.js +123 -19
- package/dist-lib/lib/core/files/rag/failure.js +2 -1
- package/dist-lib/lib/core/files/rag/index.d.ts +11 -3
- package/dist-lib/lib/core/files/rag/index.js +13 -1
- package/dist-lib/lib/core/files/rag/load.d.ts +6 -3
- package/dist-lib/lib/core/files/rag/load.js +27 -6
- package/dist-lib/lib/core/files/rag/plugin-index.d.ts +32 -0
- package/dist-lib/lib/core/files/rag/plugin-index.js +127 -0
- package/dist-lib/lib/core/files/rag/produce.d.ts +5 -3
- package/dist-lib/lib/core/files/rag/produce.js +104 -16
- package/dist-lib/lib/core/files/rag/retrieval/full-text.d.ts +3 -1
- package/dist-lib/lib/core/files/rag/retrieval/full-text.js +62 -13
- package/dist-lib/lib/core/files/rag/text-cache.d.ts +28 -0
- package/dist-lib/lib/core/files/rag/text-cache.js +141 -0
- package/dist-lib/lib/core/files/records/knowledge.d.ts +1 -3
- package/dist-lib/lib/core/files/records/knowledge.js +7 -40
- package/dist-lib/lib/core/files/records/reclaim.js +5 -10
- package/dist-lib/lib/core/files/upload.js +68 -41
- package/dist-lib/lib/core/fs-drives.js +14 -26
- package/dist-lib/lib/core/invite.d.ts +1 -0
- package/dist-lib/lib/core/invite.js +4 -2
- package/dist-lib/lib/core/local/files.d.ts +1 -1
- package/dist-lib/lib/core/local/files.js +5 -5
- package/dist-lib/lib/core/local/index.d.ts +13 -6
- package/dist-lib/lib/core/local/index.js +103 -69
- package/dist-lib/lib/core/local/run-logs.d.ts +1 -1
- package/dist-lib/lib/core/local/run-logs.js +5 -5
- package/dist-lib/lib/core/mesh-processor.d.ts +19 -0
- package/dist-lib/lib/core/mesh-processor.js +87 -0
- package/dist-lib/lib/core/mesh.d.ts +48 -5
- package/dist-lib/lib/core/mesh.js +318 -129
- package/dist-lib/lib/core/migrations/agent-catalog.js +2 -8
- package/dist-lib/lib/core/migrations/agent-tools-scope.js +5 -11
- package/dist-lib/lib/core/migrations/workspace-from-0-21/index.d.ts +88 -0
- package/dist-lib/lib/core/migrations/workspace-from-0-21/index.js +731 -0
- package/dist-lib/lib/core/migrations/writer-links.d.ts +2 -0
- package/dist-lib/lib/core/migrations/writer-links.js +32 -0
- package/dist-lib/lib/core/project-keys.d.ts +6 -0
- package/dist-lib/lib/core/project-keys.js +11 -0
- package/dist-lib/lib/core/public-mesh.d.ts +3 -0
- package/dist-lib/lib/core/public-mesh.js +10 -8
- package/dist-lib/lib/core/run-requests.d.ts +3 -0
- package/dist-lib/lib/core/run-requests.js +21 -1
- package/dist-lib/lib/core/skill-availability.d.ts +33 -0
- package/dist-lib/lib/core/skill-availability.js +67 -0
- package/dist-lib/lib/core/sync/background.d.ts +0 -1
- package/dist-lib/lib/core/sync/background.js +2 -1
- package/dist-lib/lib/core/sync/changed.d.ts +4 -2
- package/dist-lib/lib/core/sync/changed.js +1 -1
- package/dist-lib/lib/core/sync/index.js +52 -10
- package/dist-lib/lib/core/sync/view-updates.d.ts +18 -12
- package/dist-lib/lib/core/sync/view-updates.js +82 -91
- package/dist-lib/lib/core/sync-status.js +97 -41
- package/dist-lib/lib/core/tool-activity.js +6 -12
- package/dist-lib/lib/core/tool-configs.js +17 -29
- package/dist-lib/lib/core/upgrade-summary.d.ts +19 -0
- package/dist-lib/lib/core/upgrade-summary.js +67 -0
- package/dist-lib/lib/core/view-read.d.ts +4 -0
- package/dist-lib/lib/core/view-read.js +23 -0
- package/dist-lib/lib/core/workspace-upgrade.d.ts +7 -0
- package/dist-lib/lib/core/workspace-upgrade.js +27 -16
- package/dist-lib/lib/core.d.ts +45 -42
- package/dist-lib/lib/core.js +675 -640
- package/dist-lib/lib/errors.d.ts +24 -0
- package/dist-lib/lib/errors.js +24 -0
- package/dist-lib/lib/llm-model-names.d.ts +1 -1
- package/dist-lib/lib/llm-model-names.js +1 -1
- package/dist-lib/lib/mesh-scoped-engine.js +4 -0
- package/dist-lib/lib/model-context.js +1 -2
- package/dist-lib/lib/model-download.d.ts +6 -1
- package/dist-lib/lib/model-download.js +16 -3
- package/dist-lib/lib/oauth/resolve.js +29 -8
- package/dist-lib/lib/oauth/token-exchange-core.js +11 -0
- package/dist-lib/lib/probe-rounds.d.ts +7 -2
- package/dist-lib/lib/probe-rounds.js +6 -1
- package/dist-lib/lib/run-transcript.d.ts +4 -1
- package/dist-lib/lib/run-transcript.js +15 -6
- package/dist-lib/schema/engine.js +83 -6
- package/dist-lib/schema/indexes.d.ts +3 -0
- package/dist-lib/schema/indexes.js +5 -0
- package/dist-lib/schema/local.js +11 -2
- package/dist-lib/schema/qvac.js +49 -5
- package/dist-lib/spec/bind.js +34 -0
- package/dist-lib/spec/calls.js +5 -0
- package/dist-lib/spec/capabilities.d.ts +17 -0
- package/dist-lib/spec/hrpc/hrpc.json +65 -0
- package/dist-lib/spec/hrpc/index.js +68 -5
- package/dist-lib/spec/hrpc/messages.js +260 -23
- package/dist-lib/spec/hyperschema/index.js +260 -23
- package/dist-lib/spec/hyperschema/schema.json +181 -0
- package/dist-lib/spec/hyperschema/types.d.ts +48 -0
- package/dist-lib/spec/local/hyperdb/messages.js +270 -224
- package/dist-lib/spec/local/hyperschema/index.js +73 -38
- package/dist-lib/spec/local/hyperschema/schema.json +31 -0
- package/dist-lib/spec/local/hyperschema/types.d.ts +8 -0
- package/dist-lib/spec/mesh/hyperdb/db.json +39 -2
- package/dist-lib/spec/mesh/hyperdb/index.js +120 -3
- package/dist-lib/spec/mesh/hyperdb/messages.js +1328 -838
- package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +30 -0
- package/dist-lib/spec/mesh/hyperdispatch/index.js +86 -1
- package/dist-lib/spec/mesh/hyperdispatch/messages.js +682 -287
- package/dist-lib/spec/mesh/hyperschema/index.js +682 -287
- package/dist-lib/spec/mesh/hyperschema/schema.json +201 -0
- package/dist-lib/spec/mesh/hyperschema/types.d.ts +52 -0
- package/package.json +82 -71
- package/tui/README.md +52 -0
- package/tui/app.mjs +69 -12
- package/tui/boot.mjs +27 -6
- package/tui/cli.mjs +93 -0
- package/tui/index.mjs +2 -87
- package/tui/lib/attachment-files.mjs +82 -0
- package/tui/lib/reopen-meshes.mjs +15 -0
- package/tui/lib/updater.mjs +86 -0
- package/tui/main.mjs +29 -57
- package/worker/assistant-sidecar.mjs +10 -3
- package/worker/stow-entry.mjs +3 -52
- package/dist-lib/lib/core/chat-project.d.ts +0 -2
- package/dist-lib/lib/core/chat-project.js +0 -15
|
@@ -8,18 +8,21 @@ import crypto from 'hypercore-crypto';
|
|
|
8
8
|
import HypercoreId from 'hypercore-id-encoding';
|
|
9
9
|
import ReadyResource from 'ready-resource';
|
|
10
10
|
import { Files } from './files/index.js';
|
|
11
|
+
import { LEVEL_PRIORITIES } from '@qvac/logging/constants';
|
|
11
12
|
import MeshDefinition from '../../spec/mesh/hyperdb/index.js';
|
|
12
13
|
import { Router, encode, version } from '../../spec/mesh/hyperdispatch/index.js';
|
|
14
|
+
import { raceResource } from '../race-resource.js';
|
|
15
|
+
import { firstEvent } from './first-event.js';
|
|
13
16
|
import { syncChanges } from './sync/index.js';
|
|
14
|
-
import {
|
|
17
|
+
import { ownReads } from './chat-read.js';
|
|
18
|
+
import { memoryPending, ViewUpdates } from './sync/view-updates.js';
|
|
15
19
|
import { BackgroundSync } from './sync/background.js';
|
|
16
20
|
import { buildDefaultSeed, isOwnKickedRow, isOwnRevokedRow } from './helpers/index.js';
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
19
|
-
import { devicesByWriterKey, devicesOfMember, liveDeviceForWriter } from './contract/activity.js';
|
|
21
|
+
import { signDeviceAttestation, signWriterClaim } from './device-attestation.js';
|
|
22
|
+
import { devicesOfMember, liveDeviceForWriter } from './contract/activity.js';
|
|
20
23
|
import { MESH_NAME_ROW } from './contract/mesh-name.js';
|
|
21
24
|
import { versionedOp } from './contract/oldest-version.js';
|
|
22
|
-
import { KNOWN_RULES, rulesVersion } from './contract/mesh-rules.js';
|
|
25
|
+
import { KNOWN_RULES, MESH_RULES_ROW, rulesVersion } from './contract/mesh-rules.js';
|
|
23
26
|
import { WORKSPACE_ROW } from './contract/workspace.js';
|
|
24
27
|
import { registerRoutes } from './contract/index.js';
|
|
25
28
|
import { assertRoleRoute, inRole } from './contract/roles.js';
|
|
@@ -28,7 +31,40 @@ import { buildHealthAssistant } from './health-assistant.js';
|
|
|
28
31
|
import b4a from 'b4a';
|
|
29
32
|
import { errorMessage, isRecord } from '../guards.js';
|
|
30
33
|
import { CoreError, isCoreError } from '../errors.js';
|
|
31
|
-
import {
|
|
34
|
+
import { ACTIVE_CHATS, ACTIVE_CHAT_GROUPS, AGENT_HOSTS, CHATS, CHUNK_COUNTERS, DEVICES, LINKED_WRITERS_BY_MOST_RECENT, LIVE_DEVICES_BY_WRITER_KEY, LIVE_WRITERS_BY_MOST_RECENT, MEMBERS, MESH_NAMES, MESH_RULES } from './collections.js';
|
|
35
|
+
export class AppliedOp {
|
|
36
|
+
writer;
|
|
37
|
+
length;
|
|
38
|
+
constructor(writer, length) {
|
|
39
|
+
this.writer = writer;
|
|
40
|
+
this.length = length;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const PRIORITIES = new Map(Object.entries(LEVEL_PRIORITIES));
|
|
44
|
+
function filters(log, level) {
|
|
45
|
+
const at = log.getLevel?.() ?? '';
|
|
46
|
+
return at === 'off' || LEVEL_PRIORITIES[level] > (PRIORITIES.get(at) ?? Infinity);
|
|
47
|
+
}
|
|
48
|
+
function opLog(log, applying) {
|
|
49
|
+
const tagged = (level) => (...args) => {
|
|
50
|
+
if (filters(log, level)) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const node = applying();
|
|
54
|
+
const op = node && new AppliedOp(b4a.toHex(node.key).slice(0, 8), node.length);
|
|
55
|
+
log[level](...args, ...(op ? [op] : []));
|
|
56
|
+
};
|
|
57
|
+
return {
|
|
58
|
+
error: tagged('error'),
|
|
59
|
+
warn: tagged('warn'),
|
|
60
|
+
info: tagged('info'),
|
|
61
|
+
debug: tagged('debug'),
|
|
62
|
+
getLevel: log.getLevel?.bind(log)
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
export function logErrors(log) {
|
|
66
|
+
return (err, { meshId }) => log?.error('errors', err.message, { meshId, err });
|
|
67
|
+
}
|
|
32
68
|
const [MESH_KEYPAIR, MESH_ENCRYPTION, MESH_DEVICE] = crypto.namespace('qvac-assistant/mesh', 3);
|
|
33
69
|
const DEFAULT_NAMESPACE = b4a.alloc(32);
|
|
34
70
|
export const SCHEMA_VERSION = version;
|
|
@@ -38,6 +74,13 @@ const BLOB_CORE = 'qvac-blobs';
|
|
|
38
74
|
export function meshPublicKeyFromSeed(seed) {
|
|
39
75
|
return crypto.keyPair(crypto.hash([MESH_KEYPAIR, seed])).publicKey;
|
|
40
76
|
}
|
|
77
|
+
async function sidebarRows(read, chats, memberId) {
|
|
78
|
+
const [counters] = await Promise.all([
|
|
79
|
+
Promise.all(chats.map((chat) => read.get(CHUNK_COUNTERS, { chatId: chat.id }))),
|
|
80
|
+
ownReads(read, chats, memberId)
|
|
81
|
+
]);
|
|
82
|
+
return counters;
|
|
83
|
+
}
|
|
41
84
|
async function ownCores(storage, namespace, writerKey) {
|
|
42
85
|
const keys = [crypto.discoveryKey(writerKey)];
|
|
43
86
|
for await (const discoveryKey of storage.createDiscoveryKeyStream(namespace)) {
|
|
@@ -51,14 +94,9 @@ async function ownCores(storage, namespace, writerKey) {
|
|
|
51
94
|
}
|
|
52
95
|
return keys;
|
|
53
96
|
}
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
'@qvac/members',
|
|
58
|
-
'@qvac/active-chats'
|
|
59
|
-
];
|
|
60
|
-
export async function isTrustedWriter(snap, writerKey, { ourMemberId }) {
|
|
61
|
-
const device = await liveDeviceForWriter(snap, writerKey);
|
|
97
|
+
const WARMUP_TAIL = 16;
|
|
98
|
+
const VIEW_READ_TIMEOUT = 5_000;
|
|
99
|
+
export function trustsDevice(device, { ourMemberId }) {
|
|
62
100
|
if (!device) {
|
|
63
101
|
return false;
|
|
64
102
|
}
|
|
@@ -67,6 +105,9 @@ export async function isTrustedWriter(snap, writerKey, { ourMemberId }) {
|
|
|
67
105
|
}
|
|
68
106
|
return !!device.memberId;
|
|
69
107
|
}
|
|
108
|
+
export async function isTrustedWriter(snap, writerKey, scope) {
|
|
109
|
+
return trustsDevice(await liveDeviceForWriter(snap, writerKey), scope);
|
|
110
|
+
}
|
|
70
111
|
export function claimableDevice(row) {
|
|
71
112
|
return {
|
|
72
113
|
id: row.id,
|
|
@@ -89,11 +130,16 @@ export class Mesh extends ReadyResource {
|
|
|
89
130
|
_key;
|
|
90
131
|
_writerSeed;
|
|
91
132
|
_log;
|
|
133
|
+
_applyLog;
|
|
134
|
+
_applying = null;
|
|
135
|
+
_onerror;
|
|
92
136
|
_blindPeerKeys;
|
|
93
137
|
_bootFrom;
|
|
138
|
+
_joinHeads;
|
|
94
139
|
_wakeup;
|
|
95
140
|
attachmentUrl;
|
|
96
141
|
fileIndex;
|
|
142
|
+
history;
|
|
97
143
|
_scope;
|
|
98
144
|
router;
|
|
99
145
|
_keyPair;
|
|
@@ -105,12 +151,13 @@ export class Mesh extends ReadyResource {
|
|
|
105
151
|
workspaceKey = null;
|
|
106
152
|
_updates;
|
|
107
153
|
_openedAt = 0;
|
|
108
|
-
|
|
154
|
+
_sidebarChats;
|
|
109
155
|
_joined = false;
|
|
110
156
|
_orphanedBlobSweepStatus = { state: 'pending' };
|
|
111
157
|
_bootInterrupted = false;
|
|
112
158
|
_localWriterUploadedLength = 0;
|
|
113
159
|
_ourMemberId = null;
|
|
160
|
+
_ourMembership = null;
|
|
114
161
|
_name = null;
|
|
115
162
|
_membership = {
|
|
116
163
|
members: 0,
|
|
@@ -124,8 +171,10 @@ export class Mesh extends ReadyResource {
|
|
|
124
171
|
_stallReported = false;
|
|
125
172
|
_admitted = false;
|
|
126
173
|
_refused = false;
|
|
174
|
+
_applied = new Map();
|
|
127
175
|
_kickSeen = null;
|
|
128
176
|
_birth = null;
|
|
177
|
+
_ownWriterSession = null;
|
|
129
178
|
_rulesRaised = null;
|
|
130
179
|
_autobee = null;
|
|
131
180
|
_blobs = null;
|
|
@@ -147,6 +196,16 @@ export class Mesh extends ReadyResource {
|
|
|
147
196
|
get log() {
|
|
148
197
|
return this._log;
|
|
149
198
|
}
|
|
199
|
+
reportError(err) {
|
|
200
|
+
if (this.closing || this._autobee?.closing) {
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
this._report(err);
|
|
204
|
+
}
|
|
205
|
+
_report(err) {
|
|
206
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
207
|
+
this._onerror(error, { meshId: b4a.toHex(this.key) });
|
|
208
|
+
}
|
|
150
209
|
get devicePublicKey() {
|
|
151
210
|
return this._deviceKeyPair.publicKey;
|
|
152
211
|
}
|
|
@@ -186,6 +245,9 @@ export class Mesh extends ReadyResource {
|
|
|
186
245
|
get ourMemberId() {
|
|
187
246
|
return this._ourMemberId;
|
|
188
247
|
}
|
|
248
|
+
get ourMembership() {
|
|
249
|
+
return this._ourMembership;
|
|
250
|
+
}
|
|
189
251
|
get ourDeviceId() {
|
|
190
252
|
return this._deviceId;
|
|
191
253
|
}
|
|
@@ -232,15 +294,16 @@ export class Mesh extends ReadyResource {
|
|
|
232
294
|
async refreshOurMember() {
|
|
233
295
|
if (!this._deviceId)
|
|
234
296
|
return null;
|
|
235
|
-
|
|
297
|
+
return this.recordOurMember(await this.view.get(DEVICES, { id: this._deviceId }));
|
|
298
|
+
}
|
|
299
|
+
recordOurMember(own) {
|
|
236
300
|
this._ourMemberId = own?.memberId ?? null;
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
memberId: own.memberId,
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
this.emit('membership', membership);
|
|
301
|
+
this._ourMembership =
|
|
302
|
+
own?.memberId && own.memberProof
|
|
303
|
+
? { memberId: own.memberId, proof: own.memberProof, attestedAt: own.attestedAt || 0 }
|
|
304
|
+
: null;
|
|
305
|
+
if (this._ourMembership) {
|
|
306
|
+
this.emit('membership', this._ourMembership);
|
|
244
307
|
}
|
|
245
308
|
return own;
|
|
246
309
|
}
|
|
@@ -271,16 +334,23 @@ export class Mesh extends ReadyResource {
|
|
|
271
334
|
this._key = opts.key ?? null;
|
|
272
335
|
this._writerSeed = opts.writerSeed;
|
|
273
336
|
this._log = opts.logger ?? null;
|
|
337
|
+
this._applyLog = this._log && opLog(this._log, () => this._applying);
|
|
338
|
+
this._onerror = opts.onerror ?? logErrors(this._log);
|
|
274
339
|
this._slowWriteMs = opts.slowWriteMs ?? SLOW_WRITE_MS;
|
|
275
340
|
this._slowDrainMs = opts.slowDrainMs ?? SLOW_DRAIN_MS;
|
|
276
341
|
this._blindPeerKeys = opts.blindPeerKeys ?? [];
|
|
277
342
|
this._bootFrom = opts.bootFrom;
|
|
278
|
-
this.
|
|
343
|
+
this._joinHeads = opts.joinHeads ?? [];
|
|
279
344
|
this._deviceId = opts.deviceId ?? null;
|
|
280
345
|
this._wakeup = opts.wakeup;
|
|
281
346
|
this.attachmentUrl = opts.attachmentUrl ?? null;
|
|
282
347
|
this.fileIndex = opts.fileIndex ?? null;
|
|
283
|
-
this.
|
|
348
|
+
this.history = opts.history ?? null;
|
|
349
|
+
this._sidebarChats = opts.sidebarChats ?? null;
|
|
350
|
+
this._updates = new ViewUpdates((update) => this._syncUpdate(update), {
|
|
351
|
+
pending: opts.pendingUpdates ?? memoryPending(),
|
|
352
|
+
onerror: (err) => this.reportError(err)
|
|
353
|
+
});
|
|
284
354
|
this.router = new Router();
|
|
285
355
|
this._keyPair = crypto.keyPair(crypto.hash([MESH_KEYPAIR, this._seed]));
|
|
286
356
|
this._deviceKeyPair = crypto.keyPair(crypto.hash([MESH_DEVICE, this._seed]));
|
|
@@ -319,6 +389,7 @@ export class Mesh extends ReadyResource {
|
|
|
319
389
|
const writerKeyPair = await namespace.createKeyPair(this._writerSeed ?? 'local');
|
|
320
390
|
const autobee = new Autobee(namespace, this._key, {
|
|
321
391
|
open: this._autobeeOpen.bind(this),
|
|
392
|
+
close: this._autobeeClose.bind(this),
|
|
322
393
|
apply: this._autobeeApply.bind(this),
|
|
323
394
|
update: this._autobeeUpdate.bind(this),
|
|
324
395
|
warmup: this._autobeeWarmup.bind(this),
|
|
@@ -333,18 +404,22 @@ export class Mesh extends ReadyResource {
|
|
|
333
404
|
strictReindex: true
|
|
334
405
|
});
|
|
335
406
|
this._autobee = autobee;
|
|
336
|
-
this.autobee.on('error', (err) => this.
|
|
407
|
+
this.autobee.on('error', (err) => this._report(err));
|
|
408
|
+
this.autobee.on('warning', (err) => this.reportError(err));
|
|
337
409
|
this.autobee.on('writable', () => {
|
|
338
|
-
void this.checkOwnAdmission().catch(
|
|
410
|
+
void this.checkOwnAdmission().catch((err) => this.reportError(err));
|
|
339
411
|
this._raiseRulesLater();
|
|
340
412
|
});
|
|
341
|
-
this.autobee.on('move-to', (to, from) =>
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
413
|
+
this.autobee.on('move-to', (to, from) => {
|
|
414
|
+
this._applied.clear();
|
|
415
|
+
this._log?.info(this._scope, 'fast-forwarded onto a head', {
|
|
416
|
+
from: from?.length ?? 0,
|
|
417
|
+
to: to?.length ?? 0,
|
|
418
|
+
fastForwards: this.autobee.stats.fastForwards,
|
|
419
|
+
sinceOpenMs: this._sinceOpen()
|
|
420
|
+
});
|
|
421
|
+
});
|
|
422
|
+
this.autobee.on('interrupt', (err) => this.reportError(err));
|
|
348
423
|
if (this._bootInterrupted) {
|
|
349
424
|
void this.autobee.close().catch(noop);
|
|
350
425
|
}
|
|
@@ -355,7 +430,10 @@ export class Mesh extends ReadyResource {
|
|
|
355
430
|
await blobs.close().catch(noop);
|
|
356
431
|
throw err;
|
|
357
432
|
}
|
|
358
|
-
this.autobee.bootstrap.on('peer-add', () =>
|
|
433
|
+
this.autobee.bootstrap.on('peer-add', () => {
|
|
434
|
+
this.emit('peer-add');
|
|
435
|
+
this._updates.bump();
|
|
436
|
+
});
|
|
359
437
|
this.openedEmpty = !this.view.db.head()?.length;
|
|
360
438
|
this.autobee.local.on('upload', (index) => {
|
|
361
439
|
this._localWriterUploadedLength = Math.max(this._localWriterUploadedLength, index + 1);
|
|
@@ -370,13 +448,12 @@ export class Mesh extends ReadyResource {
|
|
|
370
448
|
writers: this.writerCount,
|
|
371
449
|
sinceOpenMs: this._sinceOpen()
|
|
372
450
|
});
|
|
373
|
-
|
|
374
|
-
.start()
|
|
375
|
-
.catch((err) => this._log?.warn(this._scope, 'resuming the view sync failed', err));
|
|
451
|
+
this._updates.start();
|
|
376
452
|
if (!this.foreign) {
|
|
377
453
|
void this._sweepOrphanedBlobs(this.blobs.core.length);
|
|
378
454
|
}
|
|
379
455
|
this._raiseRulesLater();
|
|
456
|
+
this._announceOwnWriter();
|
|
380
457
|
this.files.start({ deviceId: this._deviceId });
|
|
381
458
|
}
|
|
382
459
|
startBlindPeering(peering) {
|
|
@@ -407,11 +484,29 @@ export class Mesh extends ReadyResource {
|
|
|
407
484
|
return this._bootFrom?.length ?? null;
|
|
408
485
|
}
|
|
409
486
|
async wakeHeldWriters() {
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
487
|
+
const hints = new Map();
|
|
488
|
+
const hint = (key) => {
|
|
489
|
+
if (!b4a.equals(key, this.localWriterKey)) {
|
|
490
|
+
hints.set(b4a.toHex(key), { key, length: 0 });
|
|
491
|
+
}
|
|
492
|
+
};
|
|
493
|
+
const snap = this.view.snapshot();
|
|
494
|
+
try {
|
|
495
|
+
for await (const device of snap.find(LIVE_DEVICES_BY_WRITER_KEY)) {
|
|
496
|
+
if (device.writerKey) {
|
|
497
|
+
hint(device.writerKey);
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
for await (const host of snap.find(AGENT_HOSTS)) {
|
|
501
|
+
hint(host.writerKey);
|
|
413
502
|
}
|
|
414
503
|
}
|
|
504
|
+
finally {
|
|
505
|
+
await snap.close();
|
|
506
|
+
}
|
|
507
|
+
if (hints.size) {
|
|
508
|
+
this.autobee.hintWakeup([...hints.values()]);
|
|
509
|
+
}
|
|
415
510
|
}
|
|
416
511
|
get writerCount() {
|
|
417
512
|
let count = 0;
|
|
@@ -461,20 +556,26 @@ export class Mesh extends ReadyResource {
|
|
|
461
556
|
}
|
|
462
557
|
interruptBoot() {
|
|
463
558
|
this._bootInterrupted = true;
|
|
559
|
+
this._updates.close();
|
|
464
560
|
if (this._autobee) {
|
|
465
561
|
void this._autobee.close().catch(noop);
|
|
466
562
|
}
|
|
467
563
|
}
|
|
468
|
-
|
|
564
|
+
close() {
|
|
469
565
|
this._updates.close();
|
|
566
|
+
return super.close();
|
|
567
|
+
}
|
|
568
|
+
async _close() {
|
|
470
569
|
this._log?.info(this._scope, 'closing', {
|
|
471
570
|
sinceOpenMs: this._sinceOpen(),
|
|
472
571
|
...this._lengths(),
|
|
473
572
|
...this.autobee.stats
|
|
474
573
|
});
|
|
475
574
|
this._background.stop();
|
|
575
|
+
this._ownWriterSession?.destroy();
|
|
476
576
|
await this._files?.close();
|
|
477
577
|
await Promise.all([this.autobee.close(), this.blobs.close()]);
|
|
578
|
+
await this.view.close();
|
|
478
579
|
this._blindPeering = null;
|
|
479
580
|
}
|
|
480
581
|
async _sweepOrphanedBlobs(blobLength) {
|
|
@@ -525,6 +626,7 @@ export class Mesh extends ReadyResource {
|
|
|
525
626
|
assertJoined(this.autobee, opts);
|
|
526
627
|
const write = this.autobee.append(encode(`@qvac/${name}`, versionedOp(name, payload)), opts);
|
|
527
628
|
await this._watchStall(write, { what: 'mesh write', op: name, ms: this._slowWriteMs });
|
|
629
|
+
assertWritten(this.autobee);
|
|
528
630
|
}
|
|
529
631
|
async _watchStall(pending, watch) {
|
|
530
632
|
const started = Date.now();
|
|
@@ -571,8 +673,8 @@ export class Mesh extends ReadyResource {
|
|
|
571
673
|
}
|
|
572
674
|
}
|
|
573
675
|
async _reportStall({ what, op }, { started, reports }) {
|
|
574
|
-
const { stats,
|
|
575
|
-
const missing = await this._coresNotHeld([
|
|
676
|
+
const { stats, writable } = this.autobee;
|
|
677
|
+
const missing = await this._coresNotHeld([this.autobee.bee]).catch(noop);
|
|
576
678
|
const fastForwarding = await this._fastForwardHeld().catch(noop);
|
|
577
679
|
const writers = writable ? undefined : await this._writersHeld().catch(noop);
|
|
578
680
|
this._log?.warn(this._scope, `${what} still pending`, {
|
|
@@ -646,9 +748,8 @@ export class Mesh extends ReadyResource {
|
|
|
646
748
|
return;
|
|
647
749
|
}
|
|
648
750
|
this._rulesRaised = this._raiseRules().catch((err) => {
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
}
|
|
751
|
+
this._rulesRaised = null;
|
|
752
|
+
this.reportError(err);
|
|
652
753
|
});
|
|
653
754
|
}
|
|
654
755
|
async _raiseRules() {
|
|
@@ -689,6 +790,29 @@ export class Mesh extends ReadyResource {
|
|
|
689
790
|
async _appendBirthOp(identity, device, { member, sharing = false, defaults = true, modelName, workspace, project } = {}) {
|
|
690
791
|
if (this.autobee.local.length > 0)
|
|
691
792
|
return;
|
|
793
|
+
if (this._joinHeads.length > 0) {
|
|
794
|
+
this._log?.info(this._scope, 'join waits on the invite heads', {
|
|
795
|
+
heads: this._joinHeads.length
|
|
796
|
+
});
|
|
797
|
+
this.autobee.hintWakeup(this._joinHeads);
|
|
798
|
+
await this.autobee.updated();
|
|
799
|
+
while (!this.closing) {
|
|
800
|
+
const advanced = firstEvent([
|
|
801
|
+
[this, 'join-heads'],
|
|
802
|
+
[this, 'close']
|
|
803
|
+
]);
|
|
804
|
+
const heads = this._joinHeads;
|
|
805
|
+
const held = await Promise.all(heads.map(async (head) => (await this.autobee.system.has(head)) ||
|
|
806
|
+
!!(await this.autobee.system.get(head.key))?.isRemoved));
|
|
807
|
+
if (held.every(Boolean) && heads === this._joinHeads) {
|
|
808
|
+
break;
|
|
809
|
+
}
|
|
810
|
+
await advanced;
|
|
811
|
+
}
|
|
812
|
+
if (this.closing) {
|
|
813
|
+
throw new CoreError('MESH_CLOSED_BEFORE_WRITE');
|
|
814
|
+
}
|
|
815
|
+
}
|
|
692
816
|
const { publicKey: deviceId, secretKey } = 'secretKey' in identity ? identity : { publicKey: identity, secretKey: null };
|
|
693
817
|
const localKey = this.autobee.local.key;
|
|
694
818
|
const row = device
|
|
@@ -715,10 +839,7 @@ export class Mesh extends ReadyResource {
|
|
|
715
839
|
birth.dispatch('name-mesh', { name, namedAt: row.joinedAt });
|
|
716
840
|
}
|
|
717
841
|
if (project && member) {
|
|
718
|
-
birth.dispatch('put-project-member', {
|
|
719
|
-
memberId: member.member.id,
|
|
720
|
-
addedAt: project.upgraded ? undefined : row.joinedAt
|
|
721
|
-
});
|
|
842
|
+
birth.dispatch('put-project-member', { memberId: member.member.id });
|
|
722
843
|
birth.dispatch('name-mesh', { name: project.name, namedAt: row.joinedAt });
|
|
723
844
|
}
|
|
724
845
|
if (!sharing) {
|
|
@@ -741,6 +862,29 @@ export class Mesh extends ReadyResource {
|
|
|
741
862
|
join.dispatch('attest-device', this.attestDevice(deviceId));
|
|
742
863
|
}
|
|
743
864
|
await join.flush({ optimistic: true });
|
|
865
|
+
this._announceOwnWriter();
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
joinAfter(heads) {
|
|
869
|
+
this._joinHeads = heads;
|
|
870
|
+
this.autobee.hintWakeup(heads);
|
|
871
|
+
this.emit('join-heads');
|
|
872
|
+
}
|
|
873
|
+
_announceOwnWriter() {
|
|
874
|
+
if (!this._wakeup || this._ownWriterSession || this.minted || this.localLength === 0) {
|
|
875
|
+
return;
|
|
876
|
+
}
|
|
877
|
+
const announce = (peer, session) => session.announce(peer, [{ key: this.localWriterKey, length: this.localLength }]);
|
|
878
|
+
const session = this._wakeup.session(this._keyPair.publicKey, {
|
|
879
|
+
active: false,
|
|
880
|
+
discoveryKey: this.discoveryKey,
|
|
881
|
+
onpeeractive: announce
|
|
882
|
+
});
|
|
883
|
+
this._ownWriterSession = session;
|
|
884
|
+
for (const peer of session.peers) {
|
|
885
|
+
if (peer.active) {
|
|
886
|
+
announce(peer, session);
|
|
887
|
+
}
|
|
744
888
|
}
|
|
745
889
|
}
|
|
746
890
|
_bornKind(project) {
|
|
@@ -794,6 +938,9 @@ export class Mesh extends ReadyResource {
|
|
|
794
938
|
const taken = Math.max(this._localWriterUploadedLength, ...peers.map((peer) => peer.remoteLength));
|
|
795
939
|
return Math.max(0, this.autobee.local.length - taken);
|
|
796
940
|
}
|
|
941
|
+
appliedLength(writerKey) {
|
|
942
|
+
return this._applied.get(b4a.toHex(writerKey));
|
|
943
|
+
}
|
|
797
944
|
async suspend() {
|
|
798
945
|
this._background.pause();
|
|
799
946
|
await this._files?.suspend();
|
|
@@ -811,6 +958,14 @@ export class Mesh extends ReadyResource {
|
|
|
811
958
|
pickedFrom = null;
|
|
812
959
|
syncChatOrder = null;
|
|
813
960
|
chatOrderBuilt = Promise.resolve();
|
|
961
|
+
chatOrderStale = false;
|
|
962
|
+
read() {
|
|
963
|
+
return this._heldOrServed(this.view);
|
|
964
|
+
}
|
|
965
|
+
_heldOrServed(db) {
|
|
966
|
+
const peers = this.autobee.bootstrap.peers.length;
|
|
967
|
+
return db.snapshot({ timeout: VIEW_READ_TIMEOUT, wait: peers > 0 });
|
|
968
|
+
}
|
|
814
969
|
watch(listener) {
|
|
815
970
|
this.view.watch(listener);
|
|
816
971
|
}
|
|
@@ -818,11 +973,11 @@ export class Mesh extends ReadyResource {
|
|
|
818
973
|
this.view.unwatch(listener);
|
|
819
974
|
}
|
|
820
975
|
async _autobeeIsTrusted(key, reference) {
|
|
821
|
-
if (this.closing)
|
|
822
|
-
|
|
823
|
-
|
|
976
|
+
if (this.autobee.closing) {
|
|
977
|
+
throw closedUnderHook();
|
|
978
|
+
}
|
|
824
979
|
try {
|
|
825
|
-
const trusted = await
|
|
980
|
+
const trusted = await this._trusts(reference.db, key);
|
|
826
981
|
if (!trusted) {
|
|
827
982
|
this._log?.debug(this._scope, 'writer is no live device of this mesh', {
|
|
828
983
|
writerKey: b4a.toHex(key).slice(0, 16)
|
|
@@ -830,71 +985,97 @@ export class Mesh extends ReadyResource {
|
|
|
830
985
|
}
|
|
831
986
|
return trusted;
|
|
832
987
|
}
|
|
833
|
-
|
|
834
|
-
|
|
988
|
+
catch (err) {
|
|
989
|
+
this._log?.debug(this._scope, 'judging a writer failed', err);
|
|
990
|
+
return false;
|
|
835
991
|
}
|
|
836
992
|
}
|
|
837
993
|
async _autobeeMostRecentTrusted(target, reference) {
|
|
838
|
-
if (this.closing)
|
|
994
|
+
if (this.autobee.closing) {
|
|
995
|
+
throw closedUnderHook();
|
|
996
|
+
}
|
|
997
|
+
try {
|
|
998
|
+
const writer = reference
|
|
999
|
+
? await this._newestTrustedOf(target.db, reference.db)
|
|
1000
|
+
: await this._newestLinked(target.db);
|
|
1001
|
+
if (!writer) {
|
|
1002
|
+
this._log?.debug(this._scope, 'vouching for no head: no live writer has been active');
|
|
1003
|
+
return null;
|
|
1004
|
+
}
|
|
1005
|
+
this._log?.debug(this._scope, 'vouching for a head', {
|
|
1006
|
+
writerKey: b4a.toHex(writer.writerKey).slice(0, 16),
|
|
1007
|
+
length: writer.length
|
|
1008
|
+
});
|
|
1009
|
+
return { key: writer.writerKey, length: writer.length };
|
|
1010
|
+
}
|
|
1011
|
+
catch (err) {
|
|
1012
|
+
this._log?.debug(this._scope, 'vouching for a head failed', err);
|
|
839
1013
|
return null;
|
|
840
|
-
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
_newestLinked(db) {
|
|
1017
|
+
return db.findOne(this._trustedIndex(), { reverse: true });
|
|
1018
|
+
}
|
|
1019
|
+
_trustedIndex() {
|
|
1020
|
+
return this.ourMemberId ? LINKED_WRITERS_BY_MOST_RECENT : LIVE_WRITERS_BY_MOST_RECENT;
|
|
1021
|
+
}
|
|
1022
|
+
async _newestTrustedOf(candidate, ours) {
|
|
1023
|
+
const held = this._heldOrServed(ours);
|
|
841
1024
|
try {
|
|
842
|
-
for await (const writer of
|
|
1025
|
+
for await (const writer of candidate.find(this._trustedIndex(), {
|
|
843
1026
|
reverse: true
|
|
844
1027
|
})) {
|
|
845
|
-
if (
|
|
846
|
-
|
|
847
|
-
this._log?.debug(this._scope, 'vouching for a head', {
|
|
848
|
-
writerKey: b4a.toHex(writer.writerKey).slice(0, 16),
|
|
849
|
-
length: writer.length
|
|
850
|
-
});
|
|
851
|
-
return { key: writer.writerKey, length: writer.length };
|
|
1028
|
+
if (await isTrustedWriter(held, writer.writerKey, this)) {
|
|
1029
|
+
return writer;
|
|
852
1030
|
}
|
|
853
1031
|
}
|
|
854
|
-
this._log?.debug(this._scope, 'vouching for no head: no live writer has been active');
|
|
855
1032
|
return null;
|
|
856
1033
|
}
|
|
857
1034
|
finally {
|
|
858
|
-
await
|
|
1035
|
+
await held.close();
|
|
859
1036
|
}
|
|
860
1037
|
}
|
|
861
|
-
async
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
return false;
|
|
866
|
-
}
|
|
867
|
-
const device = await liveDeviceForWriter(target.db, writerKey);
|
|
868
|
-
if (!device?.deviceSignature) {
|
|
869
|
-
return false;
|
|
1038
|
+
async _trusts(ours, writerKey) {
|
|
1039
|
+
const held = this._heldOrServed(ours);
|
|
1040
|
+
try {
|
|
1041
|
+
return await isTrustedWriter(held, writerKey, this);
|
|
870
1042
|
}
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
deviceId: device.id,
|
|
874
|
-
writerKey,
|
|
875
|
-
joinedAt: device.joinedAt
|
|
876
|
-
};
|
|
877
|
-
if (!verifyWriterClaim(device.deviceSignature, binding) ||
|
|
878
|
-
!(await this._trustedDevice(device.id))) {
|
|
879
|
-
return false;
|
|
1043
|
+
finally {
|
|
1044
|
+
await held.close();
|
|
880
1045
|
}
|
|
881
|
-
this._log?.debug(this._scope, 'writer vouched by its row', {
|
|
882
|
-
writerKey: b4a.toHex(writerKey).slice(0, 16),
|
|
883
|
-
deviceId: b4a.toHex(device.id).slice(0, 16)
|
|
884
|
-
});
|
|
885
|
-
return true;
|
|
886
1046
|
}
|
|
887
1047
|
_autobeeOpen(bee) {
|
|
888
1048
|
const db = HyperDB.bee2(bee, MeshDefinition, { autoUpdate: true });
|
|
889
1049
|
return { db };
|
|
890
1050
|
}
|
|
1051
|
+
_autobeeClose(view) {
|
|
1052
|
+
return view === this._autobee?.view ? Promise.resolve() : view.db.close();
|
|
1053
|
+
}
|
|
891
1054
|
async _autobeeWarmup(view) {
|
|
892
1055
|
let snap = null;
|
|
893
1056
|
try {
|
|
894
1057
|
const read = view.db.snapshot();
|
|
895
1058
|
snap = read;
|
|
896
|
-
const
|
|
897
|
-
const
|
|
1059
|
+
const tail = { reverse: true, limit: WARMUP_TAIL };
|
|
1060
|
+
const [own, byId, name, rules, live, linked, chats, groups] = await Promise.all([
|
|
1061
|
+
liveDeviceForWriter(read, this.localWriterKey),
|
|
1062
|
+
this._deviceId ? read.get(DEVICES, { id: this._deviceId }) : null,
|
|
1063
|
+
read.get(MESH_NAMES, { id: MESH_NAME_ROW }),
|
|
1064
|
+
read.get(MESH_RULES, { id: MESH_RULES_ROW }),
|
|
1065
|
+
read.findOne(LIVE_WRITERS_BY_MOST_RECENT, { reverse: true }),
|
|
1066
|
+
read.findOne(LINKED_WRITERS_BY_MOST_RECENT, { reverse: true }),
|
|
1067
|
+
this._sidebarTop(read),
|
|
1068
|
+
read.find(ACTIVE_CHAT_GROUPS, tail).toArray()
|
|
1069
|
+
]);
|
|
1070
|
+
const memberId = byId?.memberId ?? own?.memberId ?? null;
|
|
1071
|
+
const newest = [live, linked].filter((row) => row !== null);
|
|
1072
|
+
const [member, counters, devices] = await Promise.all([
|
|
1073
|
+
memberId ? read.get(MEMBERS, { id: memberId }) : null,
|
|
1074
|
+
sidebarRows(read, chats, memberId),
|
|
1075
|
+
Promise.all(newest.map((writer) => liveDeviceForWriter(read, writer.writerKey)))
|
|
1076
|
+
]);
|
|
1077
|
+
const rows = [own, byId, name, rules, ...newest, member, ...devices, ...counters].filter((row) => row !== null);
|
|
1078
|
+
const warmed = rows.length + chats.length + groups.length;
|
|
898
1079
|
this._log?.info(this._scope, 'warmed candidate view', {
|
|
899
1080
|
warmed,
|
|
900
1081
|
length: view.db.db.head()?.length ?? 0,
|
|
@@ -904,11 +1085,20 @@ export class Mesh extends ReadyResource {
|
|
|
904
1085
|
}
|
|
905
1086
|
catch (err) {
|
|
906
1087
|
this._log?.warn(this._scope, 'warmup failed', err);
|
|
1088
|
+
throw err;
|
|
907
1089
|
}
|
|
908
1090
|
finally {
|
|
909
1091
|
await snap?.close();
|
|
910
1092
|
}
|
|
911
1093
|
}
|
|
1094
|
+
async _sidebarTop(read) {
|
|
1095
|
+
const kept = (await this._sidebarChats?.(WARMUP_TAIL)) ?? [];
|
|
1096
|
+
if (kept.length === 0) {
|
|
1097
|
+
return read.find(ACTIVE_CHATS, { reverse: true, limit: WARMUP_TAIL }).toArray();
|
|
1098
|
+
}
|
|
1099
|
+
const rows = await Promise.all(kept.map(({ chatId }) => read.get(CHATS, { id: chatId })));
|
|
1100
|
+
return rows.filter((row) => row !== null);
|
|
1101
|
+
}
|
|
912
1102
|
async _autobeeApply(nodes, view, host) {
|
|
913
1103
|
const tx = view.db.transaction();
|
|
914
1104
|
try {
|
|
@@ -918,6 +1108,9 @@ export class Mesh extends ReadyResource {
|
|
|
918
1108
|
await this._applyNode(node, { tx, host });
|
|
919
1109
|
}
|
|
920
1110
|
await tx.flush();
|
|
1111
|
+
for (const node of nodes) {
|
|
1112
|
+
this._applied.set(b4a.toHex(node.key), node.length);
|
|
1113
|
+
}
|
|
921
1114
|
}
|
|
922
1115
|
catch (err) {
|
|
923
1116
|
await tx.close();
|
|
@@ -932,6 +1125,7 @@ export class Mesh extends ReadyResource {
|
|
|
932
1125
|
}
|
|
933
1126
|
}
|
|
934
1127
|
async _applyNode(node, { tx, host }) {
|
|
1128
|
+
this._applying = node;
|
|
935
1129
|
try {
|
|
936
1130
|
await this.router.dispatch(node.value, {
|
|
937
1131
|
tx,
|
|
@@ -939,7 +1133,7 @@ export class Mesh extends ReadyResource {
|
|
|
939
1133
|
node,
|
|
940
1134
|
meshPublicKey: this.publicKey,
|
|
941
1135
|
meshDevicePublicKey: this.devicePublicKey,
|
|
942
|
-
log: this.
|
|
1136
|
+
log: this._applyLog
|
|
943
1137
|
});
|
|
944
1138
|
}
|
|
945
1139
|
catch (err) {
|
|
@@ -948,18 +1142,20 @@ export class Mesh extends ReadyResource {
|
|
|
948
1142
|
}
|
|
949
1143
|
this._halt(err, host);
|
|
950
1144
|
}
|
|
1145
|
+
finally {
|
|
1146
|
+
this._applying = null;
|
|
1147
|
+
}
|
|
951
1148
|
}
|
|
952
1149
|
_halt(err, host) {
|
|
953
1150
|
this._log?.warn(this._scope, 'halting apply: the mesh moved past this build', err);
|
|
954
1151
|
return host.interrupt(err);
|
|
955
1152
|
}
|
|
956
1153
|
async _autobeeUpdate(_view, changes) {
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
return;
|
|
1154
|
+
if (this._joinHeads.length > 0 && this.autobee.local.length === 0) {
|
|
1155
|
+
this.emit('join-heads');
|
|
960
1156
|
}
|
|
961
|
-
const
|
|
962
|
-
const
|
|
1157
|
+
const change = changes.get('view');
|
|
1158
|
+
const { from } = change;
|
|
963
1159
|
await this._updates.push({
|
|
964
1160
|
from,
|
|
965
1161
|
to: change.to?.length ?? 0,
|
|
@@ -974,21 +1170,15 @@ export class Mesh extends ReadyResource {
|
|
|
974
1170
|
return null;
|
|
975
1171
|
const started = Date.now();
|
|
976
1172
|
let reached = null;
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
reached = await syncChanges(this, this.autobee.view, from);
|
|
980
|
-
}
|
|
981
|
-
else {
|
|
982
|
-
this._arrive();
|
|
983
|
-
}
|
|
984
|
-
if (this._admitted && !this.autobee.writable) {
|
|
985
|
-
await this.checkOwnAdmission();
|
|
986
|
-
}
|
|
1173
|
+
if (from && incremental) {
|
|
1174
|
+
reached = await syncChanges(this, this.autobee.view, from);
|
|
987
1175
|
}
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
1176
|
+
else {
|
|
1177
|
+
await this.history?.edited(null);
|
|
1178
|
+
this._arrive();
|
|
1179
|
+
}
|
|
1180
|
+
if (this._admitted && !this.autobee.writable) {
|
|
1181
|
+
await this.checkOwnAdmission();
|
|
992
1182
|
}
|
|
993
1183
|
this._log?.debug(this._scope, 'view synced', {
|
|
994
1184
|
from: from?.length ?? 0,
|
|
@@ -1008,11 +1198,7 @@ export class Mesh extends ReadyResource {
|
|
|
1008
1198
|
this.emit('agents-dirty');
|
|
1009
1199
|
this.emit('skill-secrets-dirty');
|
|
1010
1200
|
this.emit('meshes-dirty', true);
|
|
1011
|
-
void this._readLanding().catch((err) =>
|
|
1012
|
-
if (!this.closing) {
|
|
1013
|
-
this._log?.warn(this._scope, 'reading the landed view failed', err);
|
|
1014
|
-
}
|
|
1015
|
-
});
|
|
1201
|
+
void this._readLanding().catch((err) => this.reportError(err));
|
|
1016
1202
|
}
|
|
1017
1203
|
async _readLanding() {
|
|
1018
1204
|
const started = Date.now();
|
|
@@ -1039,7 +1225,8 @@ export class Mesh extends ReadyResource {
|
|
|
1039
1225
|
if (!this._admitted || !this._deviceId) {
|
|
1040
1226
|
return;
|
|
1041
1227
|
}
|
|
1042
|
-
|
|
1228
|
+
const { localWriter } = this.autobee.writers;
|
|
1229
|
+
if (!localWriter.isRemoved) {
|
|
1043
1230
|
return;
|
|
1044
1231
|
}
|
|
1045
1232
|
const own = await this.view.get(DEVICES, { id: this._deviceId });
|
|
@@ -1101,8 +1288,8 @@ export class Mesh extends ReadyResource {
|
|
|
1101
1288
|
return isOwnKickedRow(this, own) ? 'kicked' : 'left';
|
|
1102
1289
|
}
|
|
1103
1290
|
async ownWriterRefused() {
|
|
1104
|
-
const
|
|
1105
|
-
if (!
|
|
1291
|
+
const { localWriter } = this.autobee.writers;
|
|
1292
|
+
if (!localWriter.isRemoved) {
|
|
1106
1293
|
return false;
|
|
1107
1294
|
}
|
|
1108
1295
|
return !(await liveDeviceForWriter(this.view, this.localWriterKey));
|
|
@@ -1143,6 +1330,12 @@ export class MeshBatch {
|
|
|
1143
1330
|
this._ops = [];
|
|
1144
1331
|
this._names = [];
|
|
1145
1332
|
await this._watch(names.join(','), this._autobee.append(ops, opts));
|
|
1333
|
+
assertWritten(this._autobee);
|
|
1334
|
+
}
|
|
1335
|
+
}
|
|
1336
|
+
function assertWritten(autobee) {
|
|
1337
|
+
if (autobee.closing && autobee.appending) {
|
|
1338
|
+
throw new CoreError('MESH_CLOSED_BEFORE_WRITE');
|
|
1146
1339
|
}
|
|
1147
1340
|
}
|
|
1148
1341
|
function noop() { }
|
|
@@ -1161,6 +1354,9 @@ function viewCoreOf(head) {
|
|
|
1161
1354
|
function isIncremental(change) {
|
|
1162
1355
|
return isRecord(change) && change.incremental === true;
|
|
1163
1356
|
}
|
|
1357
|
+
function closedUnderHook() {
|
|
1358
|
+
return new Error('Autobee closed under a trust hook');
|
|
1359
|
+
}
|
|
1164
1360
|
function assertJoined(autobee, opts) {
|
|
1165
1361
|
if (!opts?.optimistic && !autobee.writable) {
|
|
1166
1362
|
throw new CoreError('MESH_STILL_JOINING');
|
|
@@ -1171,13 +1367,6 @@ function openedPart(part, name) {
|
|
|
1171
1367
|
throw new Error(`mesh ${name} read before open`);
|
|
1172
1368
|
return part;
|
|
1173
1369
|
}
|
|
1174
|
-
async function drain(stream) {
|
|
1175
|
-
let count = 0;
|
|
1176
|
-
for await (const _row of stream) {
|
|
1177
|
-
count++;
|
|
1178
|
-
}
|
|
1179
|
-
return count;
|
|
1180
|
-
}
|
|
1181
1370
|
const NEWER_BUILD_MESSAGES = new Set(['Unsupported version', 'Unknown enum', 'Out of bounds']);
|
|
1182
1371
|
function isInterrupt(err) {
|
|
1183
1372
|
return err instanceof Error && err.message === 'Apply interrupted';
|