@qvac/core 0.7.2 → 0.7.4

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 CHANGED
@@ -13,6 +13,93 @@ release.
13
13
 
14
14
  ## [Unreleased]
15
15
 
16
+ ## [0.7.4] - 2026-10-13
17
+
18
+ ### Added
19
+
20
+ - A mesh write still pending after five seconds, or a mesh's first drain
21
+ still pending after fifteen, logs a warning once: the ops a write carries,
22
+ the drain's counters, and every core the views read through that this device
23
+ holds only in part, with how many peers could serve it, and which blind peers
24
+ it has reached. Writes queued behind
25
+ the same stall do not repeat it, and it logs again when the stall settles or
26
+ the write fails. A drain parked on data no connected peer holds used to say
27
+ nothing, so a hung send, stop, delete or leave left no trace in an exported
28
+ log.
29
+ - Leaving a mesh logs when it starts, so an exported log shows the attempt even
30
+ when the leave never finishes. (#352)
31
+
32
+ - Model routes reach another device of this account (QVAC-25101). `listModels`,
33
+ `modelsWatch`, `loadModel`, `unloadModel`, `deleteModel`, `retryModelDownload`,
34
+ `clearModelDownload`, `modelUsers`, the custom-model routes and the model
35
+ watches take an optional `deviceId`, and Core runs the call on that device over
36
+ the `qvac/device-control` peer channel, so fit, memory, disk and the download
37
+ queue are that device's. Only a live device of this account's own member is
38
+ served, and a custom model added from another device must come from a link.
39
+ While the device is away a call rejects with `the device is not reachable`;
40
+ `meshStatusWatch`'s `peerDeviceIds` says when it is connected again. (#280)
41
+
42
+ ```js
43
+ await engine.loadModel({ name: 'Qwen3.5-9B-Q4_K_M', deviceId: desktop.id })
44
+ ```
45
+
46
+ - A phone changes a paired desktop's own model settings and pauses its
47
+ downloads. `configGet`, `configSet` and `configWatch` take an optional
48
+ `deviceId` and reach `model.idleUnloadMinutes` and `model.memoryCapPercent` on
49
+ that device; any other key is refused there, since the same store holds
50
+ integration tokens. `setModelPaused` takes `deviceId` too, so a download on the
51
+ desktop pauses there rather than on the phone, until the desktop restarts. (#359)
52
+
53
+ ```js
54
+ import { MODEL_MEMORY_CAP_KEY } from '@qvac/core/constants'
55
+
56
+ await engine.configSet({ key: MODEL_MEMORY_CAP_KEY, value: 60, deviceId: desktop.id })
57
+ await engine.setModelPaused({ name: 'Qwen3.5-9B-Q4_K_M', paused: true, deviceId: desktop.id })
58
+ ```
59
+
60
+ - `duck`: `/device use <n|name>` points every `/models` command at another
61
+ device of this account, and `/device list` shows each one, whether it is
62
+ connected and how many models it has downloaded. `/models idle`,
63
+ `/models cap` and `/models settings` set and show a device's idle limit and
64
+ memory cap, and `/models pause` / `/models resume` hold a download. (#360)
65
+
66
+ ### Changed
67
+
68
+ - `loadModel`, `unloadModel`, `deleteModel`, `modelUsers`,
69
+ `clearModelDownload`, `retryModelDownload` and `removeCustomModel` had a
70
+ request of `name` alone; its new optional `deviceId` adds a flags byte, so
71
+ this Core cannot decode those calls from a `Client` of an earlier release
72
+ (`Out of bounds`). Upgrade the `Client` with the Core.
73
+ - `configGet`, `configWatch` and `configSet` gain the same flags byte with
74
+ their optional `deviceId`, so this Core cannot decode them from a `Client` of
75
+ an earlier release either. (#280, #359)
76
+ - `duck --join <invite>` now takes only a member invite, and the new `duck --device-join <invite>` takes only a device invite; each refuses the other's and names the right flag. `/join` previews say whether the current mesh is retired, and `/invite` says how the invitee joins. `/meshes` tags the account mesh as `home (your account)` and its footer says how to name a mesh. (#377)
77
+
78
+ ### Fixed
79
+
80
+ - Suspending no longer logs every watch it closes as `watch failed` with a
81
+ bare `Stream was destroyed`. The assistant ended those streams itself, so
82
+ none of them failed.
83
+ - A log line that carries an error in its details, such as
84
+ `{ op, err }`, prints the error's stack instead of `{}`. A destroyed stream's
85
+ error records the code that destroyed it, so the log now says why it ended.
86
+ - A device lending its compute no longer crashes when the borrower drops a call
87
+ while the lender is still accepting it. Before, the dropped stream's error
88
+ went unhandled and took the lender's whole process down. (#355)
89
+ - `updateAgent` and `deleteAgent` stamp their write after the row it replaces,
90
+ so an edit or a delete made on a device whose clock runs behind the last
91
+ writer's is applied instead of dropped as stale. A dropped delete had still
92
+ deleted the agent's chats. (#358)
93
+ - `duck` started with a device invite no longer fails with
94
+ `This device cannot reach its account to check`. It asked for its own invite
95
+ while the join was still under way; it now waits for the join to finish.
96
+
97
+ ## [0.7.3] - 2026-10-13
98
+
99
+ ### Changed
100
+
101
+ - `MAX_AUDIO_ATTACHMENT_BYTES` (`@qvac/core/audio-format`) rises from 25 MiB to 100 MiB, matching `MAX_ATTACHMENT_BYTES`, so a client no longer refuses audio the mesh accepts. (#375)
102
+
16
103
  ## [0.7.2] - 2026-10-13
17
104
 
18
105
  ### Added
@@ -1493,7 +1580,9 @@ Published under the `release-0.1` dist-tag.
1493
1580
  await assistant.ready()
1494
1581
  ```
1495
1582
 
1496
- [Unreleased]: https://github.com/tetherto/qvac-core/compare/v0.7.2...HEAD
1583
+ [Unreleased]: https://github.com/tetherto/qvac-core/compare/v0.7.4...HEAD
1584
+ [0.7.4]: https://github.com/tetherto/qvac-core/compare/v0.7.3...v0.7.4
1585
+ [0.7.3]: https://github.com/tetherto/qvac-core/compare/v0.7.2...v0.7.3
1497
1586
  [0.7.2]: https://github.com/tetherto/qvac-core/compare/v0.7.1...v0.7.2
1498
1587
  [0.7.1]: https://github.com/tetherto/qvac-core/compare/v0.7.0...v0.7.1
1499
1588
  [0.7.0]: https://github.com/tetherto/qvac-core/compare/v0.6.7...v0.7.0
package/README.md CHANGED
@@ -725,9 +725,10 @@ The TUI copies its mesh invite to the clipboard on ctrl+y. On a second machine
725
725
  /join qvac://mesh/…
726
726
  ```
727
727
 
728
- That retires the mesh it is on, joins the invited one, and re-resolves the agent
729
- and chat from it. The engine takes an invite live, so joining never needed to be
730
- a boot argument.
728
+ The preview says who you would join as. A device invite (`/invite`) retires the
729
+ mesh this device is on and pairs it into the inviter's account; a member invite
730
+ (`/invite member`) adds a mesh beside yours. Headless, `duck --join <invite>`
731
+ takes a member invite and `duck --device-join <invite>` a device invite.
731
732
 
732
733
  `--headless` brings the same stack up with no TUI: it prints the invite and then
733
734
  streams logs (including the sidecar child's own output, which the TUI drops) to
@@ -0,0 +1,11 @@
1
+ import type { Capabilities, CapabilityHandlers } from '../../spec/capabilities.d.ts';
2
+ export declare const LINK_SOURCES_ONLY = "A model added from another device must come from a link: a path would name a file on this device.";
3
+ export declare const MODEL_SETTINGS_ONLY = "Another device may read and change only the model settings of this one.";
4
+ export declare const DEVICE_ROUTES: readonly ["listModels", "deleteModel", "loadModel", "unloadModel", "retryModelDownload", "clearModelDownload", "modelCatalogWatch", "modelDownloadsWatch", "customModelsWatch", "previewCustomModel", "addCustomModel", "removeCustomModel", "modelUsers", "modelsWatch", "configGet", "configSet", "configWatch", "setModelPaused"];
5
+ export type DeviceRoutes = Pick<CapabilityHandlers, (typeof DEVICE_ROUTES)[number]>;
6
+ export interface ForwardingOptions {
7
+ readonly deviceId: Buffer;
8
+ readonly remote: (deviceId: Buffer) => Capabilities;
9
+ }
10
+ export declare function forwardToDevices(local: Partial<CapabilityHandlers>, opts: ForwardingOptions): DeviceRoutes;
11
+ export declare function servedToSiblings(local: Partial<CapabilityHandlers>): DeviceRoutes;
@@ -0,0 +1,124 @@
1
+ import b4a from 'b4a';
2
+ import { Readable } from 'streamx';
3
+ import { asError } from '../guards.js';
4
+ import { frameReadable } from '../frame-readable.js';
5
+ import { MODEL_IDLE_UNLOAD_KEY, MODEL_MEMORY_CAP_KEY } from '../constants.js';
6
+ export const LINK_SOURCES_ONLY = 'A model added from another device must come from a link: a path would name a file on this device.';
7
+ export const MODEL_SETTINGS_ONLY = 'Another device may read and change only the model settings of this one.';
8
+ const SIBLING_CONFIG_KEYS = new Set([
9
+ MODEL_IDLE_UNLOAD_KEY,
10
+ MODEL_MEMORY_CAP_KEY
11
+ ]);
12
+ export const DEVICE_ROUTES = [
13
+ 'listModels',
14
+ 'deleteModel',
15
+ 'loadModel',
16
+ 'unloadModel',
17
+ 'retryModelDownload',
18
+ 'clearModelDownload',
19
+ 'modelCatalogWatch',
20
+ 'modelDownloadsWatch',
21
+ 'customModelsWatch',
22
+ 'previewCustomModel',
23
+ 'addCustomModel',
24
+ 'removeCustomModel',
25
+ 'modelUsers',
26
+ 'modelsWatch',
27
+ 'configGet',
28
+ 'configSet',
29
+ 'configWatch',
30
+ 'setModelPaused'
31
+ ];
32
+ function modelRoutes(at) {
33
+ return {
34
+ listModels: (input) => answer(at(input).listModels, 'listModels', here(input)),
35
+ deleteModel: (input) => answer(at(input).deleteModel, 'deleteModel', here(input)),
36
+ loadModel: (input) => answer(at(input).loadModel, 'loadModel', here(input)),
37
+ unloadModel: (input) => answer(at(input).unloadModel, 'unloadModel', here(input)),
38
+ retryModelDownload: (input) => answer(at(input).retryModelDownload, 'retryModelDownload', here(input)),
39
+ clearModelDownload: (input) => answer(at(input).clearModelDownload, 'clearModelDownload', here(input)),
40
+ modelCatalogWatch: (input = {}) => watched(() => required(at(input).modelCatalogWatch, 'modelCatalogWatch')(here(input))),
41
+ modelDownloadsWatch: (input = {}) => watched(() => required(at(input).modelDownloadsWatch, 'modelDownloadsWatch')(here(input))),
42
+ customModelsWatch: (input = {}) => watched(() => required(at(input).customModelsWatch, 'customModelsWatch')(here(input))),
43
+ previewCustomModel: (input) => answer(at(input).previewCustomModel, 'previewCustomModel', here(input)),
44
+ addCustomModel: (input) => answer(at(input).addCustomModel, 'addCustomModel', here(input)),
45
+ removeCustomModel: (input) => answer(at(input).removeCustomModel, 'removeCustomModel', here(input)),
46
+ modelUsers: (input) => answer(at(input).modelUsers, 'modelUsers', here(input)),
47
+ modelsWatch: (input = {}) => watched(() => required(at(input).modelsWatch, 'modelsWatch')(here(input))),
48
+ configGet: (input) => answer(at(input).configGet, 'configGet', here(input)),
49
+ configSet: (input) => answer(at(input).configSet, 'configSet', here(input)),
50
+ configWatch: (input) => watched(() => required(at(input).configWatch, 'configWatch')(here(input))),
51
+ setModelPaused: (input) => answer(at(input).setModelPaused, 'setModelPaused', here(input))
52
+ };
53
+ }
54
+ export function forwardToDevices(local, opts) {
55
+ const { deviceId, remote } = opts;
56
+ const elsewhere = (input) => {
57
+ const target = input?.deviceId;
58
+ return target && !b4a.equals(target, deviceId) ? target : null;
59
+ };
60
+ const at = (input) => {
61
+ const target = elsewhere(input);
62
+ return target ? remote(target) : local;
63
+ };
64
+ return modelRoutes(at);
65
+ }
66
+ export function servedToSiblings(local) {
67
+ const routes = modelRoutes(() => local);
68
+ return {
69
+ ...routes,
70
+ previewCustomModel: (input) => {
71
+ refuseLocalSources(input);
72
+ return routes.previewCustomModel(input);
73
+ },
74
+ addCustomModel: (input) => {
75
+ refuseLocalSources(input);
76
+ return routes.addCustomModel(input);
77
+ },
78
+ configGet: (input) => {
79
+ refuseOtherKeys(input);
80
+ return routes.configGet(input);
81
+ },
82
+ configSet: (input) => {
83
+ refuseOtherKeys(input);
84
+ return routes.configSet(input);
85
+ },
86
+ configWatch: (input) => watched(() => {
87
+ refuseOtherKeys(input);
88
+ return routes.configWatch(input);
89
+ })
90
+ };
91
+ }
92
+ function here(input) {
93
+ const local = { ...input };
94
+ delete local.deviceId;
95
+ return local;
96
+ }
97
+ function required(handler, route) {
98
+ if (!handler) {
99
+ throw new Error(`${route} is not served on this device`);
100
+ }
101
+ return handler;
102
+ }
103
+ function watched(open) {
104
+ try {
105
+ return open();
106
+ }
107
+ catch (err) {
108
+ return frameReadable(new Readable({ read: (cb) => cb(asError(err)) }));
109
+ }
110
+ }
111
+ async function answer(handler, route, input) {
112
+ return await required(handler, route)(input);
113
+ }
114
+ function refuseLocalSources(input) {
115
+ const sources = [input.src, input.mmprojSrc].filter((src) => !!src);
116
+ if (!sources.every((src) => /^https?:\/\//i.test(src?.trim() ?? ''))) {
117
+ throw new Error(LINK_SOURCES_ONLY);
118
+ }
119
+ }
120
+ function refuseOtherKeys({ key }) {
121
+ if (!SIBLING_CONFIG_KEYS.has(key)) {
122
+ throw new Error(MODEL_SETTINGS_ONLY);
123
+ }
124
+ }
@@ -971,9 +971,10 @@ export class QvacAssistant extends ReadyResource {
971
971
  _supervisedWatch(label, run) {
972
972
  let running = false;
973
973
  let again = false;
974
+ let stopped = false;
974
975
  const settle = (err) => {
975
976
  running = false;
976
- if (err !== null && !isMissingMesh(err)) {
977
+ if (err !== null && !stopped && !isMissingMesh(err)) {
977
978
  this._log?.error('assistant', `${label} watch failed`, err);
978
979
  }
979
980
  if (again) {
@@ -990,9 +991,14 @@ export class QvacAssistant extends ReadyResource {
990
991
  return;
991
992
  }
992
993
  running = true;
994
+ stopped = false;
993
995
  run().then(() => settle(null), (err) => settle(err));
994
996
  };
995
- return { start, stop: () => void (again = false) };
997
+ const stop = () => {
998
+ again = false;
999
+ stopped = true;
1000
+ };
1001
+ return { start, stop };
996
1002
  }
997
1003
  async _reclaimDeletedChats(frames) {
998
1004
  let known = null;
@@ -1,4 +1,4 @@
1
1
  export { isAudioAttachment as isAudioFile } from './core/audio-attachment.js';
2
2
  export { AUDIO_EXTENSIONS } from '../schema/media-kind.js';
3
3
  export { wavDurationSeconds } from './wav-duration.js';
4
- export const MAX_AUDIO_ATTACHMENT_BYTES = 25 * 1024 * 1024;
4
+ export const MAX_AUDIO_ATTACHMENT_BYTES = 100 * 1024 * 1024;
@@ -184,7 +184,7 @@ export async function updateAgent(store, agentId, input) {
184
184
  lenderDeviceId: input.clearLenderDeviceId
185
185
  ? undefined
186
186
  : patch(input.lenderDeviceId, current.lenderDeviceId),
187
- updatedAt: Date.now()
187
+ updatedAt: Math.max(Date.now(), current.updatedAt + 1)
188
188
  };
189
189
  await store.dispatch('put-agent', { agent: updated });
190
190
  return updated;
@@ -206,7 +206,11 @@ export async function deleteAgent(store, deviceId, id) {
206
206
  throw new Error('cannot delete the default agent');
207
207
  }
208
208
  const deletedAt = Date.now();
209
- const updated = { ...current, deletedAt, updatedAt: deletedAt };
209
+ const updated = {
210
+ ...current,
211
+ deletedAt,
212
+ updatedAt: Math.max(deletedAt, current.updatedAt + 1)
213
+ };
210
214
  await store.dispatch('put-agent', { agent: updated });
211
215
  await softDeleteChatsForAgent(store, deviceId, id);
212
216
  await softDeleteToolConfigsForAgent(store, id);
@@ -0,0 +1,23 @@
1
+ import type { Capabilities, CapabilityHandlers } from '../../spec/capabilities.d.ts';
2
+ import { type PeerStream } from './peer-duplex.ts';
3
+ export declare const DEVICE_UNREACHABLE = "the device is not reachable";
4
+ export declare const DEVICE_REVOKED = "the device no longer takes requests from this one";
5
+ export interface DeviceControlOptions {
6
+ readonly allowed: (deviceId: Uint8Array) => Promise<boolean>;
7
+ readonly served: () => Partial<CapabilityHandlers> | null;
8
+ }
9
+ export declare class DeviceControl {
10
+ private readonly _opts;
11
+ private readonly _channel;
12
+ private readonly _engines;
13
+ private readonly _ours;
14
+ private readonly _checks;
15
+ constructor(opts: DeviceControlOptions);
16
+ addStream(stream: PeerStream): void;
17
+ engine(deviceId: Uint8Array): Capabilities;
18
+ revalidate(): Promise<void>;
19
+ destroy(): void;
20
+ private _accept;
21
+ private _changed;
22
+ private _recheck;
23
+ }
@@ -0,0 +1,92 @@
1
+ import b4a from 'b4a';
2
+ import { settleOnClose } from '@qvac/harness/rpc';
3
+ import { duplexPair } from '@qvac/harness/transport';
4
+ import { bindApi } from '../../spec/bind.js';
5
+ import { engineClient } from '../../spec/calls.js';
6
+ import HRPC from '../../spec/hrpc/index.js';
7
+ import { PeerDuplexChannel } from './peer-duplex.js';
8
+ const PROTOCOL = 'qvac/device-control';
9
+ export const DEVICE_UNREACHABLE = 'the device is not reachable';
10
+ export const DEVICE_REVOKED = 'the device no longer takes requests from this one';
11
+ export class DeviceControl {
12
+ _opts;
13
+ _channel;
14
+ _engines;
15
+ _ours;
16
+ _checks;
17
+ constructor(opts) {
18
+ this._opts = opts;
19
+ this._channel = new PeerDuplexChannel({
20
+ protocol: PROTOCOL,
21
+ onaccept: (from) => this._accept(from),
22
+ unreachable: DEVICE_UNREACHABLE,
23
+ revoked: DEVICE_REVOKED,
24
+ onchange: (deviceId) => this._changed(deviceId)
25
+ });
26
+ this._engines = new Map();
27
+ this._ours = new Set();
28
+ this._checks = new Map();
29
+ }
30
+ addStream(stream) {
31
+ this._channel.addStream(stream);
32
+ }
33
+ engine(deviceId) {
34
+ const id = b4a.toHex(deviceId);
35
+ if (!this._ours.has(id)) {
36
+ throw new Error(DEVICE_UNREACHABLE);
37
+ }
38
+ const held = this._engines.get(id);
39
+ if (held) {
40
+ return held;
41
+ }
42
+ const stream = this._channel.dial(id);
43
+ const engine = engineClient(settleOnClose(new HRPC(stream)));
44
+ stream.once('close', () => {
45
+ if (this._engines.get(id) === engine) {
46
+ this._engines.delete(id);
47
+ }
48
+ });
49
+ this._engines.set(id, engine);
50
+ return engine;
51
+ }
52
+ async revalidate() {
53
+ await this._channel.revalidate(this._opts.allowed);
54
+ await Promise.all(this._channel.openIds().map((id) => this._recheck(id)));
55
+ }
56
+ destroy() {
57
+ this._channel.destroy();
58
+ this._engines.clear();
59
+ }
60
+ async _accept(from) {
61
+ const handlers = this._opts.served();
62
+ if (!handlers || !(await this._opts.allowed(from))) {
63
+ return null;
64
+ }
65
+ const [ours, theirs] = duplexPair();
66
+ bindApi(settleOnClose(new HRPC(ours)), handlers);
67
+ return theirs;
68
+ }
69
+ _changed(deviceId) {
70
+ if (this._channel.isOpen(deviceId)) {
71
+ void this._recheck(deviceId);
72
+ return;
73
+ }
74
+ this._ours.delete(deviceId);
75
+ this._engines.delete(deviceId);
76
+ }
77
+ async _recheck(deviceId) {
78
+ const check = (this._checks.get(deviceId) ?? 0) + 1;
79
+ this._checks.set(deviceId, check);
80
+ const allowed = await this._opts.allowed(b4a.from(deviceId, 'hex')).catch(() => false);
81
+ if (this._checks.get(deviceId) !== check) {
82
+ return;
83
+ }
84
+ if (allowed && this._channel.isOpen(deviceId)) {
85
+ this._ours.add(deviceId);
86
+ return;
87
+ }
88
+ this._ours.delete(deviceId);
89
+ this._engines.delete(deviceId);
90
+ this._channel.closeDialled(deviceId, DEVICE_REVOKED);
91
+ }
92
+ }
@@ -40,6 +40,8 @@ export interface MeshOptions {
40
40
  wakeup?: Wakeup;
41
41
  attachmentUrl?: AttachmentUrl | null;
42
42
  fileIndex?: MeshFileIndex | null;
43
+ slowWriteMs?: number;
44
+ slowDrainMs?: number;
43
45
  }
44
46
  export declare const MESH_JOINING = "This mesh is still joining: it accepts writes once it is joined";
45
47
  export declare function meshPublicKeyFromSeed(seed: Buffer): Buffer;
@@ -109,6 +111,9 @@ export declare class Mesh extends ReadyResource {
109
111
  private _name;
110
112
  private _membership;
111
113
  private readonly _deviceId;
114
+ private readonly _slowWriteMs;
115
+ private readonly _slowDrainMs;
116
+ private _stallReported;
112
117
  private _admitted;
113
118
  private _birth;
114
119
  private _autobee;
@@ -153,6 +158,11 @@ export declare class Mesh extends ReadyResource {
153
158
  get localLength(): number;
154
159
  get joining(): boolean;
155
160
  dispatch(name: string, payload: unknown, opts?: AppendOptions): Promise<void>;
161
+ private _watchStall;
162
+ private _endStall;
163
+ private _reportStall;
164
+ private _coresNotHeld;
165
+ private _coreHeld;
156
166
  batch(): MeshBatch;
157
167
  appendBirthOp(identity: Buffer | KeyPair, device?: DeviceMetadata, opts?: BirthOptions | (() => Promise<BirthOptions>)): Promise<void>;
158
168
  private _settleBirth;
@@ -182,15 +192,19 @@ export declare class Mesh extends ReadyResource {
182
192
  export interface MeshDispatcher {
183
193
  dispatch(name: string, payload: unknown): void | Promise<void>;
184
194
  }
195
+ type WriteWatch = (op: string, write: Promise<void>) => Promise<void>;
185
196
  export interface MeshBatchOptions {
186
197
  readonly compute?: boolean;
187
198
  readonly joined?: boolean;
199
+ readonly watch?: WriteWatch;
188
200
  }
189
201
  export declare class MeshBatch {
190
202
  private readonly _autobee;
191
203
  private readonly _compute;
192
204
  private readonly _joined;
205
+ private readonly _watch;
193
206
  private _ops;
207
+ private _names;
194
208
  constructor(autobee: Autobee<MeshView>, opts?: MeshBatchOptions);
195
209
  get size(): number;
196
210
  dispatch(name: string, payload: unknown): void;
@@ -24,6 +24,8 @@ import { errorMessage, isRecord } from '../guards.js';
24
24
  import { ACTIVE_AGENTS, ACTIVE_CHATS, DEVICES, LIVE_WRITERS_BY_MOST_RECENT, MEMBERS, MESH_NAMES } from './collections.js';
25
25
  const [MESH_KEYPAIR, MESH_ENCRYPTION, MESH_DEVICE] = crypto.namespace('qvac-assistant/mesh', 3);
26
26
  export const MESH_JOINING = 'This mesh is still joining: it accepts writes once it is joined';
27
+ const SLOW_WRITE_MS = 5000;
28
+ const SLOW_DRAIN_MS = 15_000;
27
29
  export function meshPublicKeyFromSeed(seed) {
28
30
  return crypto.keyPair(crypto.hash([MESH_KEYPAIR, seed])).publicKey;
29
31
  }
@@ -88,6 +90,9 @@ export class Mesh extends ReadyResource {
88
90
  revoked: 0
89
91
  };
90
92
  _deviceId;
93
+ _slowWriteMs;
94
+ _slowDrainMs;
95
+ _stallReported = false;
91
96
  _admitted = false;
92
97
  _birth = null;
93
98
  _autobee = null;
@@ -221,6 +226,8 @@ export class Mesh extends ReadyResource {
221
226
  this._key = opts.key ?? null;
222
227
  this._writerSeed = opts.writerSeed;
223
228
  this._log = opts.logger ?? null;
229
+ this._slowWriteMs = opts.slowWriteMs ?? SLOW_WRITE_MS;
230
+ this._slowDrainMs = opts.slowDrainMs ?? SLOW_DRAIN_MS;
224
231
  this._blindPeerKeys = opts.blindPeerKeys ?? [];
225
232
  this._blindPeeringOpts = opts.blindPeeringOpts;
226
233
  this._bootFrom = opts.bootFrom;
@@ -342,7 +349,7 @@ export class Mesh extends ReadyResource {
342
349
  async _sweepOrphanedBlobs(blobLength) {
343
350
  this._orphanedBlobSweepStatus = { state: 'running' };
344
351
  try {
345
- await this.autobee.update();
352
+ await this._watchStall(this.autobee.update(), { what: 'mesh drain', ms: this._slowDrainMs });
346
353
  await this.files.sweepOrphans(blobLength);
347
354
  this._orphanedBlobSweepStatus = { state: 'succeeded' };
348
355
  this._log?.info(this._scope, 'orphaned blob sweep finished');
@@ -385,10 +392,93 @@ export class Mesh extends ReadyResource {
385
392
  async dispatch(name, payload, opts) {
386
393
  assertComputeRoute(this._compute, name);
387
394
  assertJoined(this.autobee, opts);
388
- await this.autobee.append(encode(`@qvac/${name}`, payload), opts);
395
+ const write = this.autobee.append(encode(`@qvac/${name}`, payload), opts);
396
+ await this._watchStall(write, { what: 'mesh write', op: name, ms: this._slowWriteMs });
397
+ }
398
+ async _watchStall(pending, watch) {
399
+ const started = Date.now();
400
+ let reported = false;
401
+ const timer = setTimeout(() => {
402
+ if (this._stallReported) {
403
+ return;
404
+ }
405
+ this._stallReported = true;
406
+ reported = true;
407
+ this._reportStall(watch, started).catch(noop);
408
+ }, watch.ms);
409
+ try {
410
+ await pending;
411
+ this._endStall(watch, { reported, started, failed: false });
412
+ }
413
+ catch (err) {
414
+ this._endStall(watch, { reported, started, failed: true, err });
415
+ throw err;
416
+ }
417
+ finally {
418
+ clearTimeout(timer);
419
+ }
420
+ }
421
+ _endStall({ what, op }, { reported, started, failed, err }) {
422
+ if (!reported) {
423
+ return;
424
+ }
425
+ this._stallReported = false;
426
+ const ms = Date.now() - started;
427
+ if (failed) {
428
+ this._log?.warn(this._scope, `${what} failed`, { op, ms, err });
429
+ }
430
+ else {
431
+ this._log?.info(this._scope, `${what} settled`, { op, ms });
432
+ }
433
+ }
434
+ async _reportStall({ what, op }, started) {
435
+ const { stats, system } = this.autobee;
436
+ const missing = await this._coresNotHeld([system.bee, this.autobee.bee]).catch(noop);
437
+ this._log?.warn(this._scope, `${what} still pending`, {
438
+ op,
439
+ afterMs: Date.now() - started,
440
+ writable: this.autobee.writable,
441
+ interrupted: !!this.autobee.interrupted,
442
+ drains: stats.drains,
443
+ applies: stats.applies,
444
+ appends: stats.appends,
445
+ fastForwards: stats.fastForwards,
446
+ localLength: this.autobee.local.length,
447
+ missing,
448
+ blindPeers: this.blindPeerStats()
449
+ });
450
+ }
451
+ async _coresNotHeld(views) {
452
+ const keys = new Map();
453
+ for (const { context } of views) {
454
+ for (const { key } of [context.core, ...context.cores]) {
455
+ keys.set(b4a.toHex(key), key);
456
+ }
457
+ }
458
+ const held = await Promise.all([...keys.values()].map((key) => this._coreHeld(key)));
459
+ return held.filter((core) => core.held < core.length);
460
+ }
461
+ async _coreHeld(key) {
462
+ const core = this._store.get({ key, active: false });
463
+ try {
464
+ await core.ready();
465
+ return {
466
+ key: b4a.toHex(key).slice(0, 8),
467
+ length: core.length,
468
+ held: core.contiguousLength,
469
+ peers: core.peers.length
470
+ };
471
+ }
472
+ finally {
473
+ await core.close();
474
+ }
389
475
  }
390
476
  batch() {
391
- return new MeshBatch(this.autobee, { compute: this._compute, joined: true });
477
+ return new MeshBatch(this.autobee, {
478
+ compute: this._compute,
479
+ joined: true,
480
+ watch: (op, write) => this._watchStall(write, { what: 'mesh write', op, ms: this._slowWriteMs })
481
+ });
392
482
  }
393
483
  appendBirthOp(identity, device, opts = {}) {
394
484
  this._birth ??= (async () => {
@@ -663,11 +753,14 @@ export class MeshBatch {
663
753
  _autobee;
664
754
  _compute;
665
755
  _joined;
756
+ _watch;
666
757
  _ops = [];
758
+ _names = [];
667
759
  constructor(autobee, opts = {}) {
668
760
  this._autobee = autobee;
669
761
  this._compute = opts.compute ?? false;
670
762
  this._joined = opts.joined ?? false;
763
+ this._watch = opts.watch ?? ((_op, write) => write);
671
764
  }
672
765
  get size() {
673
766
  return this._ops.length;
@@ -675,6 +768,7 @@ export class MeshBatch {
675
768
  dispatch(name, payload) {
676
769
  assertComputeRoute(this._compute, name);
677
770
  this._ops.push(encode(`@qvac/${name}`, payload));
771
+ this._names.push(name);
678
772
  }
679
773
  async flush(opts) {
680
774
  if (this._ops.length === 0)
@@ -683,8 +777,10 @@ export class MeshBatch {
683
777
  assertJoined(this._autobee, opts);
684
778
  }
685
779
  const ops = this._ops;
780
+ const names = this._names;
686
781
  this._ops = [];
687
- await this._autobee.append(ops, opts);
782
+ this._names = [];
783
+ await this._watch(names.join(','), this._autobee.append(ops, opts));
688
784
  }
689
785
  }
690
786
  function noop() { }
@@ -19,7 +19,9 @@ export declare class PeerDuplexChannel {
19
19
  private readonly _pairing;
20
20
  constructor(opts: PeerDuplexOptions);
21
21
  addStream(stream: PeerStream): void;
22
+ openIds(): string[];
22
23
  isOpen(deviceId: string): boolean;
24
+ closeDialled(deviceId: string, reason: string): void;
23
25
  dial(deviceId: string): Duplex;
24
26
  revalidate(allowed: (deviceId: Uint8Array) => Promise<boolean>): Promise<void>;
25
27
  destroy(): void;