@threadedstack/wmd-web 0.1.38 → 0.1.39

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 CHANGED
@@ -2778,6 +2778,8 @@ attributes the guest has seen across a cache drop (the page's `ck.dropNames()`),
2778
2778
  - `scripts/speed-suite/p9count.sh` prints `P9C-RAN names|plain`, read from the guest's own mount line, and discards a run whose line does not match
2779
2779
  what the query named.
2780
2780
 
2781
- ## The Go compiler's Wasm twin (X6, `gowasm=1`, off by default)
2781
+ ## The Go compiler's Wasm twin (X6; off until a base url is set)
2782
2782
 
2783
- With the page option `gowasm=1` the guest's `go build` compiles of a known Go release run in a Wasm build of the same compiler in a Worker of the page, and give the same object files. The twin modules (about 41 MB per Go release) are **not** in this package or in the release. An embedder builds them (`make gowasm GOVER=1.25.14 OUT=<folder>`, then `scripts/gowasm/check-twin.sh` and `bun scripts/gowasm/make-table.mjs`), serves the folder (`tools.json` and `compile-<version>.wasm`) from its own origin, and hands the table to `containerBundle` (`edge`) and `bootWmd` (the 9P service's `gowasm` field) as `web/page/ckpt-gowasm.js` does. The table is keyed by the sha256 of the image's own `compile` file and names one module (url and sha256) per entry; the Worker checks each module's hash before it compiles it. An image whose compile is not in the table gets no twin and builds as it does without the switch. Build command, folder layout, table shapes, verification and the images that carry a listed compile: `docs/gowasm-twin.md`.
2783
+ An embedder does nothing to get the twin: update the package and call `containerBundle` and `bootWmd` as before, with no line of twin code. `bootWmd` reads the pins (`src/gowasm/pins-data.js`, a JS module that ships in the package, no request), adds the stub mounts to the bundle's spec and sets the service's `gowasm` field from it; the image decides (an image with no Go compile, or a Go release with no pin, runs as before and downloads nothing). The twin's module (41 to 53 MB per Go release) is fetched by the twin's Worker on first use from an ordered list of sources (`GOWASM_SOURCES_DEFAULT` in `src/gowasm/setup.js`, `null` for now: the twin is off until a page or the default names sources): the first that gives the pinned sha256 and size wins, a source that fails, stalls, is too big, refuses the file or has a wrong hash is left for the next (jsDelivr refuses files over 50 MB, so for the largest twin the working sources are the registry tarball and unpkg: each release needs only one source that works), and when all fail the guest compiles, with one `gowasm-off` event that names each source. Source kinds: `{ kind: 'tgz', url }` (an npm registry tarball) and `{ kind: 'file', base }` (`<base>/<file>`); only the pins name the package. The module is kept in Cache Storage of the page's origin under a key that holds its hash (checked again at each boot). What a consumer may pass to `bootWmd`: `gowasm: false` (off), `gowasm: { sources: [...] }`, `gowasm: { base: 'https://.../gowasm' }` (the override for an app that serves its own copy of the files `compile-<go release>.wasm`), `gowasm: { limits }`. The one CSP line an app with a Content-Security-Policy needs, with the default sources: `connect-src 'self' https://registry.npmjs.org https://cdn.jsdelivr.net https://unpkg.com`. Test page words: `gowasm=1`, `gowasm=auto` (only a table the page states by `gowasmtools=<url>` or `<meta name="wmd-gowasm-table" content="<url>">`), `gowasmbase=<url>`. A checkpoint keeps the twin choice it was taken with (a checkpoint taken with the twin always restores: with `gowasm: false` the stub mounts stay, no twin Worker starts, every compile runs in the guest, one `gowasm-off` event, rule `checkpoint-twin`; one taken without it restores without it). Headers, limits, the cache, the checkpoint rule and the images that carry a listed compile: `docs/gowasm-twin.md`.
2784
+
2785
+ Off is off: with `gowasm: false` (or no twin source and no table) there is no twin file requested, no twin Worker started, no stub mount in the container (the guest's `/proc/mounts` has none), and no event. What the page does load is four small library modules that main's page does not, because the checkpoint rule needs the twin's spec text even when the twin is off: `src/gowasm/boot-setup.js`, `src/gowasm/setup.js`, `src/gowasm/pins-data.js` and `src/host/gowasm-guard.js`, 4 requests and 39,808 bytes uncompressed (counted once in Chromium against a copy of `origin/main`'s `web/`: 228 requests against 225, 65 distinct paths against 61; `web/test/x6/fix10-count.spec.js`). Nothing else differs in what the page asks for.
package/RELEASE.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "wmd-web",
3
3
  "version": "0.5.1",
4
- "commit": "19c9d61297cf7d3c531935ba4e13610730eaf34a",
4
+ "commit": "6b1f00376baee9fe58ee14b13752c505c8e75573",
5
5
  "previous": null,
6
6
  "decisions": [
7
7
  "D-388",
@@ -69,7 +69,9 @@
69
69
  "D-640",
70
70
  "D-641",
71
71
  "D-642",
72
- "D-652"
72
+ "D-652",
73
+ "D-653",
74
+ "D-654"
73
75
  ],
74
76
  "vmSources": "396ad82ca79a77c77620c75791f60cc3b83c6a1c",
75
77
  "guest": {
@@ -80,16 +82,16 @@
80
82
  "bytes": 1567615
81
83
  },
82
84
  "Image": {
83
- "sha256": "daa4dc90fe59ceda984ba97342f0abab0a45e3ba1a14d361d142234347f5a9e4",
85
+ "sha256": "8542df8ca5f5b1b543129281e70e9906220da0ef299d93af190df2f953237b97",
84
86
  "bytes": 39049224
85
87
  },
86
88
  "Image-4k": {
87
- "sha256": "284de4fdb2b7a25f4ecabd03f03aaa56c1d22f99082f6c5c2db2fb0b7c5a6c36",
89
+ "sha256": "696a4ce9d2fdc9b5a945f49f5faeb59f744fbdea9d0d69216afc6d29677398f0",
88
90
  "bytes": 38828040
89
91
  },
90
92
  "initramfs.cpio.gz": {
91
- "sha256": "cef3a42510e38748c181c56165ad8b30520ec29fdb82f5822fdef0de3f3e5d02",
92
- "bytes": 4715393
93
+ "sha256": "ea7cf5e117d4d0361ac3f9fe152729d5fa36746bc4017eea1c760227983628b8",
94
+ "bytes": 4720468
93
95
  },
94
96
  "features": {
95
97
  "sha256": "02693b8b6c099e10fdb7a092394102ee92971ce1e39d0d6e5327ff8be1d0ee74",
@@ -143,7 +145,7 @@
143
145
  }
144
146
  },
145
147
  "files": {
146
- "README.md": "b95e4c26b7ea033060d8a38af27a3b44695ee02f99d870836a61b0e183d94594",
148
+ "README.md": "88f523deff7d9c7fc9f4b14454d6d036cfa6570ad21d1d3c55b027d6d7f5776a",
147
149
  "edge-site/own/HASHES.txt": "e79eaf7a2e1200dd4f638f87a2faaa3e7ad799af83ebff1c33abb9b68c88cf04",
148
150
  "edge-site/own/NOTICES.txt": "8ceca5406ee5833737bd5fa4a41d434bf5e4d26eff6da1e42c693176ee113101",
149
151
  "edge-site/own/acorn/LICENSE": "76a876cf886ff9be2a8b5e2e86514fed06223c8c9f0c1e9ee9606e93841e00b7",
@@ -158,15 +160,15 @@
158
160
  "export-page/index.html": "f5450c89e63601bbbbffef421f03491b231d1f143070012c1c5315d4b0059f43",
159
161
  "export-page/run.css": "b57adf1e76553549c1f89ff157b7973409dedc21889cd3932948cd05b51adc6f",
160
162
  "export-page/run.js": "717636a632a40469106ddf1383e198eaac5d83651b5866d0699ec04b82462b7f",
161
- "guest/arm64/Image": "daa4dc90fe59ceda984ba97342f0abab0a45e3ba1a14d361d142234347f5a9e4",
162
- "guest/arm64/Image-4k": "284de4fdb2b7a25f4ecabd03f03aaa56c1d22f99082f6c5c2db2fb0b7c5a6c36",
163
+ "guest/arm64/Image": "8542df8ca5f5b1b543129281e70e9906220da0ef299d93af190df2f953237b97",
164
+ "guest/arm64/Image-4k": "696a4ce9d2fdc9b5a945f49f5faeb59f744fbdea9d0d69216afc6d29677398f0",
163
165
  "guest/arm64/features": "02693b8b6c099e10fdb7a092394102ee92971ce1e39d0d6e5327ff8be1d0ee74",
164
- "guest/arm64/initramfs.cpio.gz": "cef3a42510e38748c181c56165ad8b30520ec29fdb82f5822fdef0de3f3e5d02",
166
+ "guest/arm64/initramfs.cpio.gz": "ea7cf5e117d4d0361ac3f9fe152729d5fa36746bc4017eea1c760227983628b8",
165
167
  "guest/arm64/vm.wasm": "1553b50b591cc579d2ce24e79c3fd72360db34a8a337c108e25e515a5eb920b6",
166
168
  "package.json": "7d43c05047d0a3ea56b4a7709f48a60de32e15c3bdf328d55435c46637981b40",
167
169
  "src/base.js": "982f8e46dbb727ec542a494ebdb884ccd19a31ff4e091425b2d1fc01b0d4754a",
168
170
  "src/blob.js": "bb2a2f0d87ce236280a7be2f246c926010dfe07f649c27da5e70d8eb5eaf7061",
169
- "src/boot.js": "5e2dfbd60cefc01c7cda07957c969d62ce507e56a810098eb4814dad6430c202",
171
+ "src/boot.js": "4c4260088f8a1d9b7dcc031f5e9f90411787cda3648e204844291f416d452455",
170
172
  "src/build/builder.js": "c7794ec15b183213228776c8f4fb0950dec7e621f948358da7de8f90ef4f70ec",
171
173
  "src/build/context.js": "d38097c61dbdadc4d41290c010425a4422f499aba473fa2b83f2a5c41e543571",
172
174
  "src/build/dockerfile.js": "5957fe3d4ac8ff8b312f980ee04bd1d98ad4373a61f9a33e02f1ff1108b6ea53",
@@ -234,34 +236,40 @@
234
236
  "src/fs/seed.js": "1cd2765e5d28e961bc4e81b41898f768a63e24feaf44e0aa42bd23e5cb910a74",
235
237
  "src/fs/svc-snapshot.js": "eae82a6e136f9430680197c617601028da35cbe8786df39d6cab28280a2f9a7c",
236
238
  "src/fs/upper-pack.js": "737a7e2bb5aac0ff774645db5789d699adfff263ca6f979e6687a9183a47f9dc",
239
+ "src/gowasm/boot-setup.js": "7033465098fe5b70611bb89bc56f9ffda08de4a6699b5cfc087161de694a72ea",
237
240
  "src/gowasm/fetch-twin.mjs": "de7c5f29c069dc3b009964b8b96084d567ef15d641238b1122a0d7f2c473f799",
241
+ "src/gowasm/make-pins-module.mjs": "d13db3b38fdfe37e4db66f3b7d0433df15d590260433b295e2654af12feacd75",
242
+ "src/gowasm/pins-data.js": "71fc2e21e2363de883823bccea5bb9ca3c46077e6fab56a04cfa511739563e19",
238
243
  "src/gowasm/pins.js": "d4426f97e73ee5703d12912f803a46d64662da4cdeda8040b942b3621ada1636",
239
244
  "src/gowasm/pins.json": "0f40d8553c19915b328263077097b0c353072ec84d2afd5cf388e92a2fde9cce",
245
+ "src/gowasm/setup.js": "2e58f9e51e4d658abbd1c9cd16ba2197dae2cc9c20f20ce0d137c212c5bbdd1f",
246
+ "src/gowasm/twin-fetch.js": "c5d53ccb6ee21c0ffc76d4c9f41f2dea1ceec45a3fe26d5b98cfbd4b9aecce70",
240
247
  "src/host/base-cache.js": "e0a9ea65e4eb9e30e7f78b28d9e5653b395f8eaa6c5e4c75b92f123c8d6a76a9",
241
248
  "src/host/base-file.js": "edb47c1eeb6b1cec627fa5894512185336b15d3f5db96a7151a8a57b34b4a2e8",
242
249
  "src/host/base-io.js": "cdd049875dce9f1417f28b27efa2c6f494cbe2a49851249150d0c1ec871a8b59",
243
250
  "src/host/boot-events.js": "a6567c889fa1e95ad7e917107a2593f03d14cc921827616368b3417de96faad2",
244
- "src/host/chan-service.js": "da34324ede7ab2cb51769ef487808de1f42ecbee468035c112fc45a0a87e2ec2",
251
+ "src/host/chan-service.js": "b21c98bdb0403124c9bf14b0c084ade871736ef9458d8ba46816d62d7dba9076",
245
252
  "src/host/chan-services.js": "aab7a60789da260b16549d5f0895e539cce0d086c066afa033d2b5840186ded4",
246
253
  "src/host/chan.js": "bb9e76890b0efeee7cfbd8481b9fb4620b3b184459d96d62ff68ddcdc8a46f83",
247
254
  "src/host/ckpt-file.js": "da7938fa640a062e77bdfb59caad5b3b1fcbf6150e3220de3be23f253f4c4497",
248
255
  "src/host/ckpt-io.js": "3fbe74f0254c597f10d088c85e8ba72725502d86ae4dd8c6d2bf0821a93c873f",
249
256
  "src/host/ckpt-point.js": "42bc16b3f754de3cd4e39f37a0feca2ffd8b4231cd72ec32bd6e1ea734c3c8c7",
250
257
  "src/host/ckpt-save.js": "e222571033ae74a4439596c5e90e03b2aa9a5516665dd290f8dd243712294523",
251
- "src/host/ckpt.js": "4e61414c21089622dd9523aeca78b2fcce6e2cfd74917af5d6891859b0316857",
258
+ "src/host/ckpt.js": "f079c6a71f769855d8ddc1c521c7408d3e73cd152f99872607e0b6918177173b",
252
259
  "src/host/clock.js": "aba6af3a8b1a985ae340892222e4b482d33afe20f5c8b3a4c7403ce87d1e247c",
253
- "src/host/ctl.js": "b2b288e9c7f1afa0f81ef7ba69080df5019b3967bc6f8e1a04ce76ba93d0ba04",
260
+ "src/host/csp-reason.js": "ecec797e31006896176ff4e2972f11e5d5df160fe0cd80071c39d7808ff922fc",
261
+ "src/host/ctl.js": "86066c8d3be8e6f6b05e831344cd72ebc446ced391fc8290e1c584940b8ab3e0",
254
262
  "src/host/disk-map.js": "3d7e0d7d488ce25fc6a0e4dc0a9b1c8fc2af96bac1c03491a1d30be392af268b",
255
263
  "src/host/disk-worker.js": "6e82bfeb4aa3f8b02bd7cb2de7427def26114e016f6b05196c8485d7fc8315ee",
256
264
  "src/host/disk.js": "e941e62cc2d9a83582d53c80c09c5f0f682508f77390237bedff4051c489b2cd",
257
- "src/host/edge-go.js": "5c325ef6876de0c75d6fca338fefa94da3d83b8a550077cf38b91e3365c37152",
265
+ "src/host/edge-go.js": "0067007ccbe3ee97352aa90048fd9245de531b1203334c5a51d5b6f1136c3dc7",
258
266
  "src/host/edge-host.js": "463db51d4ea96ce4540b47acccfe0f0eb702c504dbe79f9b3404641a9def6f63",
259
267
  "src/host/errno.js": "26021d85962084be2c43c98fb3accafc2a8f11fbf3c8a5b8d484bd6a088ae4db",
260
268
  "src/host/fs.js": "d6753f74db35b7b01baa8709fc7fdcdfc8d638dfe42f454b0a833b22111e8f4a",
261
269
  "src/host/gcnudge.js": "4a6569cb73ae24a11fc4ac8c467380bd9efba0bb5fa9867b797bc4f153ab15ab",
262
270
  "src/host/gowasm-errtext.js": "13457e6d183cc147feb2dcacfa8f6552e4e07d50fa491514447de082fd8eb874",
263
- "src/host/gowasm-guard.js": "094cd9e25714cfd470974c482b4a10dc546fb5b7d425109728a04ca1dfc71ce7",
264
- "src/host/gowasm-worker.js": "a04ed94fa15ef197d3dee2689df7afaa04d631c82854d1147fb1c6bd30d6b8fc",
271
+ "src/host/gowasm-guard.js": "c0ca9fa28bfd373b70d16b68331060ae080d3c0c13e4a2e15d874b9620826bf2",
272
+ "src/host/gowasm-worker.js": "f39c6f2c0f9587a47b5c038b5f3d8c2bef0375b692ee7c3a488553a101de6921",
265
273
  "src/host/i64shim.js": "6c7cbe226c1596acc2ef084b1c7f817b220d43a17fb927d288246257619d5301",
266
274
  "src/host/jit-cache.js": "c31311029de8ee20c0ba6f6b09042f8b91ab08e07134e5a6b1a4de3a1d492c58",
267
275
  "src/host/jit-hub-core.js": "f8d39dd0e4cbd42568bf56a8a7105f321d33bcf2d5faf8756d45a1ed692bd344",
@@ -271,6 +279,7 @@
271
279
  "src/host/layout.js": "d5bc5aa9292630b204438df5d6278fda6daa2a65acc061e1c57e0272b32fecc8",
272
280
  "src/host/monitor.js": "ae1741ba35668bcc6192642246494433b175668933921634c699af5a0a69acc0",
273
281
  "src/host/p9mount.js": "cd27abd769b19ed8b26050ddd8b6d61f81faa5a431924217dd0bf2593258d5b9",
282
+ "src/host/pinned-fetch.js": "873eb804604c150f6c8e488eb6c49fb8dc7a85108e80bf1a831904b9555726eb",
274
283
  "src/host/ram-mem.js": "bf4036d5d12be1d5d2f5223e526b0b8e225bc702ae7e60a2a59fdfe9fbbb49e3",
275
284
  "src/host/stdio.js": "43973675578eb1d324c2c6d7078e51a6a1f121190ada5ef14a5551364b8c7f5e",
276
285
  "src/host/threads.js": "952381940e7f43114867e9a9714528f2f20c8064b6b0f2018ab075128dc601be",
package/guest/arm64/Image CHANGED
@@ -1,4 +1,4 @@
1
1
  [diffend] Oversized file quarantined before diffing.
2
2
  name: package/guest/arm64/Image
3
3
  size: 39049224 bytes
4
- sha256: daa4dc90fe59ceda984ba97342f0abab0a45e3ba1a14d361d142234347f5a9e4
4
+ sha256: 8542df8ca5f5b1b543129281e70e9906220da0ef299d93af190df2f953237b97
@@ -1,4 +1,4 @@
1
1
  [diffend] Oversized file quarantined before diffing.
2
2
  name: package/guest/arm64/Image-4k
3
3
  size: 38828040 bytes
4
- sha256: 284de4fdb2b7a25f4ecabd03f03aaa56c1d22f99082f6c5c2db2fb0b7c5a6c36
4
+ sha256: 696a4ce9d2fdc9b5a945f49f5faeb59f744fbdea9d0d69216afc6d29677398f0
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@threadedstack/wmd-web",
3
- "version": "0.1.38",
3
+ "version": "0.1.39",
4
4
  "license": "MIT",
5
5
  "homepage": "https://github.com/threadedstack/wmd-releases",
6
6
  "repository": {
@@ -9,8 +9,8 @@
9
9
  },
10
10
  "description": "Browser host for wmd's guest vm.wasm (Web Workers + shared memory + WASI preview1)",
11
11
  "wmd": {
12
- "tag": "v0.1.38",
13
- "commit": "19c9d61297cf7d3c531935ba4e13610730eaf34a",
12
+ "tag": "v0.1.39",
13
+ "commit": "6b1f00376baee9fe58ee14b13752c505c8e75573",
14
14
  "web": "0.5.1"
15
15
  },
16
16
  "type": "module",
package/src/boot.js CHANGED
@@ -45,9 +45,10 @@ import {
45
45
  CONSOLE_IN_LIMIT, chanCounters, chanView, consoleInputGate, createChanBlock, markGuestGone, markServiceDead, ringBell, ringRead,
46
46
  } from './host/chan.js'
47
47
  import { ControlClient } from './host/ctl.js'
48
+ import { chooseTwin, eventsOf, putBackTwin, specKeysDiffering, twinForBoot } from './gowasm/boot-setup.js'
48
49
  import { PAGE as CKPT_PAGE, requestCheckpoint } from './host/ckpt-point.js'
49
50
  import {
50
- CheckpointError, serviceErrorCode, assembleCheckpoint, bootKeyForLayout, bootKeyOf, checkCheckpointFits, checkPacksFit, checkVmKeyFits, packDigestsOf, fdTableBytes, parseCheckpoint, restoreFdTable, resumePorts, sha256Hex, validateFdTable,
51
+ CheckpointError, serviceErrorCode, assembleCheckpoint, bootKeyForChoice, bootKeyOf, checkCheckpointFits, checkPacksFit, checkVmKeyFits, packDigestsOf, fdTableBytes, parseCheckpoint, restoreFdTable, resumePorts, sha256Hex, validateFdTable,
51
52
  } from './host/ckpt.js'
52
53
  import { captureJit, jitSnapStats, modulesOf, ownersNeedingHub, restoreStore } from './host/jit-snap.js'
53
54
  export { CheckpointError, memoryStore, opfsStore } from './host/ckpt.js'
@@ -700,6 +701,19 @@ export async function bootWmd(o) {
700
701
  const t0 = absMs()
701
702
  const timings = { t0 }
702
703
  const ev = e => o.onEvent?.(e)
704
+ // X6 part 3: the Go twin's setup (src/gowasm/boot-setup.js): stub mounts in the bundle's spec and the service's gowasm field, from the pins; nothing for a
705
+ // caller that passed `gowasm: false`, named no source and has no default, or whose service has its own gowasm field
706
+ // Part 4: a boot that restores takes the twin choice its checkpoint was taken with (chooseTwin): here for a checkpoint in hand (o.restore), when the
707
+ // base snapshot's header is known for o.restoreLazy (below, with the layout), in vm.restore() before the running machine is stopped. A checkpoint taken
708
+ // with the twin that this boot has off (gowasm: false, no source, a setup that is not usable) restores (part 6): its spec text stays (the guest has the stub mounts),
709
+ // the service's gowasm field says `off` so the page starts no twin Worker, the channel service builds a GoEdge that is off from the start (every stub is answered
710
+ // with the fallback, the guest compiles), and one event {t: 'gowasm-off', rule: 'checkpoint-twin'} says so.
711
+ const twin = await twinForBoot(o)
712
+ const twinFirst = await chooseTwin(twin, o.restore?.header || null)
713
+ o = twinFirst.o
714
+ // the twin's events are said once: now, or, for a base snapshot (o.restoreLazy), when its header is known (its choice may supersede the boot's own reason)
715
+ let twinSaid = !o.restoreLazy
716
+ if (twinSaid) for (const e of eventsOf(twin, twinFirst)) ev(e)
703
717
  // W24: the start-up events (host/boot-events.js); be.close() at the end
704
718
  const be = createBootEvents({ emit: ev, t0, now: absMs })
705
719
  // the emulator's last stderr text, for the error a wait on a dead machine gives
@@ -856,7 +870,7 @@ export async function bootWmd(o) {
856
870
  })
857
871
  filesP.catch(() => {}) // awaited below; must not go unhandled if vm.wasm fails first
858
872
  // P-B3: the channel services' packs (fetched in parallel with the rest); pack:<n> numbers them over all services
859
- const chanSvcs = o.channels?.services || []
873
+ let chanSvcs = o.channels?.services || []
860
874
  let packN = 0
861
875
  const packIds = chanSvcs.map(svc => (svc.layers || []).map(() => packN++))
862
876
  const layersP = A
@@ -868,7 +882,9 @@ export async function bootWmd(o) {
868
882
  ).then(downloaded)
869
883
  layersP.catch(() => {})
870
884
  let module, files, chanLayers, tableMin, vmBytes
871
- function baseRefused(err) {
885
+ let twinByBase = false // the base snapshot's header changed the twin choice (a header refused after that puts the options' choice back)
886
+ let twinChosen = null // chooseTwin's answer for the base snapshot's header: its events are said when the snapshot is accepted (or the options' own, when it is refused)
887
+ async function baseRefused(err) {
872
888
  const lazy = lazyIn || baseLazy
873
889
  lazyIn = null
874
890
  baseLazy = null
@@ -876,6 +892,21 @@ export async function bootWmd(o) {
876
892
  lazy?.abort?.(err)
877
893
  } catch {}
878
894
  if (!o.restoreFallback) throw err
895
+ // fix 10 (F1): the machine boots cold, so it takes the options' own twin choice (spec text, gowasm field, boot key) whatever refused the snapshot, the header or
896
+ // the checks of its body: this is the one place
897
+ if (twinByBase) {
898
+ const back = await putBackTwin(twin, o, bootDesc, { flat: flatRam, maxPages: maximum })
899
+ o = back.o
900
+ chanSvcs = o.channels?.services || []
901
+ twinByBase = false
902
+ if (back.key) {
903
+ bootKey = back.key.key
904
+ bootDesc = back.key.desc
905
+ if (heldAssets) heldAssets.keys = { ...heldAssets.keys, bootKey, bootDesc }
906
+ }
907
+ }
908
+ if (!twinSaid) for (const e of twin.events) ev(e)
909
+ twinSaid = true
879
910
  timings.baseFallback = { code: err && err.code ? err.code : 'error', message: err && err.message ? err.message : String(err) }
880
911
  // W24: the restore stage ends here, unused; the kernel stage follows
881
912
  be.now('restore', { t: 'boot', stage: 'restore', state: 'done', used: false, fallback: timings.baseFallback.code, bytes: baseBytes, total: baseTotal, source: baseSource })
@@ -890,12 +921,23 @@ export async function bootWmd(o) {
890
921
  const h = await raceAbort(lazyIn.header)
891
922
  checkVmKeyFits(h, vmKey)
892
923
  settleLayout(chooseLayout(o.args, o, h)) // X24 default: the base snapshot's own layout
924
+ // part 4: and its own twin choice. The spec text is first used by the boot key and by the channel service's Worker, both below this point in this same
925
+ // try, so the choice made here is made before either has it; only the layers and ports of the services were used before (their packs download meanwhile)
926
+ const tw = await chooseTwin(twin, h)
927
+ if (tw.o !== o) {
928
+ o = { ...o, channels: tw.o.channels }
929
+ chanSvcs = o.channels?.services || []
930
+ twinByBase = true
931
+ }
932
+ twinChosen = tw
893
933
  lazyIn.proceed?.() // the reader decodes only from here: a refused header costs what was queued, not the file
894
934
  } catch (err) {
895
935
  if (signal?.aborted) throw err
896
- baseRefused(err)
936
+ await baseRefused(err) // (the machine that boots cold takes the options' own twin choice back there, whatever refused the snapshot)
897
937
  }
898
938
  }
939
+ if (!twinSaid && !lazyIn) for (const e of twin.events) ev(e) // no base snapshot after all
940
+ if (!lazyIn) twinSaid = true
899
941
  if (!layoutSaid) settleLayout(layout0) // no base snapshot, or its header was refused: the options' layout
900
942
  const tc = absMs()
901
943
  be.now('compile', { t: 'boot', stage: 'compile', state: 'start', moduleBytes: vmBytes.byteLength, reused: !!A })
@@ -919,12 +961,15 @@ export async function bootWmd(o) {
919
961
  if (A) {
920
962
  packs = A.keys.packs
921
963
  const ad = A.keys.bootDesc
922
- if (!ad || (ad.maxPages === maximum && Array.isArray(ad.env) && ad.env.includes('WMD_RAM_FLAT=1') === flatRam)) {
964
+ // part 4: the spec texts of this boot (the twin choice the checkpoint made) against the ones the assets' machine had
965
+ const cfgKeys = ad ? await specKeysDiffering(ad, chanSvcs) : {}
966
+ if (!ad || (ad.maxPages === maximum && Array.isArray(ad.env) && ad.env.includes('WMD_RAM_FLAT=1') === flatRam && !Object.keys(cfgKeys).length)) {
923
967
  bootKey = A.keys.bootKey
924
968
  bootDesc = A.keys.bootDesc
925
969
  } else {
926
970
  // X24 default: the checkpoint chose another RAM layout than the machine these assets came from had: the same key, with that layout
927
- const k = await bootKeyForLayout(ad, { flat: flatRam, maxPages: maximum })
971
+ // (part 4: and the same with the other spec text when the checkpoint chose another twin state)
972
+ const k = await bootKeyForChoice(ad, { flat: flatRam, maxPages: maximum }, cfgKeys)
928
973
  bootKey = k.key
929
974
  bootDesc = k.desc
930
975
  }
@@ -964,10 +1009,12 @@ export async function bootWmd(o) {
964
1009
  checkCheckpointFits(parsed.header, { vmKey, bootKey, bootDesc, maxPages: maximum, ramPages, hasJit: o.jit !== false, fmaFused: fmaFuses() })
965
1010
  checkPacksFit(parsed.header.packs, packDigests)
966
1011
  restoreIn = parsed
1012
+ if (!twinSaid) for (const e of twinChosen ? eventsOf(twin, twinChosen) : twin.events) ev(e) // the snapshot is accepted: the checkpoint's twin choice stands
1013
+ twinSaid = true
967
1014
  lazyIn = null // baseLazy stays: accept() when the restore has succeeded, reject() if it fails
968
1015
  } catch (err) {
969
1016
  if (signal?.aborted) throw err
970
- baseRefused(err)
1017
+ await baseRefused(err)
971
1018
  }
972
1019
  }
973
1020
  if (restoreIn) checkCheckpointFits(restoreIn.header, { vmKey, bootKey, bootDesc, maxPages: maximum, ramPages, hasJit: o.jit !== false, fmaFused: fmaFuses() })
@@ -1137,6 +1184,7 @@ export async function bootWmd(o) {
1137
1184
  // P-B10: the network Worker, when a net service has a relay
1138
1185
  let netWorker = null
1139
1186
  let goWorker = null // X6 (default off): the Wasm twin of the guest's Go compiler
1187
+ let goCspOff = null // takes the document's policy-violation listener of the live twin Worker off again
1140
1188
  let netStats = null
1141
1189
  let netMember = null // W56: this guest on an in-page switch (switch.js attach())
1142
1190
  const netWaiters = new Map()
@@ -1248,6 +1296,7 @@ export async function bootWmd(o) {
1248
1296
  else if (m.t === 'gowasm-stop') {
1249
1297
  goWorker?.terminate()
1250
1298
  goWorker = null
1299
+ goCspOff?.()
1251
1300
  }
1252
1301
  else if (m.t === 'gowasm-restart') {
1253
1302
  // the twin was in the middle of a compile that was cancelled: a Worker cannot be stopped inside one, so it ends and a new one starts
@@ -1324,10 +1373,36 @@ export async function bootWmd(o) {
1324
1373
  const mc = new MessageChannel()
1325
1374
  const w = (goWorker = new Worker(new URL('./host/gowasm-worker.js', import.meta.url), { type: 'module', name: 'wmd-gowasm' }))
1326
1375
  w.onerror = err => chanSvc?.postMessage({ t: 'gowasm-died', why: `the twin Worker failed: ${err.message || err}` })
1327
- w.postMessage({ t: 'init', port: mc.port2, wasmUrl: goSvc.gowasm.wasmUrl, wasmSha256: goSvc.gowasm.wasmSha256, modules: goSvc.gowasm.modules, preload: goSvc.gowasm.preload, cacheBytes: goSvc.gowasm.cacheBytes }, [mc.port2])
1376
+ // fix 10 (F4): a policy violation the document sees is told to the Worker too (the Worker listens for its own); its reason text then says the policy blocked the source
1377
+ if (typeof document !== 'undefined') {
1378
+ goCspOff?.() // one listener, for the live Worker
1379
+ const onViolation = e => {
1380
+ if (goWorker !== w) return // that Worker is gone
1381
+ try {
1382
+ w.postMessage({ t: 'csp', violation: { blockedURI: String(e.blockedURI), effectiveDirective: String(e.effectiveDirective || ''), disposition: String(e.disposition || 'enforce') } })
1383
+ } catch {}
1384
+ }
1385
+ document.addEventListener('securitypolicyviolation', onViolation)
1386
+ goCspOff = () => {
1387
+ document.removeEventListener('securitypolicyviolation', onViolation)
1388
+ goCspOff = null
1389
+ }
1390
+ }
1391
+ w.postMessage({ t: 'init', port: mc.port2, wasmUrl: goSvc.gowasm.wasmUrl, wasmSha256: goSvc.gowasm.wasmSha256, modules: goSvc.gowasm.modules, preload: goSvc.gowasm.preload, sources: goSvc.gowasm.sources, cacheBytes: goSvc.gowasm.cacheBytes }, [mc.port2])
1328
1392
  return mc.port1
1329
1393
  }
1330
- const gowasmPort = goSvc ? spawnGoTwin() : null
1394
+ // X6 part 3: a twin Worker that cannot start (a page whose CSP forbids it, no Worker support) must not stop the machine: the channel service takes the
1395
+ // stub mounts out of the bundle (no twin Worker) and one event says why
1396
+ let gowasmPort = null
1397
+ let gowasmWhy = ''
1398
+ // part 6: a service whose gowasm field says `off` (a checkpoint taken with the twin, restored in a boot that has the twin off) gets no Worker
1399
+ if (goSvc && !goSvc.gowasm.off) {
1400
+ try {
1401
+ gowasmPort = spawnGoTwin()
1402
+ } catch (err) {
1403
+ gowasmWhy = `the twin Worker could not start: ${(err && err.message) || err}`
1404
+ }
1405
+ }
1331
1406
  chanSvc.postMessage({
1332
1407
  t: 'init',
1333
1408
  sab: chanSab,
@@ -1348,6 +1423,7 @@ export async function bootWmd(o) {
1348
1423
  netPort,
1349
1424
  edge: o.edge ? { port9p: o.edge.port, root: o.edge.root || '', rt: o.edge.rt || '' } : undefined, // X5 (edge=1 only)
1350
1425
  gowasmPort,
1426
+ gowasmWhy,
1351
1427
  }, [...(netPort ? [netPort] : []), ...(gowasmPort ? [gowasmPort] : [])])
1352
1428
  consoleGate = consoleInputGate(chanSab, o.channels.consoleInputBytes ?? CONSOLE_IN_LIMIT)
1353
1429
  }
@@ -1564,6 +1640,7 @@ export async function bootWmd(o) {
1564
1640
  failUpperWaiters('the machine has ended (its files ended with it)')
1565
1641
  netWorker?.terminate()
1566
1642
  goWorker?.terminate()
1643
+ goCspOff?.()
1567
1644
  netMember?.detach() // W56: its peers' streams to it fail; the name keeps its address
1568
1645
  if (netWorker) timings.net = netStats
1569
1646
  if (chanSab) timings.chan = { ...chanCounters(chanSab), service: chanStats, died: chanDied }
@@ -1632,6 +1709,7 @@ export async function bootWmd(o) {
1632
1709
  netWorker = null
1633
1710
  if (goWorker) goWorker.onerror = null
1634
1711
  goWorker = null
1712
+ goCspOff?.()
1635
1713
  netMember?.detach()
1636
1714
  module = files = null
1637
1715
  }
@@ -2135,8 +2213,12 @@ export async function bootWmd(o) {
2135
2213
  // X24 default (#70): the checkpoint decides the next machine's RAM layout (bootWmd does the same from the same header), so the check is
2136
2214
  // made against that layout's key, memory maximum and RAM memory, not this machine's
2137
2215
  const L = chooseLayout(o.args, { ...o, ...over }, parsed.header)
2216
+ // part 4: and its twin choice (the next machine's own, from the options as the caller gave them): a checkpoint taken with the twin restores whatever this boot's twin state (chooseTwin keeps its spec text and says the twin is off), the usual fit check follows, before this machine stops
2217
+ const nextOpts = { ...o, channels: twin.plain.channels, ...over }
2218
+ const tw = await chooseTwin(await twinForBoot(nextOpts), parsed.header)
2219
+ const cfgKeys = bootDesc ? await specKeysDiffering(bootDesc, tw.o.channels?.services) : {}
2138
2220
  let target = { key: bootKey, desc: bootDesc }
2139
- if (bootDesc && (L.flat !== flatRam || L.maximum !== maximum)) target = await bootKeyForLayout(bootDesc, { flat: L.flat, maxPages: L.maximum })
2221
+ if (bootDesc && (L.flat !== flatRam || L.maximum !== maximum || Object.keys(cfgKeys).length)) target = await bootKeyForChoice(bootDesc, { flat: L.flat, maxPages: L.maximum }, cfgKeys)
2140
2222
  checkCheckpointFits(parsed.header, { vmKey, bootKey: target.key, bootDesc: target.desc, maxPages: L.maximum, ramPages: L.ramPages, hasJit: o.jit !== false, fmaFused: fmaFuses() })
2141
2223
  if (!heldAssets) throw new CheckpointError('not-ready', 'this machine was booted without checkpoint support: it has no kept boot files to restore with')
2142
2224
  validateFdTable(parsed.ctl, heldAssets.files.length) // the fd table is checked against this boot's files now, not after the old machine is gone
@@ -2156,7 +2238,7 @@ export async function bootWmd(o) {
2156
2238
  memory = null
2157
2239
  oldHub = keptHub
2158
2240
  keptHub = null
2159
- const next = await bootWmd({ ...o, ...over, checkpoint: true, restore: ckpt, assets, _adopt: oldHub, _net: { instance: netInstance, names: [...netNames] } })
2241
+ const next = await bootWmd({ ...nextOpts, checkpoint: true, restore: ckpt, assets, _adopt: oldHub, _net: { instance: netInstance, names: [...netNames] } })
2160
2242
  heldAssets = null // the new machine holds them
2161
2243
  return next
2162
2244
  } finally {
@@ -0,0 +1,175 @@
1
+ // X6 default part 3, item 1: bootWmd does the twin's whole setup alone. Called first thing by bootWmd (src/boot.js) with its options; for each 9P
2
+ // service whose files hold bundle/config/config.json (what containerBundle returns, src/host/ctl.js) it adds the stub mounts to the spec text and sets
3
+ // the service's `gowasm` field with the mount destinations read from that spec. The consumer writes no twin line.
4
+ //
5
+ // What a consumer MAY pass, on bootWmd only:
6
+ // gowasm: false the twin is off; nothing is changed, nothing is said
7
+ // gowasm: { sources: [...] } the ordered sources of the twin's module (default GOWASM_SOURCES_DEFAULT; src/host/pinned-fetch.js)
8
+ // gowasm: { base: 'https://.../gowasm' } one file source: <base>/compile-<go release>.wasm (an app that serves its own copy)
9
+ // gowasm: { limits: {...} } beatLimitMs, readyLimitMs, runLimitMs, earlyReply, lateFallbackAt (tests)
10
+ // gowasm: { pins, packages } the pins and the package names (tests)
11
+ // gowasm: true as undefined, but a missing source list is said (one event) instead of silently off
12
+ // A service that already has its own `gowasm` field (a caller that calls gowasmSetup itself, the test page) is left as it is; a spec that already has
13
+ // the mounts (a caller that still passes `edge` to containerBundle) gets nothing added: adding twice changes nothing.
14
+ // The image decides: the channel service's Worker takes the stub mounts out of the spec for an image with no Go compile (GoEdge.pruneBundle) or when the
15
+ // twin's code cannot be loaded (noTwin), before the guest reads the spec; an image with a Go release that has no pin keeps the stubs, and its compiles go
16
+ // to the guest with one calm event (GoEdge, rule 'unpinned').
17
+ // A checkpoint: the service files are part of the boot key, so the spec text (with or without the twin's stub mounts) says which choice a checkpoint was taken with.
18
+ // A boot that restores takes the choice its checkpoint was taken with (chooseTwin, below), the way X24's RAM layout does (chooseLayout): a checkpoint taken
19
+ // without the twin restores into a boot that would have the twin on, and runs without it (one event, rule 'checkpoint'); a checkpoint taken with the twin keeps it,
20
+ // and one taken with the twin ALWAYS restores, also in a boot that has the twin off (gowasm: false, no source, a setup that is not usable): it keeps its own spec
21
+ // text (the guest has the stub mounts), the service's gowasm field says `off` (no Worker, nothing fetched), every compile runs in the guest, one event, rule
22
+ // 'checkpoint-twin'. A header with no boot description, or whose spec text is neither, changes nothing.
23
+ import { addEdgeToolMounts } from '../host/ctl.js'
24
+ import { bootKeyForChoice, sampleKey, specChoice } from '../host/ckpt.js'
25
+ import { GOWASM_SOURCES_DEFAULT, planOf } from './setup.js'
26
+
27
+ export { GOWASM_SOURCES_DEFAULT }
28
+ const CONFIG = 'bundle/config/config.json'
29
+ // the spec text does not depend on the sources, so a boot that does not use the twin still makes the text a checkpoint with the twin would hold
30
+ const TEXT_ONLY = [{ kind: 'file', base: 'https://twin.invalid/' }]
31
+ const textOf = raw => {
32
+ const d = raw && typeof raw === 'object' && !(raw instanceof Uint8Array) && 'data' in raw ? raw.data : raw
33
+ return typeof d === 'string' || d instanceof Uint8Array ? d : new Uint8Array(d)
34
+ }
35
+
36
+ /**
37
+ * @param {object} o bootWmd's options
38
+ * @returns {Promise<{o: object, events: object[], plain: object, twinOn: boolean, texts: {i: number, port: number, plain: string|Uint8Array, twin: string}[], off: string}>}
39
+ * o is the same object when nothing changes; events are {t: 'gowasm-off', why} (at most one). For chooseTwin: `plain` is the options as given, `twinOn` says
40
+ * whether `o` has the twin, `texts` holds, per 9P service the twin could change, its spec text without and with the twin (even when the twin is off for
41
+ * this boot: a checkpoint taken with it is told apart), and `off` says why it is off (empty when on).
42
+ */
43
+ export async function twinForBoot(o) {
44
+ const events = []
45
+ const res = (o2, extra = {}) => ({ o: o2, events, plain: o, twinOn: false, texts: [], off: '', offO: null, ...extra })
46
+ try {
47
+ const opt = o && o.gowasm
48
+ const given = opt && typeof opt === 'object' ? opt : {}
49
+ const sources = given.sources ?? (given.base ? [{ kind: 'file', base: given.base }] : GOWASM_SOURCES_DEFAULT)
50
+ const services = o && o.channels && Array.isArray(o.channels.services) ? o.channels.services : []
51
+ const mine = services.map((s, i) => [s, i]).filter(([s]) => s && (s.kind === '9p' || s.kind === 1) && s.files && typeof s.files === 'object' && !s.gowasm && s.files[CONFIG] !== undefined)
52
+ if (!mine.length) return res(o)
53
+ const consumerOff = opt === false || !sources
54
+ const plan = planOf({ sources: consumerOff ? TEXT_ONLY : sources, pins: given.pins, packages: given.packages, limits: given.limits || {} })
55
+ const consumerWhy = opt === false ? 'gowasm: false' : 'no source'
56
+ // the text of the spec with the stub mounts is decided by the tools alone, not by the sources or the limits: when this boot does not use the twin (the consumer's
57
+ // choice, or a setup that is not usable) it is made from the caller's pins and, when those are not usable, from the package's own, so a checkpoint taken with
58
+ // the twin can still be told apart and restored (part 6)
59
+ let textPlan = plan
60
+ if (plan.why) {
61
+ textPlan = planOf({ sources: TEXT_ONLY, pins: given.pins, packages: given.packages, limits: {} })
62
+ if (textPlan.why) textPlan = planOf({ sources: TEXT_ONLY, limits: {} })
63
+ }
64
+ /** The services with the twin's spec text (and a gowasm field made by `p`), the texts to tell, or null when a spec is not JSON. */
65
+ const build = (p, fieldOf) => {
66
+ const next = services.slice()
67
+ const texts = []
68
+ for (const [svc, i] of mine) {
69
+ const raw = svc.files[CONFIG]
70
+ let spec
71
+ try {
72
+ spec = JSON.parse(typeof raw === 'string' ? raw : new TextDecoder().decode(raw && typeof raw === 'object' && 'data' in raw ? raw.data : raw))
73
+ } catch {
74
+ return { bad: "the bundle's config is not JSON" }
75
+ }
76
+ if (!spec || !Array.isArray(spec.mounts)) return { bad: "the bundle's config has no mounts" }
77
+ spec.mounts = addEdgeToolMounts(spec.mounts, p.edge.tools)
78
+ const text = JSON.stringify(spec, null, 2) + '\n'
79
+ const given0 = textOf(raw)
80
+ if (!(typeof given0 === 'string' && given0 === text)) texts.push({ i, port: svc.port, plain: given0, twin: text })
81
+ next[i] = { ...svc, files: { ...svc.files, [CONFIG]: typeof raw === 'string' && raw === text ? raw : text }, gowasm: fieldOf(p.service(spec.mounts.map(m => m.destination))) }
82
+ }
83
+ return { next, texts }
84
+ }
85
+ /** What a restore of a checkpoint taken with the twin gets when the twin is off for this boot: the twin's text (the stubs stay), a gowasm field that says off. */
86
+ const offOf = (why, built) => (built.next ? { ...o, channels: { ...o.channels, services: built.next.map((sv, i) => (sv !== services[i] ? { ...sv, gowasm: { ...sv.gowasm, modules: [], preload: [], sources: undefined, off: why } } : sv)) } } : null)
87
+ const goOff = (why, event) => {
88
+ if (event) events.push({ t: 'gowasm-off', why })
89
+ const built = textPlan.why ? { bad: textPlan.why } : build(textPlan, f => f)
90
+ return built.next ? res(o, { texts: built.texts, off: why, offO: offOf(why, built) }) : res(o, { off: why })
91
+ }
92
+ if (plan.why) return consumerOff ? goOff(consumerWhy, false) : goOff(`the twin's setup is not usable: ${plan.why}`, true)
93
+ const built = build(plan, f => f)
94
+ if (built.bad) return consumerOff ? goOff(consumerWhy, false) : (events.push({ t: 'gowasm-off', why: built.bad }), res(o, { off: built.bad }))
95
+ if (consumerOff) {
96
+ if (opt === undefined) return res(o, { texts: built.texts, off: 'no source', offO: offOf('no source', built) })
97
+ if (opt === false) return res(o, { texts: built.texts, off: 'gowasm: false', offO: offOf('gowasm: false', built) })
98
+ events.push({ t: 'gowasm-off', why: 'no source of the twin is named: no sources and no default, so none is asked for' })
99
+ return res(o, { texts: built.texts, off: 'no source', offO: offOf('no source', built) })
100
+ }
101
+ return res({ ...o, channels: { ...o.channels, services: built.next } }, { twinOn: true, texts: built.texts })
102
+ } catch (e) {
103
+ return { o, events: [{ t: 'gowasm-off', why: `the twin's setup failed: ${(e && e.message) || e}` }], plain: o, twinOn: false, texts: [], off: 'failed', offO: null }
104
+ }
105
+ }
106
+
107
+ /**
108
+ * X6 default part 4: a restore takes the twin choice its checkpoint was taken with. `v` is twinForBoot's result and `header` the checkpoint's (or a base
109
+ * snapshot's) header; the only thing read of it is which of the two spec texts the first 9P service's recorded key matches (specChoice). Returns
110
+ * {o, events}: `o` is v.o unless the checkpoint was taken without the twin and this boot would have it (then the options as given, no stub mount, no
111
+
112
+ * gowasm field, and one event {t: 'gowasm-off', rule: 'checkpoint', why}). A checkpoint that has the twin in a boot that has it off (gowasm: false, no source,
113
+ * an unusable setup) restores with its own spec text and a gowasm field that says `off` (part 6, see below). A header with no description, a text that
114
+ * is neither, or services that disagree change nothing: the usual check of the restore decides, as it did before. X5 would call the same two functions
115
+ * with its own mounts' texts (specChoice names the text; its setup function returns what twinForBoot returns).
116
+ */
117
+ export async function chooseTwin(v, header) {
118
+ const none = { o: v.o, events: [] }
119
+ if (!header || typeof header !== 'object' || !v.texts || !v.texts.length) return none
120
+ const rec = []
121
+ for (const t of v.texts) rec.push(await specChoice(header, t.i, t.port, CONFIG, { plain: t.plain, twin: t.twin }))
122
+ if (rec[0] === null || rec.some(r => r !== rec[0])) return none
123
+ if (rec[0] === 'plain') {
124
+ if (!v.twinOn) return none
125
+ return { o: v.plain, events: [{ t: 'gowasm-off', rule: 'checkpoint', why: 'the checkpoint was taken without the Go twin, so this machine runs without it (its compiles run in the guest)' }] }
126
+ }
127
+ if (v.twinOn) return none
128
+ // Part 6: a checkpoint taken with the twin always restores, whatever this boot's twin state. The twin is off for this boot (gowasm: false, no source, a setup
129
+ // that is not usable): the machine keeps the checkpoint's spec text (its guest has the stub mounts), no twin Worker is started, nothing of the twin is fetched,
130
+ // every compile runs in the guest (GoEdge started off answers each stub with the fallback), and one calm event says so. A spec that is not JSON has no
131
+ // twin text to make: the usual check of the restore decides and says what differs.
132
+ if (!v.offO) return none
133
+ const why = `the checkpoint was taken with the Go twin and the twin is off in this boot (${v.off || 'off'}): the machine keeps the twin's stub mounts and runs its compiles in the guest`
134
+ return { o: v.offO, events: [{ t: 'gowasm-off', rule: 'checkpoint-twin', why }], supersedes: true }
135
+ }
136
+
137
+ /**
138
+ * The events a boot says for its twin choice, once each: twinForBoot's own and chooseTwin's. When the checkpoint's choice supersedes (a checkpoint taken with the
139
+ * twin, a boot that has it off), the one event of the checkpoint's rule says it, with the reason, and the boot's own reason (an unusable setup) is in that text.
140
+ */
141
+ export function eventsOf(v, c) {
142
+ return c.supersedes ? c.events : [...v.events, ...c.events]
143
+ }
144
+
145
+ /**
146
+ * The sample keys of the spec text of each 9P service that has one that differs from what the boot description `desc` records ({serviceIndex: key}; empty
147
+ * when they all agree or there is no description): the services' texts after the twin choice against a boot key made before it (held assets, vm.restore()).
148
+ */
149
+ export async function specKeysDiffering(desc, services) {
150
+ const out = {}
151
+ if (!desc || !Array.isArray(desc.services) || !Array.isArray(services)) return out
152
+ for (let i = 0; i < services.length; i++) {
153
+ const raw = services[i] && services[i].files ? services[i].files[CONFIG] : undefined
154
+ if (raw === undefined) continue
155
+ const d = textOf(raw)
156
+ const key = await sampleKey(typeof d === 'string' ? new TextEncoder().encode(d) : d)
157
+ if (desc.services[i] && desc.services[i].bundle && desc.services[i].bundle[CONFIG] !== key) out[i] = key
158
+ }
159
+ return out
160
+ }
161
+
162
+ /**
163
+ * X6 fix 10 (F1): a base snapshot that is refused after the twin choice was made from its header (the late checks: fit, packs, a body that cannot be read) and
164
+ * whose boot falls back to a cold start takes the options' own twin choice back: `o` with the channels of `twin.o` (no checkpoint stub text, no `gowasm.off`),
165
+ * and, when a boot key was already made with the checkpoint's text (`bootDesc`), the key and description a cold boot of those options makes. One function for
166
+ * every refusal (boot.js baseRefused), so a refusal path added later cannot forget it. `layout` is {flat, maxPages} of the machine as it boots.
167
+ * @returns {Promise<{o: object, key: {key: string, desc: object}|null}>}
168
+ */
169
+ export async function putBackTwin(twin, o, bootDesc, layout) {
170
+ const back = { ...o, channels: twin.o.channels }
171
+ if (!bootDesc) return { o: back, key: null }
172
+ const keys = await specKeysDiffering(bootDesc, back.channels?.services)
173
+ if (!Object.keys(keys).length) return { o: back, key: null }
174
+ return { o: back, key: await bootKeyForChoice(bootDesc, layout, keys) }
175
+ }