@johnhenry/browsermesh-apps 0.11.0 → 0.13.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/README.md +645 -12
- package/package.json +3 -1
- package/src/index.mjs +15 -5
- package/src/mesh-bootstrap.mjs +12 -6
- package/src/mesh-orchestrator-tools.mjs +77 -10
- package/src/mesh-orchestrator.mjs +1 -1
- package/src/mesh-torrent.mjs +306 -53
- package/src/orchestrator.mjs +895 -6
- package/src/payments.mjs +20 -9
- package/src/peer-escrow.mjs +49 -11
- package/src/peer-payments.mjs +16 -9
- package/src/peer-timestamp.mjs +11 -3
- package/src/peer-torrent.mjs +58 -28
- package/src/pod-host-gateway.mjs +372 -0
- package/src/pod-host-routes.mjs +630 -0
- package/src/pod-host-service.mjs +732 -0
- package/src/pod-supervisor.mjs +784 -0
- package/src/resources.mjs +107 -2
- package/src/tools.mjs +73 -5
package/src/resources.mjs
CHANGED
|
@@ -30,6 +30,36 @@ export const COMPUTE_RESULT = MESH_TYPE.COMPUTE_RESULT;
|
|
|
30
30
|
/** Incremental progress update for a running job */
|
|
31
31
|
export const COMPUTE_PROGRESS = MESH_TYPE.COMPUTE_PROGRESS;
|
|
32
32
|
|
|
33
|
+
// ---------------------------------------------------------------------------
|
|
34
|
+
// Runtime classes and isolation lanes (issue #185, WP4)
|
|
35
|
+
// ---------------------------------------------------------------------------
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Canonical runtime class values a `ResourceDescriptor` advertises as
|
|
39
|
+
* `runtime:<class>` capabilities, and that `ComputeRequest.constraints`
|
|
40
|
+
* reasons about. See the "Runtime classes and placement lanes" section of
|
|
41
|
+
* this package's README and https://github.com/johnhenry/browsermesh/issues/185.
|
|
42
|
+
*/
|
|
43
|
+
export const RUNTIME_CLASS = Object.freeze({
|
|
44
|
+
BROWSER: 'browser',
|
|
45
|
+
NODE: 'node',
|
|
46
|
+
ISOLATE: 'isolate',
|
|
47
|
+
MICROVM: 'microvm',
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Isolation lane a `ComputeRequest` may require: `'any'` lets the scorer
|
|
52
|
+
* prefer a lane, `'isolate'`/`'microvm'` hard-require it.
|
|
53
|
+
*/
|
|
54
|
+
export const ISOLATION = Object.freeze({
|
|
55
|
+
ANY: 'any',
|
|
56
|
+
ISOLATE: 'isolate',
|
|
57
|
+
MICROVM: 'microvm',
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
const VALID_MODULE_TYPES = new Set(['wasm', 'js', 'shell']);
|
|
61
|
+
const VALID_ISOLATIONS = new Set(Object.values(ISOLATION));
|
|
62
|
+
|
|
33
63
|
// ---------------------------------------------------------------------------
|
|
34
64
|
// ResourceDescriptor
|
|
35
65
|
// ---------------------------------------------------------------------------
|
|
@@ -51,6 +81,9 @@ export class ResourceDescriptor {
|
|
|
51
81
|
* @param {'online'|'busy'|'offline'} [opts.availability]
|
|
52
82
|
* @param {number} [opts.updatedAt]
|
|
53
83
|
* @param {number} [opts.ttl] - Time-to-live in ms (default 60 000)
|
|
84
|
+
* @param {string|null} [opts.hostedBy] - podId of the host pod, for pods
|
|
85
|
+
* placed on a lane host (issue #185 §7: "hosted pods are child-role
|
|
86
|
+
* pods of the host pod"). `null`/omitted for a self-hosted pod.
|
|
54
87
|
*/
|
|
55
88
|
constructor({
|
|
56
89
|
podId,
|
|
@@ -59,6 +92,7 @@ export class ResourceDescriptor {
|
|
|
59
92
|
availability = 'online',
|
|
60
93
|
updatedAt,
|
|
61
94
|
ttl,
|
|
95
|
+
hostedBy,
|
|
62
96
|
}) {
|
|
63
97
|
if (!podId || typeof podId !== 'string') {
|
|
64
98
|
throw new Error('podId is required and must be a non-empty string');
|
|
@@ -75,6 +109,7 @@ export class ResourceDescriptor {
|
|
|
75
109
|
this.availability = availability;
|
|
76
110
|
this.updatedAt = updatedAt ?? Date.now();
|
|
77
111
|
this.ttl = ttl ?? 60_000;
|
|
112
|
+
this.hostedBy = hostedBy || null;
|
|
78
113
|
}
|
|
79
114
|
|
|
80
115
|
/**
|
|
@@ -120,7 +155,7 @@ export class ResourceDescriptor {
|
|
|
120
155
|
}
|
|
121
156
|
|
|
122
157
|
toJSON() {
|
|
123
|
-
|
|
158
|
+
const json = {
|
|
124
159
|
podId: this.podId,
|
|
125
160
|
resources: { ...this.resources },
|
|
126
161
|
capabilities: [...this.capabilities],
|
|
@@ -128,6 +163,8 @@ export class ResourceDescriptor {
|
|
|
128
163
|
updatedAt: this.updatedAt,
|
|
129
164
|
ttl: this.ttl,
|
|
130
165
|
};
|
|
166
|
+
if (this.hostedBy) json.hostedBy = this.hostedBy;
|
|
167
|
+
return json;
|
|
131
168
|
}
|
|
132
169
|
|
|
133
170
|
/**
|
|
@@ -142,6 +179,7 @@ export class ResourceDescriptor {
|
|
|
142
179
|
availability: data.availability,
|
|
143
180
|
updatedAt: data.updatedAt,
|
|
144
181
|
ttl: data.ttl,
|
|
182
|
+
hostedBy: data.hostedBy,
|
|
145
183
|
});
|
|
146
184
|
}
|
|
147
185
|
}
|
|
@@ -277,13 +315,15 @@ export class ComputeRequest {
|
|
|
277
315
|
/**
|
|
278
316
|
* @param {object} opts
|
|
279
317
|
* @param {string} [opts.jobId]
|
|
280
|
-
* @param {'wasm'|'js'} opts.moduleType
|
|
318
|
+
* @param {'wasm'|'js'|'shell'} opts.moduleType
|
|
281
319
|
* @param {string} opts.moduleCid - Content-addressed ID of the module
|
|
282
320
|
* @param {string} opts.entry - Entry function name
|
|
283
321
|
* @param {*} [opts.input] - Serializable input payload
|
|
284
322
|
* @param {object} [opts.constraints]
|
|
285
323
|
* @param {string} [opts.constraints.prefer] - 'gpu'|'cpu'|'any'
|
|
286
324
|
* @param {string} [opts.constraints.preferRuntimeClass]
|
|
325
|
+
* @param {'any'|'isolate'|'microvm'} [opts.constraints.isolation] - Required
|
|
326
|
+
* placement lane (issue #185 §6). Default `'any'`.
|
|
287
327
|
* @param {string[]} [opts.constraints.capabilities]
|
|
288
328
|
* @param {number} [opts.constraints.timeoutMs]
|
|
289
329
|
* @param {number} [opts.constraints.maxMemoryMb]
|
|
@@ -301,6 +341,14 @@ export class ComputeRequest {
|
|
|
301
341
|
requesterId,
|
|
302
342
|
timestamp,
|
|
303
343
|
}) {
|
|
344
|
+
if (moduleType != null && !VALID_MODULE_TYPES.has(moduleType)) {
|
|
345
|
+
throw new Error(`moduleType must be one of 'wasm', 'js', 'shell' (got "${moduleType}")`);
|
|
346
|
+
}
|
|
347
|
+
const isolation = constraints.isolation ?? ISOLATION.ANY;
|
|
348
|
+
if (!VALID_ISOLATIONS.has(isolation)) {
|
|
349
|
+
throw new Error(`constraints.isolation must be one of 'any', 'isolate', 'microvm' (got "${isolation}")`);
|
|
350
|
+
}
|
|
351
|
+
|
|
304
352
|
this.jobId = jobId ?? _genId();
|
|
305
353
|
this.moduleType = moduleType;
|
|
306
354
|
this.moduleCid = moduleCid;
|
|
@@ -310,6 +358,7 @@ export class ComputeRequest {
|
|
|
310
358
|
...constraints,
|
|
311
359
|
prefer: constraints.prefer ?? 'any',
|
|
312
360
|
preferRuntimeClass: constraints.preferRuntimeClass ?? null,
|
|
361
|
+
isolation,
|
|
313
362
|
capabilities: [...(constraints.capabilities || [])],
|
|
314
363
|
timeoutMs: constraints.timeoutMs ?? 30_000,
|
|
315
364
|
maxMemoryMb: constraints.maxMemoryMb ?? null,
|
|
@@ -430,6 +479,9 @@ export class ComputeResult {
|
|
|
430
479
|
* Scoring heuristic (0-100+ scale):
|
|
431
480
|
* - Base: 50 points for being online
|
|
432
481
|
* - Preference match (gpu/cpu): +20
|
|
482
|
+
* - preferRuntimeClass match: +60 / mismatch: -15
|
|
483
|
+
* - Isolation lane ('isolate'/'microvm' hard requirement; 'any' prefers
|
|
484
|
+
* isolate for js/wasm and microvm for shell, +25): see issue #185 §6
|
|
433
485
|
* - Memory headroom: up to +15
|
|
434
486
|
* - CPU count: up to +10
|
|
435
487
|
* - Bandwidth: up to +5
|
|
@@ -449,6 +501,29 @@ export class ResourceScorer {
|
|
|
449
501
|
else if (descriptor.availability === 'busy') s += 30;
|
|
450
502
|
else return 0; // offline nodes score zero
|
|
451
503
|
|
|
504
|
+
// -- Isolation lane (issue #185 §6) --------------------------------------
|
|
505
|
+
const isolation = request.constraints?.isolation ?? ISOLATION.ANY;
|
|
506
|
+
const moduleType = request.moduleType;
|
|
507
|
+
|
|
508
|
+
// A 'shell' module can only ever run in a microvm pod, regardless of the
|
|
509
|
+
// requested isolation lane.
|
|
510
|
+
if (moduleType === 'shell' && !descriptor.capabilities.includes(`runtime:${RUNTIME_CLASS.MICROVM}`)) {
|
|
511
|
+
return 0;
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
if (isolation === ISOLATION.ISOLATE || isolation === ISOLATION.MICROVM) {
|
|
515
|
+
if (!descriptor.capabilities.includes(`runtime:${isolation}`)) {
|
|
516
|
+
return 0;
|
|
517
|
+
}
|
|
518
|
+
} else if (isolation === ISOLATION.ANY) {
|
|
519
|
+
if (moduleType !== 'shell' && (moduleType === 'js' || moduleType === 'wasm')
|
|
520
|
+
&& descriptor.capabilities.includes(`runtime:${RUNTIME_CLASS.ISOLATE}`)) {
|
|
521
|
+
s += 25;
|
|
522
|
+
} else if (moduleType === 'shell' && descriptor.capabilities.includes(`runtime:${RUNTIME_CLASS.MICROVM}`)) {
|
|
523
|
+
s += 25;
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
|
|
452
527
|
// Preference match
|
|
453
528
|
const prefer = request.constraints?.prefer ?? 'any';
|
|
454
529
|
if (prefer === 'gpu' && descriptor.resources.gpu > 0) s += 20;
|
|
@@ -504,6 +579,36 @@ export class ResourceScorer {
|
|
|
504
579
|
}
|
|
505
580
|
return best;
|
|
506
581
|
}
|
|
582
|
+
|
|
583
|
+
/**
|
|
584
|
+
* Compute the effective placement lane for a request, for logging/audit
|
|
585
|
+
* purposes (issue #185 §6/§8 WP4). Does not consult any descriptor -- it
|
|
586
|
+
* is purely a function of the request's own `moduleType`/
|
|
587
|
+
* `constraints.isolation`.
|
|
588
|
+
*
|
|
589
|
+
* @param {ComputeRequest} request
|
|
590
|
+
* @returns {{ required: 'isolate'|'microvm'|null, preferred: 'isolate'|'microvm'|null }}
|
|
591
|
+
* `required` is set when the lane is a hard constraint (an explicit
|
|
592
|
+
* `isolation` of `'isolate'`/`'microvm'`, or `moduleType: 'shell'`
|
|
593
|
+
* which always requires `microvm`). `preferred` is set when
|
|
594
|
+
* `isolation` is `'any'` and the module type implies a preferred lane
|
|
595
|
+
* (`js`/`wasm` -> `isolate`, `shell` -> `microvm`).
|
|
596
|
+
*/
|
|
597
|
+
static lane(request) {
|
|
598
|
+
const isolation = request.constraints?.isolation ?? ISOLATION.ANY;
|
|
599
|
+
const moduleType = request.moduleType;
|
|
600
|
+
|
|
601
|
+
if (moduleType === 'shell') {
|
|
602
|
+
return { required: RUNTIME_CLASS.MICROVM, preferred: null };
|
|
603
|
+
}
|
|
604
|
+
if (isolation === ISOLATION.ISOLATE || isolation === ISOLATION.MICROVM) {
|
|
605
|
+
return { required: isolation, preferred: null };
|
|
606
|
+
}
|
|
607
|
+
if (moduleType === 'js' || moduleType === 'wasm') {
|
|
608
|
+
return { required: null, preferred: RUNTIME_CLASS.ISOLATE };
|
|
609
|
+
}
|
|
610
|
+
return { required: null, preferred: null };
|
|
611
|
+
}
|
|
507
612
|
}
|
|
508
613
|
|
|
509
614
|
// ---------------------------------------------------------------------------
|
package/src/tools.mjs
CHANGED
|
@@ -9,6 +9,38 @@
|
|
|
9
9
|
|
|
10
10
|
import { BrowserTool } from './compat.mjs';
|
|
11
11
|
|
|
12
|
+
// ── IoT duck types ───────────────────────────────────────────────────
|
|
13
|
+
//
|
|
14
|
+
// This package ships no IoT implementation. The three iot_* tools call into a
|
|
15
|
+
// bridge and a telemetry store that the host application supplies, and are only
|
|
16
|
+
// registered (see `registerMeshTools()`) when one is passed in.
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* What `iot_list` and `iot_send` call. Supply an object with these methods as
|
|
20
|
+
* `registerMeshTools(registry, mux, ft, { iotBridge })`.
|
|
21
|
+
*
|
|
22
|
+
* @typedef {object} IoTBridgeLike
|
|
23
|
+
* @property {(filter?: { protocol?: string, capability?: string }) =>
|
|
24
|
+
* Array<{ deviceId: string, name?: string, protocol: string, capabilities: string[] }>} listDevices
|
|
25
|
+
* Registered devices, optionally filtered by `protocol` and/or `capability`
|
|
26
|
+
* (called with `undefined` when neither is given).
|
|
27
|
+
* @property {(deviceId: string, payload: object) => Promise<void>|void} send
|
|
28
|
+
* Deliver `payload` to the device; throw or reject on failure.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* What `iot_telemetry` calls. Supply as
|
|
33
|
+
* `registerMeshTools(registry, mux, ft, { iotTelemetry })`.
|
|
34
|
+
*
|
|
35
|
+
* @typedef {object} IoTTelemetryLike
|
|
36
|
+
* @property {(deviceId: string, since?: number, until?: number) =>
|
|
37
|
+
* Array<{ ts: number, value: * }>} query
|
|
38
|
+
* Samples for a device, oldest first; `ts` is epoch milliseconds.
|
|
39
|
+
* @property {(deviceId: string) =>
|
|
40
|
+
* { min: number, max: number, avg: number, count: number, last: * }|null} getStats
|
|
41
|
+
* Summary statistics, or `null` when the device has no samples.
|
|
42
|
+
*/
|
|
43
|
+
|
|
12
44
|
// ── Shared state ─────────────────────────────────────────────────────
|
|
13
45
|
|
|
14
46
|
/**
|
|
@@ -35,10 +67,14 @@ export class MeshToolsContext {
|
|
|
35
67
|
setTrainingOrchestrator(orch) { this.#trainingOrchestrator = orch; }
|
|
36
68
|
getTrainingOrchestrator() { return this.#trainingOrchestrator; }
|
|
37
69
|
|
|
70
|
+
/** @param {IoTBridgeLike|null} bridge */
|
|
38
71
|
setIoTBridge(bridge) { this.#iotBridge = bridge; }
|
|
72
|
+
/** @returns {IoTBridgeLike|null} */
|
|
39
73
|
getIoTBridge() { return this.#iotBridge; }
|
|
40
74
|
|
|
75
|
+
/** @param {IoTTelemetryLike|null} telemetry */
|
|
41
76
|
setIoTTelemetry(telemetry) { this.#iotTelemetry = telemetry; }
|
|
77
|
+
/** @returns {IoTTelemetryLike|null} */
|
|
42
78
|
getIoTTelemetry() { return this.#iotTelemetry; }
|
|
43
79
|
}
|
|
44
80
|
|
|
@@ -641,14 +677,32 @@ export class IoTTelemetryTool extends BrowserTool {
|
|
|
641
677
|
// ── Registry helper ──────────────────────────────────────────────────
|
|
642
678
|
|
|
643
679
|
/**
|
|
644
|
-
* Register
|
|
680
|
+
* Register the mesh stream/file/DHT/GPU tools with a BrowserToolRegistry (12
|
|
681
|
+
* tools), plus the IoT tools the caller opts into.
|
|
682
|
+
*
|
|
683
|
+
* The IoT tools need an implementation this package does not ship, so they are
|
|
684
|
+
* registered only when you supply one: `iot_list` and `iot_send` when
|
|
685
|
+
* `options.iotBridge` is given, `iot_telemetry` when `options.iotTelemetry` is.
|
|
686
|
+
* Without them the agent never sees tools that could only fail. See
|
|
687
|
+
* {@link IoTBridgeLike} and {@link IoTTelemetryLike} for the duck types.
|
|
688
|
+
*
|
|
645
689
|
* @param {import('./compat.mjs').BrowserToolRegistry} registry
|
|
646
690
|
* @param {import('@johnhenry/browsermesh-transport').StreamMultiplexer} [multiplexer]
|
|
647
691
|
* @param {import('@johnhenry/browsermesh-sync').MeshFileTransfer} [fileTransfer]
|
|
692
|
+
* @param {object} [options]
|
|
693
|
+
* @param {IoTBridgeLike} [options.iotBridge] - Enables `iot_list` and `iot_send`.
|
|
694
|
+
* @param {IoTTelemetryLike} [options.iotTelemetry] - Enables `iot_telemetry`.
|
|
695
|
+
* @throws {TypeError} If a supplied bridge/telemetry lacks a required method.
|
|
648
696
|
*/
|
|
649
|
-
export function registerMeshTools(registry, multiplexer, fileTransfer) {
|
|
697
|
+
export function registerMeshTools(registry, multiplexer, fileTransfer, options = {}) {
|
|
698
|
+
const { iotBridge, iotTelemetry } = options || {};
|
|
699
|
+
assertHasMethods('iotBridge', iotBridge, ['listDevices', 'send']);
|
|
700
|
+
assertHasMethods('iotTelemetry', iotTelemetry, ['query', 'getStats']);
|
|
701
|
+
|
|
650
702
|
if (multiplexer) meshToolsContext.setMultiplexer(multiplexer);
|
|
651
703
|
if (fileTransfer) meshToolsContext.setFileTransfer(fileTransfer);
|
|
704
|
+
if (iotBridge) meshToolsContext.setIoTBridge(iotBridge);
|
|
705
|
+
if (iotTelemetry) meshToolsContext.setIoTTelemetry(iotTelemetry);
|
|
652
706
|
|
|
653
707
|
registry.register(new MeshStreamOpenTool());
|
|
654
708
|
registry.register(new MeshStreamCloseTool());
|
|
@@ -662,7 +716,21 @@ export function registerMeshTools(registry, multiplexer, fileTransfer) {
|
|
|
662
716
|
registry.register(new DhtPeersTool());
|
|
663
717
|
registry.register(new GpuTrainStartTool());
|
|
664
718
|
registry.register(new GpuTrainStatusTool());
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
719
|
+
if (iotBridge) {
|
|
720
|
+
registry.register(new IoTListTool());
|
|
721
|
+
registry.register(new IoTSendTool());
|
|
722
|
+
}
|
|
723
|
+
if (iotTelemetry) {
|
|
724
|
+
registry.register(new IoTTelemetryTool());
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
/** @param {string} name @param {*} value @param {string[]} methods */
|
|
729
|
+
function assertHasMethods(name, value, methods) {
|
|
730
|
+
if (value === undefined || value === null) return;
|
|
731
|
+
for (const m of methods) {
|
|
732
|
+
if (typeof value[m] !== 'function') {
|
|
733
|
+
throw new TypeError(`registerMeshTools: options.${name} must implement ${m}()`);
|
|
734
|
+
}
|
|
735
|
+
}
|
|
668
736
|
}
|