@threadedstack/wmd-web 0.1.39 → 0.1.42

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.
Files changed (57) hide show
  1. package/README.md +243 -1
  2. package/RELEASE.json +84 -49
  3. package/guest/arm64/Image +1 -1
  4. package/guest/arm64/Image-4k +1 -1
  5. package/guest/arm64/initramfs.cpio.gz +0 -0
  6. package/package.json +3 -3
  7. package/src/boot.js +231 -4
  8. package/src/edge/auto.js +77 -0
  9. package/src/edge/base.js +10 -0
  10. package/src/edge/bridge.js +76 -7
  11. package/src/edge/ctl.js +21 -4
  12. package/src/edge/error-kind.js +6 -0
  13. package/src/edge/frame-code.gen.js +6 -0
  14. package/src/edge/frame.js +58 -15
  15. package/src/edge/msg.js +3 -0
  16. package/src/edge/own/adapter.js +223 -19
  17. package/src/edge/own/exit-now.js +62 -0
  18. package/src/edge/own/fs-own-route.js +40 -3
  19. package/src/edge/own/fs-stats.js +11 -0
  20. package/src/edge/own/hub.js +126 -14
  21. package/src/edge/own/loader.js +3 -2
  22. package/src/edge/own/marks.js +158 -0
  23. package/src/edge/own/net-host.js +873 -0
  24. package/src/edge/own/node-globals.js +142 -0
  25. package/src/edge/own/pool.js +30 -1
  26. package/src/edge/own/service-receiver.js +7 -1
  27. package/src/edge/own/shim-post.js +38 -0
  28. package/src/edge/own/worker.js +34 -1
  29. package/src/edge/page.js +448 -25
  30. package/src/edge/pins.gen.js +17 -0
  31. package/src/edge/pins.js +21 -0
  32. package/src/edge/plan.js +45 -9
  33. package/src/edge/python-module.js +18 -0
  34. package/src/edge/python-pins.js +64 -0
  35. package/src/edge/python-plan.js +280 -0
  36. package/src/edge/python-source.js +34 -0
  37. package/src/edge/runtime.js +15 -2
  38. package/src/edge/sbx/cap.js +48 -0
  39. package/src/edge/sbx/common.js +186 -0
  40. package/src/edge/sbx/doc.js +121 -0
  41. package/src/edge/sbx/files.js +112 -0
  42. package/src/edge/sbx/frame.js +126 -0
  43. package/src/edge/sbx/gate.js +115 -0
  44. package/src/edge/sbx/sha256.js +68 -0
  45. package/src/edge/sbx/worker-entry.js +36 -0
  46. package/src/edge/sems.js +113 -0
  47. package/src/edge/service-host.js +57 -5
  48. package/src/edge/service-port.js +79 -5
  49. package/src/edge/setup.js +278 -0
  50. package/src/edge/shim.js +3 -1
  51. package/src/edge/watch.js +101 -0
  52. package/src/edge/worker.js +31 -2
  53. package/src/gowasm/setup.js +9 -6
  54. package/src/host/chan-service.js +8 -5
  55. package/src/host/chan-services.js +29 -4
  56. package/src/host/edge-host.js +58 -3
  57. package/wmdpack.wasm +0 -0
@@ -30,7 +30,7 @@
30
30
  import {
31
31
  CHAN_CONSOLE, CHAN_CONTROL, CHAN_NET, CHAN_P9, CONSOLE_IN_LIMIT, G2H, H2G, H_CIN_TAKEN, H_CLOSES, H_GONE, H_P9COUNT, KIND_NAMES, P9C_LOWER,
32
32
  P9C_NONE, P9C_UPPER, R_GONE, R_SHUT, RTT_BUCKETS, S_GEN, S_PORT, S_STATE, ST_CLOSING, ST_FREE, ST_OPEN, chanPorts, chanView, histSummary,
33
- p9cServed, ringAvail, ringKick, ringRead, ringSpace, ringWrite, rttBucket, stageNote,
33
+ p9cServed, ringAvail, ringBell, ringKick, ringRead, ringSpace, ringWrite, rttBucket, stageNote,
34
34
  } from './chan.js'
35
35
  import { NinePServer } from '../fs/9p/server.js'
36
36
  import { adoptServer, dumpService, loadService } from '../fs/svc-snapshot.js'
@@ -295,7 +295,7 @@ class ControlSession {
295
295
  ctl.epoch++
296
296
  this.epoch = ctl.epoch
297
297
  this.yamux = new YamuxClient({
298
- send: u8 => sess.out.push(u8),
298
+ send: u8 => (sess.out.push(u8), host.queuedOut()),
299
299
  keepAliveMs: host.ctlKeepAliveMs,
300
300
  onClose: why => host.ctlSessionDown(this, why),
301
301
  // streams the guest init opens ({"op":"edge"}): X6's compiler hand-off (default off) and X5's node host (edge=1 only)
@@ -417,7 +417,7 @@ export class ChanHost {
417
417
  * @param {number} [o.consoleInputBytes] console input (page -> guest) held at most (CONSOLE_IN_LIMIT)
418
418
  * @param {{accept: (s: object) => void, snapshot: () => object}} [o.edge] X6: takes the streams the guest opens on the control session (edge-go.js GoEdge)
419
419
  */
420
- constructor(sab, { fs = {}, onConsole, consoleInputBytes = CONSOLE_IN_LIMIT, yamuxPorts = [], ctlKeepAliveMs = 5000, netPost = null, restore = {}, edge = null, edgeAccept = null } = {}) {
420
+ constructor(sab, { fs = {}, onConsole, consoleInputBytes = CONSOLE_IN_LIMIT, yamuxPorts = [], ctlKeepAliveMs = 5000, netPost = null, restore = {}, edge = null, edgeAccept = null, edgeFlushed = null } = {}) {
421
421
  this.v = chanView(sab)
422
422
  this.edge = edge
423
423
  // P-B10: the network Worker (postMessage to it), and the net channel's session
@@ -431,6 +431,7 @@ export class ChanHost {
431
431
  this.restoredServers.set(Number(port), loadService(bytes, fs[port]))
432
432
  }
433
433
  this.yamuxPorts = new Set(yamuxPorts)
434
+ this.edgeFlushed = edgeFlushed // X25 gate 2e: called with the page's clock when a flush of the control session put bytes into the guest's ring (edge-host.js flushed)
434
435
  this.edgeAccept = edgeAccept // X5 (edge=1 only): called with each stream the guest opens on the control session; none: refused
435
436
  this.ctlKeepAliveMs = ctlKeepAliveMs
436
437
  // P-B11: the control session and the page's streams on it (k -> YamuxStream)
@@ -451,6 +452,17 @@ export class ChanHost {
451
452
  errors: [],
452
453
  }
453
454
  this.stopped = false
455
+ this.stepping = false // inside step(): what is queued for the guest is flushed by this step
456
+ }
457
+
458
+ /**
459
+ * Bytes were queued for the guest. Inside a step the step flushes them. Outside one (a promise job: the edge host's answer to a
460
+ * stub's hello after its owner decided, X5 B2.3) nothing would take them to the guest until the guest next rang the doorbell or the
461
+ * service's 500 ms wait ran out (chan-service.js loop()), and the guest waited idle for them: ring the doorbell, so the service's
462
+ * next step sends them.
463
+ */
464
+ queuedOut() {
465
+ if (!this.stepping) ringBell(this.v)
454
466
  }
455
467
 
456
468
  /** Queue an event for the page (chan-service.js posts ctlEvents after each step). */
@@ -677,6 +689,15 @@ export class ChanHost {
677
689
 
678
690
  /** One pass over every channel. Returns true if anything moved. */
679
691
  step() {
692
+ this.stepping = true
693
+ try {
694
+ return this.#step()
695
+ } finally {
696
+ this.stepping = false
697
+ }
698
+ }
699
+
700
+ #step() {
680
701
  const v = this.v
681
702
  const i32 = v.i32
682
703
  if (Atomics.load(i32, H_GONE)) {
@@ -736,13 +757,16 @@ export class ChanHost {
736
757
  sess.impl.tick?.()
737
758
  const before = sess.out.bytes
738
759
  let wrote = sess.out.flush(v, h)
760
+ if (wrote && sess.kind === CHAN_CONTROL) this.edgeFlushed?.()
739
761
  if (sess.kind === CHAN_CONSOLE && wrote) Atomics.add(v.i32, H_CIN_TAKEN, wrote) // console input taken by the ring
740
762
  if (!sess.broken && sess.impl.pending() && sess.out.bytes < OUT_LIMIT) {
741
763
  // requests held back while the replies were not read
742
764
  const n = sess.impl.pending()
743
765
  sess.impl.process(sess)
744
766
  if (sess.impl.pending() !== n) work = true
745
- wrote += sess.out.flush(v, h)
767
+ const w1 = sess.out.flush(v, h)
768
+ if (w1 && sess.kind === CHAN_CONTROL) this.edgeFlushed?.()
769
+ wrote += w1
746
770
  }
747
771
  if (wrote) {
748
772
  ks.bytesOut += wrote
@@ -764,6 +788,7 @@ export class ChanHost {
764
788
  work = true
765
789
  sess.impl.data(sess, this.inbuf.subarray(0, n))
766
790
  const w2 = sess.out.flush(v, h)
791
+ if (w2 && sess.kind === CHAN_CONTROL) this.edgeFlushed?.()
767
792
  if (sess.kind === CHAN_CONSOLE && w2) Atomics.add(v.i32, H_CIN_TAKEN, w2)
768
793
  ks.bytesOut += w2
769
794
  }
@@ -58,6 +58,9 @@ export function helloRefusal(h) {
58
58
  const tz = (h.env || []).findLast(kv => kv.startsWith('TZ='))
59
59
  if (tz !== undefined && !isUtcZone(tz.slice(3))) return `${tz}: the frame's clock zone is UTC`
60
60
  const base = String((h.argv || [])[0] || '').split('/').pop()
61
+ // X25 gate 2: a python start is the owner's to decide (src/edge/service-host.js, src/edge/python-plan.js): the stub stands over python only when the page's
62
+ // plan took the image's python, and the owner answers the cases of the command line, the tree and the environment with a fallback and its reason
63
+ if (/^python/.test(base)) return ''
61
64
  if (!/^node/.test(base)) return `${base || 'no program'} is not node`
62
65
  return ''
63
66
  }
@@ -79,10 +82,17 @@ export class EdgeHost {
79
82
  this.start = start
80
83
  this.startTimeoutMs = startTimeoutMs
81
84
  this.log = log
82
- this.stats = { streams: 0, notEdge: 0, fallbacks: 0, runs: 0, exits: 0, badFrames: 0, outBytes: { stdout: 0, stderr: 0 }, outEnded: 0, fallbackWhy: Object.create(null) } // null prototype: the names are the frame's (check5 F4)
85
+ this.stats = { streams: 0, notEdge: 0, fallbacks: 0, runs: 0, exits: 0, badFrames: 0, outBytes: { stdout: 0, stderr: 0 }, outEnded: 0, fallbackWhy: Object.create(null), trips: [] } // null prototype: the names are the frame's (check5 F4)
86
+ this.txWait = [] // [trip, field] stamps that wait for the flush that puts the bytes just queued into the guest's ring (flushed())
83
87
  this.stubs = new Set() // the stubs connected now: from the guest's stream until the stub's end (hello, reply and the whole run included)
84
88
  }
85
89
 
90
+ /** The channel service put what was queued for the guest into its ring at `t` (page clock): the stamps that waited for it get it. */
91
+ flushed(t = at()) {
92
+ for (const [trip, k] of this.txWait) if (trip[k] === undefined) trip[k] = t
93
+ this.txWait.length = 0
94
+ }
95
+
86
96
  /** How many stubs are connected. A stub that waits for a reply, or whose program runs here, cannot be carried over a checkpoint. */
87
97
  openStubs() {
88
98
  return this.stubs.size
@@ -108,6 +118,7 @@ class OuterConn {
108
118
  this.buf = new Uint8Array(0)
109
119
  this.nested = null
110
120
  st.onData = u8 => {
121
+ if (this.first === undefined) host.lastOf = this.first = at()
111
122
  st.consume(u8.length)
112
123
  if (this.nested) return this.nested.feed(u8)
113
124
  const b = new Uint8Array(this.buf.length + u8.length)
@@ -202,6 +213,10 @@ export function execStream(session) {
202
213
  }
203
214
  }
204
215
 
216
+ // X25 gate 2e: the page's clock every Worker shares (timeOrigin + now); the stamps of one stub's start are kept in stats.trips (the last 64)
217
+ const at = () => performance.timeOrigin + performance.now()
218
+ const TRIPS_KEPT = 64
219
+
205
220
  class EdgeConn {
206
221
  constructor(host, st, hold, session = null) {
207
222
  this.host = host
@@ -210,6 +225,8 @@ class EdgeConn {
210
225
  this.hold = hold
211
226
  this.buf = new Uint8Array(0)
212
227
  this.phase = 'hello' // hello -> wait -> run
228
+ this.trip = null // {of, hi, rp, ex}: the stamps of this start on the page's clock
229
+ this.of = host.lastOf // the first bytes of the outer stream this stub came by
213
230
  this.runner = null
214
231
  this.ended = false
215
232
  st.onData = u8 => {
@@ -241,6 +258,9 @@ class EdgeConn {
241
258
  const p = this.buf.slice(5, 5 + n)
242
259
  this.buf = this.buf.subarray(5 + n)
243
260
  if (this.phase === 'hello') {
261
+ // X5 net: a stream the stub opened for a connection its listener took is not a hello: its header says `accepted` and what follows is the
262
+ // connection's bytes. The run's network host (set by its start, ctx.setAccepted) takes it; with none, or when it refuses, the stream is reset.
263
+ if (type === FRAME_HDR && this.session && p.length < 512 && this.#accepted(p)) return
244
264
  if (type !== FRAME_HDR) {
245
265
  this.host.stats.badFrames++
246
266
  return this.st.abort('no hello')
@@ -253,7 +273,35 @@ class EdgeConn {
253
273
  }
254
274
  }
255
275
 
276
+ /** True when `p` is an `accepted` header and the stream was handled (taken by the run's network host or reset). */
277
+ #accepted(p) {
278
+ let h
279
+ try {
280
+ h = JSON.parse(dec.decode(p))
281
+ } catch {
282
+ return false
283
+ }
284
+ if (!h || h.kind !== 'accepted') return false
285
+ const st = this.st
286
+ const rest = this.buf
287
+ this.buf = new Uint8Array(0)
288
+ this.ended = true // this connection object does nothing more with the stream
289
+ this.host.stats.accepted = (this.host.stats.accepted || 0) + 1
290
+ let ok = false
291
+ try {
292
+ ok = typeof this.session.accepted === 'function' && this.session.accepted(st, h, rest) !== false
293
+ } catch {
294
+ ok = false
295
+ }
296
+ if (!ok && !st.closed) st.abort('no listener for this stream')
297
+ return true
298
+ }
299
+
256
300
  #reply(r) {
301
+ if (this.trip) {
302
+ this.trip.rp = at()
303
+ this.host.txWait.push([this.trip, 'rw'])
304
+ }
257
305
  if (!this.ended) this.st.write(frame(FRAME_REPLY, r))
258
306
  }
259
307
 
@@ -266,6 +314,9 @@ class EdgeConn {
266
314
  }
267
315
 
268
316
  async #hello(p) {
317
+ const trips = this.host.stats.trips
318
+ trips.push((this.trip = { of: this.of ?? at(), hi: at() }))
319
+ if (trips.length > TRIPS_KEPT) trips.splice(0, trips.length - TRIPS_KEPT)
269
320
  let h
270
321
  try {
271
322
  h = JSON.parse(dec.decode(p))
@@ -281,7 +332,7 @@ class EdgeConn {
281
332
  // an owner that never answers (a page that lost its frame, a machine that ended) must not hold the stub for ever
282
333
  let timer
283
334
  let late = false
284
- const started = Promise.resolve(this.host.start(h, { openExec: () => execStream(this.session) }))
335
+ const started = Promise.resolve(this.host.start(h, { openExec: () => execStream(this.session), openStream: () => this.session.open(), setAccepted: fn => (this.session.accepted = fn) }))
285
336
  started.then(x => late && x && x.runner?.gone?.(), () => {}) // a start that comes after the timeout is let go at once
286
337
  r = await Promise.race([
287
338
  started,
@@ -302,7 +353,11 @@ class EdgeConn {
302
353
  r.runner.onExit = e => {
303
354
  if (this.ended) return
304
355
  this.host.stats.exits++
305
- this.st.write(frame(FRAME_EXIT, e.signal ? { code: e.code, signal: e.signal } : { code: e.code }))
356
+ if (this.trip) {
357
+ this.trip.ex = at()
358
+ this.host.txWait.push([this.trip, 'ew'])
359
+ }
360
+ this.st.write(frame(FRAME_EXIT, e.signal ? { code: e.code, signal: e.signal } : e.clean === true ? { code: e.code, clean: true } : { code: e.code }))
306
361
  this.st.closeWrite()
307
362
  }
308
363
  this.#reply({ ok: true })
package/wmdpack.wasm CHANGED
Binary file