@johnhenry/browsermesh-apps 0.11.0 → 0.12.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/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
- return {
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
  // ---------------------------------------------------------------------------