@qvac/core 0.7.3 → 0.7.5

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,100 @@ release.
13
13
 
14
14
  ## [Unreleased]
15
15
 
16
+ ## [0.7.5] - 2026-10-13
17
+
18
+ ### Fixed
19
+
20
+ - A reply cut off by a phone lock now restarts on unlock instead of ending as
21
+ failed (QVAC-24690). Between `presuspend` and `suspend` iOS refuses GPU work,
22
+ so the harness ends the turn with an error event or an abort of its own.
23
+ While the assistant is in the background that ending now replays the run
24
+ under the same `runId`, for chat and capability runs alike, and a run that
25
+ arrives in that window waits. A `resume` from `presuspend` starts both again.
26
+ A thrown error still fails the run, and a context overflow reported during
27
+ that window folds after the resume instead. (#368)
28
+
29
+ ## [0.7.4] - 2026-10-13
30
+
31
+ ### Added
32
+
33
+ - A mesh write still pending after five seconds, or a mesh's first drain
34
+ still pending after fifteen, logs a warning once: the ops a write carries,
35
+ the drain's counters, and every core the views read through that this device
36
+ holds only in part, with how many peers could serve it, and which blind peers
37
+ it has reached. Writes queued behind
38
+ the same stall do not repeat it, and it logs again when the stall settles or
39
+ the write fails. A drain parked on data no connected peer holds used to say
40
+ nothing, so a hung send, stop, delete or leave left no trace in an exported
41
+ log.
42
+ - Leaving a mesh logs when it starts, so an exported log shows the attempt even
43
+ when the leave never finishes. (#352)
44
+
45
+ - Model routes reach another device of this account (QVAC-25101). `listModels`,
46
+ `modelsWatch`, `loadModel`, `unloadModel`, `deleteModel`, `retryModelDownload`,
47
+ `clearModelDownload`, `modelUsers`, the custom-model routes and the model
48
+ watches take an optional `deviceId`, and Core runs the call on that device over
49
+ the `qvac/device-control` peer channel, so fit, memory, disk and the download
50
+ queue are that device's. Only a live device of this account's own member is
51
+ served, and a custom model added from another device must come from a link.
52
+ While the device is away a call rejects with `the device is not reachable`;
53
+ `meshStatusWatch`'s `peerDeviceIds` says when it is connected again. (#280)
54
+
55
+ ```js
56
+ await engine.loadModel({ name: 'Qwen3.5-9B-Q4_K_M', deviceId: desktop.id })
57
+ ```
58
+
59
+ - A phone changes a paired desktop's own model settings and pauses its
60
+ downloads. `configGet`, `configSet` and `configWatch` take an optional
61
+ `deviceId` and reach `model.idleUnloadMinutes` and `model.memoryCapPercent` on
62
+ that device; any other key is refused there, since the same store holds
63
+ integration tokens. `setModelPaused` takes `deviceId` too, so a download on the
64
+ desktop pauses there rather than on the phone, until the desktop restarts. (#359)
65
+
66
+ ```js
67
+ import { MODEL_MEMORY_CAP_KEY } from '@qvac/core/constants'
68
+
69
+ await engine.configSet({ key: MODEL_MEMORY_CAP_KEY, value: 60, deviceId: desktop.id })
70
+ await engine.setModelPaused({ name: 'Qwen3.5-9B-Q4_K_M', paused: true, deviceId: desktop.id })
71
+ ```
72
+
73
+ - `duck`: `/device use <n|name>` points every `/models` command at another
74
+ device of this account, and `/device list` shows each one, whether it is
75
+ connected and how many models it has downloaded. `/models idle`,
76
+ `/models cap` and `/models settings` set and show a device's idle limit and
77
+ memory cap, and `/models pause` / `/models resume` hold a download. (#360)
78
+
79
+ ### Changed
80
+
81
+ - `loadModel`, `unloadModel`, `deleteModel`, `modelUsers`,
82
+ `clearModelDownload`, `retryModelDownload` and `removeCustomModel` had a
83
+ request of `name` alone; its new optional `deviceId` adds a flags byte, so
84
+ this Core cannot decode those calls from a `Client` of an earlier release
85
+ (`Out of bounds`). Upgrade the `Client` with the Core.
86
+ - `configGet`, `configWatch` and `configSet` gain the same flags byte with
87
+ their optional `deviceId`, so this Core cannot decode them from a `Client` of
88
+ an earlier release either. (#280, #359)
89
+ - `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)
90
+
91
+ ### Fixed
92
+
93
+ - Suspending no longer logs every watch it closes as `watch failed` with a
94
+ bare `Stream was destroyed`. The assistant ended those streams itself, so
95
+ none of them failed.
96
+ - A log line that carries an error in its details, such as
97
+ `{ op, err }`, prints the error's stack instead of `{}`. A destroyed stream's
98
+ error records the code that destroyed it, so the log now says why it ended.
99
+ - A device lending its compute no longer crashes when the borrower drops a call
100
+ while the lender is still accepting it. Before, the dropped stream's error
101
+ went unhandled and took the lender's whole process down. (#355)
102
+ - `updateAgent` and `deleteAgent` stamp their write after the row it replaces,
103
+ so an edit or a delete made on a device whose clock runs behind the last
104
+ writer's is applied instead of dropped as stale. A dropped delete had still
105
+ deleted the agent's chats. (#358)
106
+ - `duck` started with a device invite no longer fails with
107
+ `This device cannot reach its account to check`. It asked for its own invite
108
+ while the join was still under way; it now waits for the join to finish.
109
+
16
110
  ## [0.7.3] - 2026-10-13
17
111
 
18
112
  ### Changed
@@ -1499,7 +1593,9 @@ Published under the `release-0.1` dist-tag.
1499
1593
  await assistant.ready()
1500
1594
  ```
1501
1595
 
1502
- [Unreleased]: https://github.com/tetherto/qvac-core/compare/v0.7.3...HEAD
1596
+ [Unreleased]: https://github.com/tetherto/qvac-core/compare/v0.7.5...HEAD
1597
+ [0.7.5]: https://github.com/tetherto/qvac-core/compare/v0.7.4...v0.7.5
1598
+ [0.7.4]: https://github.com/tetherto/qvac-core/compare/v0.7.3...v0.7.4
1503
1599
  [0.7.3]: https://github.com/tetherto/qvac-core/compare/v0.7.2...v0.7.3
1504
1600
  [0.7.2]: https://github.com/tetherto/qvac-core/compare/v0.7.1...v0.7.2
1505
1601
  [0.7.1]: https://github.com/tetherto/qvac-core/compare/v0.7.0...v0.7.1
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
+ }
@@ -85,6 +85,8 @@ export declare class QvacAssistant extends ReadyResource {
85
85
  private readonly _meshConsumers;
86
86
  private readonly _runs;
87
87
  private readonly _seen;
88
+ private readonly _parked;
89
+ private readonly _backgroundReplays;
88
90
  private readonly _inferenceCalls;
89
91
  private readonly _runReplay;
90
92
  private readonly _lifecycle;
@@ -127,6 +129,7 @@ export declare class QvacAssistant extends ReadyResource {
127
129
  _close(): Promise<void>;
128
130
  get mobileClass(): boolean;
129
131
  get suspended(): boolean;
132
+ private get _backgrounded();
130
133
  presuspend(): Promise<void>;
131
134
  suspend(): Promise<void>;
132
135
  wakeup(): Promise<void>;
@@ -179,6 +182,7 @@ export declare class QvacAssistant extends ReadyResource {
179
182
  private _holderOf;
180
183
  private _holderHarness;
181
184
  private _dispatch;
185
+ private _requeue;
182
186
  private _citeWebSources;
183
187
  private _searchKnowledge;
184
188
  private _injectKnowledge;
@@ -182,6 +182,8 @@ export class QvacAssistant extends ReadyResource {
182
182
  _meshConsumers;
183
183
  _runs;
184
184
  _seen;
185
+ _parked;
186
+ _backgroundReplays;
185
187
  _inferenceCalls;
186
188
  _runReplay;
187
189
  _lifecycle;
@@ -229,6 +231,8 @@ export class QvacAssistant extends ReadyResource {
229
231
  this._meshConsumers = new Map();
230
232
  this._runs = new Map();
231
233
  this._seen = new Map();
234
+ this._parked = new Map();
235
+ this._backgroundReplays = new Set();
232
236
  this._inferenceCalls = new InferenceCallStore(inferenceCallsDir ?? persistentDir('inspector'));
233
237
  this._runReplay = new RunReplayStore(runReplayDir ?? persistentDir('run-replay'));
234
238
  this._lifecycle = new AbortController();
@@ -713,6 +717,9 @@ export class QvacAssistant extends ReadyResource {
713
717
  get suspended() {
714
718
  return this._state === QvacState.SUSPEND;
715
719
  }
720
+ get _backgrounded() {
721
+ return this._state === QvacState.PRESUSPEND || this._state === QvacState.SUSPEND;
722
+ }
716
723
  async presuspend() {
717
724
  if (this.closing)
718
725
  return;
@@ -729,6 +736,7 @@ export class QvacAssistant extends ReadyResource {
729
736
  }
730
737
  this._state = QvacState.SUSPEND;
731
738
  this._log?.info('assistant', 'suspending', { runs: this._runs.size });
739
+ this._parked.clear();
732
740
  this._stopMeshIntake();
733
741
  await this._downloads?.suspend();
734
742
  this._meshList?.destroy();
@@ -758,6 +766,8 @@ export class QvacAssistant extends ReadyResource {
758
766
  }
759
767
  if (this._state === QvacState.PRESUSPEND) {
760
768
  this._state = QvacState.RESUME;
769
+ this._parked.forEach((replay) => replay());
770
+ this._parked.clear();
761
771
  return;
762
772
  }
763
773
  this._state = QvacState.RESUME;
@@ -971,9 +981,10 @@ export class QvacAssistant extends ReadyResource {
971
981
  _supervisedWatch(label, run) {
972
982
  let running = false;
973
983
  let again = false;
984
+ let stopped = false;
974
985
  const settle = (err) => {
975
986
  running = false;
976
- if (err !== null && !isMissingMesh(err)) {
987
+ if (err !== null && !stopped && !isMissingMesh(err)) {
977
988
  this._log?.error('assistant', `${label} watch failed`, err);
978
989
  }
979
990
  if (again) {
@@ -990,9 +1001,14 @@ export class QvacAssistant extends ReadyResource {
990
1001
  return;
991
1002
  }
992
1003
  running = true;
1004
+ stopped = false;
993
1005
  run().then(() => settle(null), (err) => settle(err));
994
1006
  };
995
- return { start, stop: () => void (again = false) };
1007
+ const stop = () => {
1008
+ again = false;
1009
+ stopped = true;
1010
+ };
1011
+ return { start, stop };
996
1012
  }
997
1013
  async _reclaimDeletedChats(frames) {
998
1014
  let known = null;
@@ -1381,14 +1397,23 @@ export class QvacAssistant extends ReadyResource {
1381
1397
  this._chats.delete(request.chatId);
1382
1398
  }
1383
1399
  void this._reportCapabilities();
1384
- const requeued = controller.signal.reason === MODEL_UNLOADED && !this._seen.has(runId);
1385
- if (requeued && !this.closing) {
1386
- this._dispatch(request, meshId);
1387
- }
1400
+ this._requeue(runId, controller.signal.reason, () => this._dispatch(request, meshId));
1388
1401
  }
1389
1402
  })();
1390
1403
  settle.catch((err) => this._log?.error('assistant', `run ${runId} failed`, err));
1391
1404
  }
1405
+ _requeue(runId, reason, replay) {
1406
+ const background = this._backgroundReplays.delete(runId);
1407
+ if (this._seen.has(runId) || this.closing) {
1408
+ return;
1409
+ }
1410
+ if (background && this._state === QvacState.PRESUSPEND) {
1411
+ this._parked.set(runId, replay);
1412
+ }
1413
+ else if ((background && !this._backgrounded) || reason === MODEL_UNLOADED) {
1414
+ replay();
1415
+ }
1416
+ }
1392
1417
  async _citeWebSources(transcript, runId, result) {
1393
1418
  const records = webSourcesFromToolResult(result, WEB_RAG_SOURCE_BOUNDS);
1394
1419
  if (records.length === 0) {
@@ -1492,8 +1517,9 @@ export class QvacAssistant extends ReadyResource {
1492
1517
  const engine = meshScoped(this._engine, meshId);
1493
1518
  const { chatId } = request;
1494
1519
  const deviceId = this._requireDeviceId();
1495
- if (this.suspended) {
1520
+ if (this._backgrounded) {
1496
1521
  this._seen.delete(runId);
1522
+ this._backgroundReplays.add(runId);
1497
1523
  return;
1498
1524
  }
1499
1525
  if (await this._isHandled(engine, request)) {
@@ -1951,11 +1977,14 @@ export class QvacAssistant extends ReadyResource {
1951
1977
  continue;
1952
1978
  }
1953
1979
  await flushTranscript();
1954
- if (event.type === 'error') {
1980
+ if (event.type === 'error' && !this._backgrounded) {
1955
1981
  await failRun(event.message);
1956
1982
  return;
1957
1983
  }
1958
- if (event.type === 'aborted') {
1984
+ if (event.type === 'error') {
1985
+ this._log?.info('runs', 'background error replayed', { runId, message: event.message });
1986
+ }
1987
+ if (event.type === 'error' || event.type === 'aborted') {
1959
1988
  await endAbortedRun();
1960
1989
  return;
1961
1990
  }
@@ -2118,7 +2147,7 @@ export class QvacAssistant extends ReadyResource {
2118
2147
  this._log?.info('runs', 'capability metrics', { runId, capability, ...rest });
2119
2148
  continue;
2120
2149
  }
2121
- if (event.type === 'error') {
2150
+ if (event.type === 'error' && !this._backgrounded) {
2122
2151
  await transcript.status({
2123
2152
  state: 'failed',
2124
2153
  reason: runFailureNotice(event.message),
@@ -2126,7 +2155,10 @@ export class QvacAssistant extends ReadyResource {
2126
2155
  });
2127
2156
  return;
2128
2157
  }
2129
- if (event.type === 'aborted') {
2158
+ if (event.type === 'error') {
2159
+ this._log?.info('runs', 'background error replayed', { runId, message: event.message });
2160
+ }
2161
+ if (event.type === 'error' || event.type === 'aborted') {
2130
2162
  await endAbortedCapability();
2131
2163
  return;
2132
2164
  }
@@ -2996,7 +3028,8 @@ export class QvacAssistant extends ReadyResource {
2996
3028
  if (known) {
2997
3029
  return known;
2998
3030
  }
2999
- if (this.suspended) {
3031
+ if (this._backgrounded) {
3032
+ this._backgroundReplays.add(runId);
3000
3033
  return { kind: 'replay' };
3001
3034
  }
3002
3035
  if (reason !== undefined) {
@@ -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;