@orangecharts/wrun-chart-host 0.5.0 → 0.7.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 +17 -8
- package/dist/constants/feature-flags.constants.js +7 -2
- package/dist/constants/marketCapabilities.constants.d.ts +5 -0
- package/dist/constants/marketCapabilities.constants.js +16 -0
- package/dist/host-env.d.ts +3 -19
- package/dist/host-ports.types.d.ts +36 -17
- package/dist/index.d.ts +12 -1
- package/dist/index.js +12 -1
- package/dist/indicators/controls/tpo-session.util.js +42 -18
- package/dist/interfaces/wrun-runtime.interface.d.ts +88 -13
- package/dist/interfaces/wrun-runtime.interface.js +17 -3
- package/dist/market-data/billing.interface.d.ts +2 -1
- package/dist/settings-templates/indicator-fields.js +10 -0
- package/dist/utility/chart/chart-mount-epoch.util.d.ts +12 -0
- package/dist/utility/chart/chart-mount-epoch.util.js +32 -0
- package/dist/utility/chart/crypto-index.leaf.d.ts +16 -0
- package/dist/utility/chart/crypto-index.leaf.js +19 -0
- package/dist/utility/chart/index-mode-admission.util.js +9 -1
- package/dist/utility/chart/index-mode.util.d.ts +11 -5
- package/dist/utility/chart/index-mode.util.js +22 -10
- package/dist/utility/chart/kscript-source-answer-budget.leaf.d.ts +36 -0
- package/dist/utility/chart/kscript-source-answer-budget.leaf.js +39 -0
- package/dist/utility/chart/kscript-source-degrade.leaf.d.ts +38 -1
- package/dist/utility/chart/kscript-source-degrade.leaf.js +39 -1
- package/dist/utility/chart/live-bucket.util.d.ts +2 -1
- package/dist/utility/chart/live-bucket.util.js +47 -15
- package/dist/utility/chart/local-kscript-warmup-depth.util.d.ts +15 -1
- package/dist/utility/chart/local-kscript-warmup-depth.util.js +9 -0
- package/dist/utility/chart/market-session-schedule.util.d.ts +3 -1
- package/dist/utility/chart/market-session-schedule.util.js +24 -2
- package/dist/utility/chart/marketKind.util.d.ts +12 -7
- package/dist/utility/chart/marketKind.util.js +15 -5
- package/dist/utility/chart/ny-calendar.util.d.ts +104 -0
- package/dist/utility/chart/ny-calendar.util.js +346 -0
- package/dist/utility/chart/om-panel.util.d.ts +14 -2
- package/dist/utility/chart/om-panel.util.js +108 -40
- package/dist/utility/chart/rth-session.util.d.ts +10 -7
- package/dist/utility/chart/rth-session.util.js +17 -22
- package/dist/utility/chart/trading-day.util.js +90 -18
- package/dist/utility/chart/us-equities-session.util.js +20 -67
- package/dist/utility/chart/wrun-bar-session-facts.util.js +14 -1
- package/dist/utility/common/time.util.d.ts +7 -1
- package/dist/utility/common/time.util.js +11 -41
- package/dist/utility/number-format.util.d.ts +17 -7
- package/dist/utility/number-format.util.js +18 -8
- package/dist/utility/options/option-symbol.util.js +13 -2
- package/dist/utility/symbol-selection/contract-expiry.util.js +9 -1
- package/dist/utility/watchlist/import/import-request.leaf.d.ts +4 -1
- package/dist/utility/watchlist/import/import-request.leaf.js +5 -2
- package/dist/utility/watchlist/import/tradingview-symbols.leaf.d.ts +25 -4
- package/dist/utility/watchlist/import/tradingview-symbols.leaf.js +62 -4
- package/dist/utility/wrun/run-schedule/wrun-chart-gate-reader.util.d.ts +19 -0
- package/dist/utility/wrun/run-schedule/wrun-chart-gate-reader.util.js +57 -0
- package/dist/utility/wrun/run-schedule/wrun-gate-reader-warm.util.d.ts +23 -0
- package/dist/utility/wrun/run-schedule/wrun-gate-reader-warm.util.js +90 -0
- package/dist/utility/wrun/run-schedule/wrun-lane-groups.util.d.ts +39 -0
- package/dist/utility/wrun/run-schedule/wrun-lane-groups.util.js +60 -0
- package/dist/utility/wrun/run-schedule/wrun-late-reads.util.d.ts +77 -0
- package/dist/utility/wrun/run-schedule/wrun-late-reads.util.js +195 -0
- package/dist/utility/wrun/run-schedule/wrun-own-candles.util.d.ts +56 -0
- package/dist/utility/wrun/run-schedule/wrun-own-candles.util.js +70 -0
- package/dist/utility/wrun/run-schedule/wrun-prefix-feeds.util.d.ts +53 -0
- package/dist/utility/wrun/run-schedule/wrun-prefix-feeds.util.js +239 -0
- package/dist/utility/wrun/sources/lanes/option_stats_10m.d.ts +16 -0
- package/dist/utility/wrun/sources/lanes/option_stats_10m.js +14 -0
- package/dist/utility/wrun/sources/lanes/options_flow.d.ts +43 -18
- package/dist/utility/wrun/sources/lanes/options_flow.js +209 -72
- package/dist/utility/wrun/sources/lanes/token_supply.js +5 -31
- package/dist/utility/wrun/sources/opra-option-lanes.util.d.ts +3 -18
- package/dist/utility/wrun/sources/opra-option-lanes.util.js +12 -57
- package/dist/utility/wrun/sources/row-lane-registry.d.ts +1 -1
- package/dist/utility/wrun/sources/row-lane-registry.js +22 -8
- package/dist/utility/wrun/sources/row-lanes/option_contract_flow.d.ts +61 -0
- package/dist/utility/wrun/sources/row-lanes/option_contract_flow.js +103 -8
- package/dist/utility/wrun/sources/row-lanes/option_contract_state.d.ts +5 -0
- package/dist/utility/wrun/sources/row-lanes/option_contract_state.js +24 -12
- package/dist/utility/wrun/sources/row-lanes/option_prints.d.ts +55 -0
- package/dist/utility/wrun/sources/row-lanes/option_prints.js +95 -18
- package/dist/utility/wrun/sources/row-lanes/option_strikes.d.ts +15 -1
- package/dist/utility/wrun/sources/row-lanes/option_strikes.js +23 -8
- package/dist/utility/wrun/sources/row-lanes/option_surface.js +11 -11
- package/dist/utility/wrun/sources/wrun-poll-lane.types.d.ts +14 -2
- package/dist/utility/wrun/sources/wrun-poll-lane.types.js +6 -3
- package/dist/utility/wrun/sources/wrun-poll-lanes.util.d.ts +43 -8
- package/dist/utility/wrun/sources/wrun-poll-lanes.util.js +77 -11
- package/dist/utility/wrun/sources/wrun-row-lane.types.d.ts +31 -16
- package/dist/utility/wrun/sources/wrun-row-lane.types.js +2 -0
- package/dist/utility/wrun/sources/wrun-row-lanes.util.d.ts +52 -12
- package/dist/utility/wrun/sources/wrun-row-lanes.util.js +110 -46
- package/dist/utility/wrun/wrun-bar-count.util.d.ts +17 -6
- package/dist/utility/wrun/wrun-bar-count.util.js +27 -12
- package/dist/utility/wrun/wrun-browser-sources.leaf.d.ts +10 -0
- package/dist/utility/wrun/wrun-browser-sources.leaf.js +12 -0
- package/dist/utility/wrun/wrun-coin-lane-live.util.d.ts +3 -3
- package/dist/utility/wrun/wrun-coin-lane-live.util.js +53 -18
- package/dist/utility/wrun/wrun-color-settings.util.d.ts +6 -0
- package/dist/utility/wrun/wrun-color-settings.util.js +45 -0
- package/dist/utility/wrun/wrun-draft-overlay.util.js +14 -8
- package/dist/utility/wrun/wrun-failure-cause.d.ts +1 -1
- package/dist/utility/wrun/wrun-failure-cause.js +14 -0
- package/dist/utility/wrun/wrun-frames.js +6 -2
- package/dist/utility/wrun/wrun-host.service.d.ts +131 -59
- package/dist/utility/wrun/wrun-host.service.js +391 -194
- package/dist/utility/wrun/wrun-hosted-wire-caps.d.ts +1 -1
- package/dist/utility/wrun/wrun-hosted-wire-caps.js +2 -0
- package/dist/utility/wrun/wrun-hover.util.d.ts +3 -0
- package/dist/utility/wrun/wrun-hover.util.js +25 -4
- package/dist/utility/wrun/wrun-input-requirements.leaf.js +7 -4
- package/dist/utility/wrun/wrun-kit-run-tables.util.d.ts +24 -0
- package/dist/utility/wrun/wrun-kit-run-tables.util.js +87 -0
- package/dist/utility/wrun/wrun-lifecycle.contract.d.ts +2 -1
- package/dist/utility/wrun/wrun-live-tick.util.d.ts +8 -2
- package/dist/utility/wrun/wrun-live-tick.util.js +35 -16
- package/dist/utility/wrun/wrun-opra-flow.leaf.d.ts +40 -13
- package/dist/utility/wrun/wrun-opra-flow.leaf.js +39 -12
- package/dist/utility/wrun/wrun-option-contract-flow.util.d.ts +70 -21
- package/dist/utility/wrun/wrun-option-contract-flow.util.js +257 -112
- package/dist/utility/wrun/wrun-option-prints.util.d.ts +62 -22
- package/dist/utility/wrun/wrun-option-prints.util.js +148 -46
- package/dist/utility/wrun/wrun-option-views.util.d.ts +262 -0
- package/dist/utility/wrun/wrun-option-views.util.js +356 -0
- package/dist/utility/wrun/wrun-options-chain.util.d.ts +23 -2
- package/dist/utility/wrun/wrun-options-chain.util.js +37 -3
- package/dist/utility/wrun/wrun-overlay-drivers.util.d.ts +20 -5
- package/dist/utility/wrun/wrun-overlay-drivers.util.js +32 -7
- package/dist/utility/wrun/wrun-overlay-recovery.util.d.ts +9 -3
- package/dist/utility/wrun/wrun-overlay-recovery.util.js +146 -18
- package/dist/utility/wrun/wrun-overlay-run.util.d.ts +52 -8
- package/dist/utility/wrun/wrun-overlay-run.util.js +816 -243
- package/dist/utility/wrun/wrun-pack.util.d.ts +2 -1
- package/dist/utility/wrun/wrun-pin-spelling.util.d.ts +5 -0
- package/dist/utility/wrun/wrun-pin-spelling.util.js +28 -12
- package/dist/utility/wrun/wrun-pin-venues.util.js +4 -6
- package/dist/utility/wrun/wrun-prefix-cohort.util.d.ts +1 -1
- package/dist/utility/wrun/wrun-prefix-cohort.util.js +1 -1
- package/dist/utility/wrun/wrun-present-bits.util.d.ts +9 -0
- package/dist/utility/wrun/wrun-present-bits.util.js +39 -0
- package/dist/utility/wrun/wrun-refusal-surface.util.d.ts +3 -2
- package/dist/utility/wrun/wrun-refusal-surface.util.js +5 -4
- package/dist/utility/wrun/wrun-refusal.util.d.ts +66 -6
- package/dist/utility/wrun/wrun-refusal.util.js +335 -40
- package/dist/utility/wrun/wrun-registry-errors.util.d.ts +5 -3
- package/dist/utility/wrun/wrun-sandbox-row-source.d.ts +2 -2
- package/dist/utility/wrun/wrun-sha256.leaf.d.ts +13 -0
- package/dist/utility/wrun/wrun-sha256.leaf.js +84 -0
- package/dist/utility/wrun/wrun-source-rows.util.js +11 -0
- package/dist/utility/wrun/wrun-tier-grid.util.d.ts +28 -3
- package/dist/utility/wrun/wrun-tier-grid.util.js +79 -19
- package/dist/utility/wrun/wrun-transient-rerun.util.d.ts +14 -3
- package/dist/utility/wrun/wrun-transient-rerun.util.js +33 -8
- package/dist/workers/wrun/wrun-abi-host.d.ts +15 -2
- package/dist/workers/wrun/wrun-abi-host.js +31 -12
- package/dist/workers/wrun/wrun-engine-handler.d.ts +11 -28
- package/dist/workers/wrun/wrun-engine-handler.js +181 -12
- package/package.json +5 -5
- package/dist/utility/wrun/wrun-opra-underlying.util.d.ts +0 -12
- package/dist/utility/wrun/wrun-opra-underlying.util.js +0 -1
|
@@ -1,10 +1,24 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Main-thread WRUN host:
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
2
|
+
* Main-thread WRUN host: loads a package's module into the sandbox worker,
|
|
3
|
+
* owns that worker, and runs packed series requests through it. One shared
|
|
4
|
+
* worker serves every dev overlay; requests are id-matched, and a request
|
|
5
|
+
* that outlives its clock poisons the worker (terminate + respawn) so a
|
|
6
|
+
* spinning guest cannot wedge later runs.
|
|
7
|
+
*
|
|
8
|
+
* Modules (the shared engine protocol, wrun-chart-host 0.6.0): the main
|
|
9
|
+
* thread never hashes and never compiles a module. `loadModule` keeps a
|
|
10
|
+
* private copy of the package's bytes and posts them (`module-load`); the
|
|
11
|
+
* worker hashes them, checks the sheet's `wasm_sha256` pin, clamps the
|
|
12
|
+
* memory, prepares and compiles them (wrun-engine-handler.ts) and answers the
|
|
13
|
+
* module's key, digest and imports, which the opaque handle carries. Every
|
|
14
|
+
* full run and live-start names the module by key and carries the bytes
|
|
15
|
+
* again (copied, never transferred), so a respawned worker compiles them
|
|
16
|
+
* anew; nothing here tracks which keys the worker holds. Each request has
|
|
17
|
+
* two clocks of WRUN_RUN_TIMEOUT_MS: the start ceiling from the post (the
|
|
18
|
+
* wait behind other requests, the compile and the instantiation), then the
|
|
19
|
+
* run deadline from the worker's `{ id, running: true }`, posted once the
|
|
20
|
+
* module is instantiated, when the request executes; a terminal answer
|
|
21
|
+
* settles a request in either phase.
|
|
8
22
|
*
|
|
9
23
|
* Isolation: a death of the shared worker (the run timeout, or an error
|
|
10
24
|
* event with a request in flight) rejects every pending request with
|
|
@@ -38,45 +52,30 @@
|
|
|
38
52
|
* (`attachWrunStrategy`) exactly like the handle set.
|
|
39
53
|
*/
|
|
40
54
|
import { ensureWrunChartHostSharedHost, wrunChartHostEnv } from '../../host-env.js';
|
|
41
|
-
import { abiAtLeast, attachWrunLive, WrunLiveOrderError, wrunLiveOf } from '../../interfaces/wrun-runtime.interface.js';
|
|
55
|
+
import { abiAtLeast, attachWrunLive, WrunLiveOrderError, wrunLiveOf, wrunModuleKey, } from '../../interfaces/wrun-runtime.interface.js';
|
|
42
56
|
import { LOCAL_ENGINE_SELF_HEAL_RESET_GRACE_MS, localEngineSelfHealDelayMs, } from '../chart/local-engine-self-heal-backoff.util.js';
|
|
43
57
|
import { attachWrunStrategy, readWrunStrategySheet, wrunStrategyParamsOf } from './wrun-box-segment.util.js';
|
|
58
|
+
import { WRUN_DAEMON_ONLY_SOURCE_DISCLOSURES } from './wrun-kit-run-tables.util.js';
|
|
44
59
|
import { assertWrunSheetAbiCoupling, attachWrunDrawSkips, attachWrunHandles, readWrunHandleDefaults, } from './wrun-handles.util.js';
|
|
45
|
-
import { clampWrunModuleMemory } from './wrun-memory-clamp.util.js';
|
|
46
60
|
import { packWrunRows, unpackWrunResponse, wrunAbiVersion } from './wrun-pack.util.js';
|
|
47
61
|
import { WrunQuarantinedError, WrunWorkerPoisonError } from './wrun-registry-errors.util.js';
|
|
48
62
|
import { bindWrunHostLiveSeriesEnd } from './wrun-overlay-drivers.util.js';
|
|
49
|
-
/** Sources the kit marks daemon-only
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
* shared host entry does not export it. */
|
|
63
|
+
/** Sources the kit marks daemon-only are computed by the daemon, which owns their dependencies: the first
|
|
64
|
+
* input declaring one refuses the browser run by name, with the kit's words for it. Synchronous: the
|
|
65
|
+
* table is the kit's, copied import-free (wrun-kit-run-tables.util, pinned to the kit's export by its
|
|
66
|
+
* kit-mirror spec), so no run loads the kit bundle on the main thread to read it (OPTLOAD g1 round 3:
|
|
67
|
+
* that read evaluated 3.6 MB of authoring code, 836 ms on a measured add, for a table the kit ships empty).
|
|
68
|
+
* The chart's own candle classes (`ohlcv`, `time`) are never daemon-only. */
|
|
56
69
|
export function assertWrunBrowserSources(metadata) {
|
|
57
|
-
const candidates = [];
|
|
58
70
|
for (const input of metadata.inputs) {
|
|
59
71
|
for (const source of [input.source, metadata.inputSources[input.name]?.source]) {
|
|
60
|
-
if (typeof source
|
|
61
|
-
|
|
72
|
+
if (typeof source !== 'string' || source === 'ohlcv' || source === 'time')
|
|
73
|
+
continue;
|
|
74
|
+
if (!Object.hasOwn(WRUN_DAEMON_ONLY_SOURCE_DISCLOSURES, source))
|
|
75
|
+
continue;
|
|
76
|
+
throw new Error(`Input '${input.name}' (${source}): ${WRUN_DAEMON_ONLY_SOURCE_DISCLOSURES[source]}`);
|
|
62
77
|
}
|
|
63
78
|
}
|
|
64
|
-
if (candidates.length === 0)
|
|
65
|
-
return;
|
|
66
|
-
return wrunChartHostEnv()
|
|
67
|
-
.transport.loadWrunKit()
|
|
68
|
-
.then(({ wrunIndicatorKit }) => {
|
|
69
|
-
const daemonOnlySources = wrunIndicatorKit.WRUN_DAEMON_ONLY_SOURCES;
|
|
70
|
-
if (!daemonOnlySources)
|
|
71
|
-
return;
|
|
72
|
-
for (const [name, source] of candidates) {
|
|
73
|
-
const disclosure = typeof daemonOnlySources.has === 'function'
|
|
74
|
-
? daemonOnlySources.has(source) && 'computed on your daemon, not in the tab'
|
|
75
|
-
: Reflect.get(daemonOnlySources, source);
|
|
76
|
-
if (typeof disclosure === 'string')
|
|
77
|
-
throw new Error(`Input '${name}' (${source}): ${disclosure}`);
|
|
78
|
-
}
|
|
79
|
-
});
|
|
80
79
|
}
|
|
81
80
|
const WRUN_RUN_TIMEOUT_MS = 20_000;
|
|
82
81
|
/** One respawn of the shared worker per window (the kScript shared worker's
|
|
@@ -89,9 +88,74 @@ const WRUN_RUN_TIMEOUT_MS = 20_000;
|
|
|
89
88
|
export const WRUN_WORKER_RESPAWN_WINDOW_MS = 60_000;
|
|
90
89
|
/** `strategyBars` stride: open, high, low, close per row. */
|
|
91
90
|
const STRATEGY_BAR_STRIDE = 4;
|
|
92
|
-
/** How many
|
|
93
|
-
* and
|
|
91
|
+
/** How many module handles the host keeps (loadModule): every package version, draft Run and REPL compile mints a
|
|
92
|
+
* key, and the engine worker keeps an LRU of the same size of the compiled modules behind them. */
|
|
94
93
|
export const WRUN_MODULE_CACHE_CAPACITY = 16;
|
|
94
|
+
/** The provisional key tokens of unpinned loads: one per caller bytes object and ABI, held as long as the bytes are. */
|
|
95
|
+
const provisionalModuleKeys = new WeakMap();
|
|
96
|
+
/** An unpinned load's key until the worker names its digest: the caller's bytes object AND the ABI, so loads of one
|
|
97
|
+
* buffer for two ABIs never share (or replace) each other's entry. */
|
|
98
|
+
function provisionalModuleKey(bytes, abi) {
|
|
99
|
+
let byAbi = provisionalModuleKeys.get(bytes);
|
|
100
|
+
if (byAbi === undefined) {
|
|
101
|
+
byAbi = new Map();
|
|
102
|
+
provisionalModuleKeys.set(bytes, byAbi);
|
|
103
|
+
}
|
|
104
|
+
let key = byAbi.get(abi);
|
|
105
|
+
if (key === undefined) {
|
|
106
|
+
key = Object.freeze({ abi });
|
|
107
|
+
byAbi.set(abi, key);
|
|
108
|
+
}
|
|
109
|
+
return key;
|
|
110
|
+
}
|
|
111
|
+
/** The original bytes behind each handle this module's hosts loaded: copied at the load, never handed out, posted
|
|
112
|
+
* (copied, never transferred) with every full run and live-start. A handle no host here loaded has none: its runs
|
|
113
|
+
* carry no bytes, and a worker that does not hold its key refuses them by name. */
|
|
114
|
+
const wrunModuleBytes = new WeakMap();
|
|
115
|
+
const NO_MODULE_BYTES = new Uint8Array(0);
|
|
116
|
+
/** A caller's module bytes as a view of exactly their own range. */
|
|
117
|
+
function moduleBytesView(bytes) {
|
|
118
|
+
return ArrayBuffer.isView(bytes)
|
|
119
|
+
? new Uint8Array(bytes.buffer, bytes.byteOffset, bytes.byteLength)
|
|
120
|
+
: new Uint8Array(bytes);
|
|
121
|
+
}
|
|
122
|
+
/** A private copy of a caller's module bytes, in an ordinary buffer of its own: the caller may reuse, change or
|
|
123
|
+
* transfer its buffer after the load, and a shared buffer never backs the copy (a clone of a view on shared memory
|
|
124
|
+
* shares it, so the worker's copy could rewrite the bytes this host keeps). */
|
|
125
|
+
function snapshotModuleBytes(bytes) {
|
|
126
|
+
return new Uint8Array(moduleBytesView(bytes));
|
|
127
|
+
}
|
|
128
|
+
/** Whether a caller's bytes equal a private copy: the length, then every byte. */
|
|
129
|
+
function sameModuleBytes(copy, bytes) {
|
|
130
|
+
const view = moduleBytesView(bytes);
|
|
131
|
+
if (view.byteLength !== copy.byteLength)
|
|
132
|
+
return false;
|
|
133
|
+
for (let at = 0; at < view.byteLength; at += 1) {
|
|
134
|
+
if (view[at] !== copy[at])
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
return true;
|
|
138
|
+
}
|
|
139
|
+
/** The client's handle for a load the worker answered: opaque, frozen, its imports in the worker's order. */
|
|
140
|
+
function wrunModuleHandleOf(response, abi) {
|
|
141
|
+
return Object.freeze({
|
|
142
|
+
key: response.key,
|
|
143
|
+
sha256: response.sha256,
|
|
144
|
+
abi,
|
|
145
|
+
imports: Object.freeze(response.imports.map(({ module, name }) => Object.freeze({ module, name }))),
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
/** A refused load as its caller reads it: the class and the words the main thread's compile threw before 0.6.0 (a
|
|
149
|
+
* pin mismatch names the sheet's pin and the digest the worker found; an engine compile refusal is a
|
|
150
|
+
* WebAssembly.CompileError again; a clamp or rewrite refusal keeps its plain words). */
|
|
151
|
+
function wrunModuleLoadError(response, metadata) {
|
|
152
|
+
if (response.code === 'module-pin-mismatch') {
|
|
153
|
+
return new Error(`WRUN ${metadata.id}: wasm sha256 mismatch (metadata pins ${metadata.wasm_sha256}, bytes are ${response.sha256})`);
|
|
154
|
+
}
|
|
155
|
+
if (response.code === 'module-compile-failed')
|
|
156
|
+
return new WebAssembly.CompileError(response.error);
|
|
157
|
+
return new Error(response.error);
|
|
158
|
+
}
|
|
95
159
|
/** The quarantine key of a package: its metadata id plus the wasm digest the
|
|
96
160
|
* metadata pins, so a republished build (new digest) starts clean while every
|
|
97
161
|
* overlay of the same build shares one hold (two charts, two instances: a
|
|
@@ -193,11 +257,14 @@ function packWrunRunRequest(metadata, rows, initArgs, strategy, configure, textP
|
|
|
193
257
|
export class WrunBrowserHost {
|
|
194
258
|
worker = null;
|
|
195
259
|
pending = new Map();
|
|
260
|
+
/** One counter numbers loads, runs and live-ends: a retired incarnation's late answer names an id no entry holds. */
|
|
196
261
|
requestSeq = 0;
|
|
197
|
-
/**
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
*
|
|
262
|
+
/** The order requests enter the `run` phase in (`PendingRequest.runOrder`). */
|
|
263
|
+
runSeq = 0;
|
|
264
|
+
/** Loads by module, least recently used first (Map order: a hit moves its entry to the end). The cache only
|
|
265
|
+
* dedupes loads and owns no module: a mounted overlay keeps the handle it was handed (its record), and a live
|
|
266
|
+
* session keeps its instance in the worker (live ticks name the session, never a module), so an eviction stops
|
|
267
|
+
* neither; a handle's bytes stay behind it whatever the cache lets go, and its runs carry them. */
|
|
201
268
|
moduleCache = new Map();
|
|
202
269
|
/** Live sessions the current worker holds (installed by an ok live-start,
|
|
203
270
|
* dropped by live-end, a sessionLost answer, or the worker's death). */
|
|
@@ -213,24 +280,41 @@ export class WrunBrowserHost {
|
|
|
213
280
|
* the first death, so the first spawn ever never opens a window. */
|
|
214
281
|
lastRespawnAt = null;
|
|
215
282
|
respawnHeldUntil = 0;
|
|
216
|
-
/** Spin the sandbox worker
|
|
217
|
-
*
|
|
218
|
-
*
|
|
219
|
-
*
|
|
220
|
-
*
|
|
221
|
-
*
|
|
222
|
-
*
|
|
223
|
-
*
|
|
224
|
-
*
|
|
283
|
+
/** Spin the sandbox worker (its script's fetch starts with the spawn) and
|
|
284
|
+
* load the shared host (the packer, the leg planner, the strategy
|
|
285
|
+
* resolver; the worker holds its own for the module rewrite) ahead of the
|
|
286
|
+
* first add or Run, so the first package a reader mounts pays neither the
|
|
287
|
+
* worker spawn nor the host's parse on its click. Never the authoring kit:
|
|
288
|
+
* no run reads it on this thread (the kit tables a run reads are
|
|
289
|
+
* import-free copies, wrun-kit-run-tables.util), and its 3.6 MB evaluation
|
|
290
|
+
* was the longest main-thread task of a measured first add (OPTLOAD g1
|
|
291
|
+
* round 3). The
|
|
292
|
+
* web app's signals reach it through one owner
|
|
293
|
+
* (src/utility/chart/demand-code-warm.util.ts: a surface's open, the
|
|
294
|
+
* intent toward it); the add door, a restored package and a row's settled
|
|
295
|
+
* intent call it beside their own loads. Idempotent; a load failure
|
|
296
|
+
* surfaces on the first module load or run as before. A held respawn is not
|
|
297
|
+
* warmed around: the window exists to keep the worker down. Resolves true
|
|
298
|
+
* once the host landed and the worker spawned, false when either failed, so
|
|
299
|
+
* the caller can tell a warm from a failed one (an intent's latch clears on
|
|
300
|
+
* false and the next intent asks again). Never throws, never rejects. */
|
|
225
301
|
warm() {
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
302
|
+
let workerSpawned = true;
|
|
303
|
+
try {
|
|
304
|
+
if (this.respawnHold() === null)
|
|
305
|
+
this.ensureWorker();
|
|
306
|
+
}
|
|
307
|
+
catch {
|
|
308
|
+
workerSpawned = false;
|
|
309
|
+
}
|
|
310
|
+
let hostLoading;
|
|
311
|
+
try {
|
|
312
|
+
hostLoading = wrunChartHostEnv().loadSharedHost();
|
|
313
|
+
}
|
|
314
|
+
catch (error) {
|
|
315
|
+
hostLoading = Promise.reject(error);
|
|
316
|
+
}
|
|
317
|
+
return hostLoading.then(() => workerSpawned, () => false);
|
|
234
318
|
}
|
|
235
319
|
/** The active quarantine of a package, if its hold is still running. */
|
|
236
320
|
quarantineOf(metadata) {
|
|
@@ -244,86 +328,132 @@ export class WrunBrowserHost {
|
|
|
244
328
|
this.lastRespawnAt = null;
|
|
245
329
|
this.respawnHeldUntil = 0;
|
|
246
330
|
}
|
|
247
|
-
/**
|
|
248
|
-
*
|
|
249
|
-
*
|
|
250
|
-
*
|
|
251
|
-
*
|
|
252
|
-
*
|
|
253
|
-
*
|
|
331
|
+
/** Load (and cache) a package module: the worker hashes the bytes, refuses a
|
|
332
|
+
* digest other than the sheet's `wasm_sha256` pin with the words this host
|
|
333
|
+
* always used, refuses a memory the clamp refuses (a maximum or a starting
|
|
334
|
+
* size over the worker's memory limit, shared or 64-bit memory), stamps
|
|
335
|
+
* the limit as the maximum of a memory declared without one, exports every
|
|
336
|
+
* mutable global for the live lane's snapshot and restore, and compiles
|
|
337
|
+
* (wrun-engine-handler.ts `compileWrunModuleBytes`). This thread keeps a
|
|
338
|
+
* private copy of the bytes behind the handle and hashes and compiles
|
|
339
|
+
* nothing. A load needs the worker: during a held respawn every load
|
|
340
|
+
* rejects with the held death, a cached one too, and its start ceiling
|
|
341
|
+
* bounds the wait and the compile.
|
|
254
342
|
*
|
|
255
|
-
* The cache is an LRU of WRUN_MODULE_CACHE_CAPACITY
|
|
256
|
-
*
|
|
257
|
-
*
|
|
258
|
-
*
|
|
259
|
-
*
|
|
260
|
-
*
|
|
261
|
-
*
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
343
|
+
* The cache is an LRU of WRUN_MODULE_CACHE_CAPACITY handles, keyed by the
|
|
344
|
+
* sheet's pin and ABI when it pins one (its identity before and after the
|
|
345
|
+
* load), else by the bytes object and the ABI until the worker names the
|
|
346
|
+
* digest, then by the module's key; the first handle of a key stays the
|
|
347
|
+
* one every caller shares. A hit serves only bytes equal to the ones its
|
|
348
|
+
* entry loads: other bytes under the key (a pin claimed by bytes it was
|
|
349
|
+
* not verified from, a reused buffer whose content changed) go to the
|
|
350
|
+
* worker, which checks them against the pin, beside the cached entry and
|
|
351
|
+
* never in its place. Every caller of a load in flight shares its one
|
|
352
|
+
* post, and a load in flight is never evicted; eviction drops loaded
|
|
353
|
+
* entries only, least recently used first, so the cache passes the
|
|
354
|
+
* capacity only while more loads than that are in flight. A failed load is
|
|
355
|
+
* not cached: its entry leaves before its callers see the failure, and the
|
|
356
|
+
* next request loads again. Async, as the compile was: every failure (a
|
|
357
|
+
* detached buffer included) is a rejection, and nothing here awaits before
|
|
358
|
+
* the entry is cached. */
|
|
359
|
+
async loadModule(wasmBytes, metadata) {
|
|
360
|
+
// The hold first, before the cache: the window keeps the worker down, and a handle served from the cache now
|
|
361
|
+
// would only meet the held death at its first run.
|
|
362
|
+
const held = this.respawnHold();
|
|
363
|
+
if (held)
|
|
364
|
+
throw held;
|
|
365
|
+
const abi = wrunAbiVersion(metadata);
|
|
366
|
+
const pin = metadata.wasm_sha256 ? metadata.wasm_sha256 : undefined;
|
|
367
|
+
const cacheKey = pin !== undefined ? wrunModuleKey(pin, abi) : provisionalModuleKey(wasmBytes, abi);
|
|
368
|
+
const hit = this.moduleCache.get(cacheKey);
|
|
369
|
+
if (hit !== undefined && sameModuleBytes(hit.bytes, wasmBytes)) {
|
|
370
|
+
// A hit is the most recent use: set again, at the end of the order eviction reads from the front.
|
|
371
|
+
this.moduleCache.delete(cacheKey);
|
|
372
|
+
this.moduleCache.set(cacheKey, hit);
|
|
373
|
+
return hit.handle;
|
|
266
374
|
}
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
375
|
+
const bytes = snapshotModuleBytes(wasmBytes);
|
|
376
|
+
// Under a key whose entry loads other bytes, this load verifies its own beside that entry, never in its place.
|
|
377
|
+
const cacheable = hit === undefined;
|
|
378
|
+
const created = {
|
|
379
|
+
abi,
|
|
380
|
+
bytes,
|
|
381
|
+
loaded: false,
|
|
382
|
+
handle: this.requestModuleLoad(bytes, abi, pin, metadata).then((handle) => this.cacheLoadedModule(cacheable ? cacheKey : null, created, handle), (error) => {
|
|
383
|
+
if (cacheable && this.moduleCache.get(cacheKey) === created)
|
|
384
|
+
this.moduleCache.delete(cacheKey);
|
|
385
|
+
throw error;
|
|
386
|
+
}),
|
|
387
|
+
};
|
|
388
|
+
if (cacheable) {
|
|
389
|
+
this.moduleCache.set(cacheKey, created);
|
|
390
|
+
this.trimModuleCache();
|
|
271
391
|
}
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
// bytes; only the memory and export sections change. The rewriter is
|
|
286
|
-
// the shared host's `prepareModuleBytes` (the same rewrite under every
|
|
287
|
-
// ABI), loaded at the first compile and never on chart boot: this host
|
|
288
|
-
// sits on the chart's startup graph through the driver registry, and a
|
|
289
|
-
// viewer who never mounts an Indicator must not pay for host.js.
|
|
290
|
-
const abi = wrunAbiVersion(metadata);
|
|
291
|
-
const created = {
|
|
292
|
-
compiled: false,
|
|
293
|
-
module: wrunChartHostEnv()
|
|
294
|
-
.loadSharedHost()
|
|
295
|
-
.then(({ prepareModuleBytes }) => WebAssembly.compile(prepareModuleBytes(bounded, abi)))
|
|
296
|
-
.then((module) => {
|
|
297
|
-
created.compiled = true;
|
|
298
|
-
this.trimModuleCache();
|
|
299
|
-
return module;
|
|
300
|
-
}, (error) => {
|
|
301
|
-
if (this.moduleCache.get(digest) === created)
|
|
302
|
-
this.moduleCache.delete(digest);
|
|
303
|
-
throw error;
|
|
304
|
-
}),
|
|
305
|
-
};
|
|
306
|
-
entry = created;
|
|
392
|
+
return created.handle;
|
|
393
|
+
}
|
|
394
|
+
/** A load answered: its entry leaves the key it waited under (an unpinned sheet's provisional key; none for a load
|
|
395
|
+
* that verified other bytes beside a cached entry) for the module's own key. A handle already loaded under that key
|
|
396
|
+
* stays the one every caller shares. */
|
|
397
|
+
cacheLoadedModule(cacheKey, entry, handle) {
|
|
398
|
+
if (cacheKey !== null && this.moduleCache.get(cacheKey) === entry)
|
|
399
|
+
this.moduleCache.delete(cacheKey);
|
|
400
|
+
const known = this.moduleCache.get(handle.key);
|
|
401
|
+
if (known !== undefined && known !== entry && known.loaded) {
|
|
402
|
+
this.moduleCache.delete(handle.key);
|
|
403
|
+
this.moduleCache.set(handle.key, known);
|
|
404
|
+
return known.handle;
|
|
307
405
|
}
|
|
308
|
-
|
|
406
|
+
entry.loaded = true;
|
|
407
|
+
this.moduleCache.set(handle.key, entry);
|
|
309
408
|
this.trimModuleCache();
|
|
310
|
-
return
|
|
409
|
+
return handle;
|
|
311
410
|
}
|
|
312
|
-
/**
|
|
313
|
-
*
|
|
411
|
+
/** Post one `module-load` of the private bytes (copied, never transferred: the handle keeps them for its runs) and
|
|
412
|
+
* mint the handle from the worker's answer. */
|
|
413
|
+
requestModuleLoad(bytes, abi, pin, metadata) {
|
|
414
|
+
return new Promise((resolve, reject) => {
|
|
415
|
+
const request = {
|
|
416
|
+
kind: 'module-load',
|
|
417
|
+
id: ++this.requestSeq,
|
|
418
|
+
abi,
|
|
419
|
+
...(pin !== undefined ? { sha256Pin: pin } : {}),
|
|
420
|
+
bytes,
|
|
421
|
+
};
|
|
422
|
+
this.post(request, [], {
|
|
423
|
+
kind: 'load',
|
|
424
|
+
// The package a compile that outlives its ceiling is held under, like a run's.
|
|
425
|
+
quarantineKey: wrunHostQuarantineKey(metadata),
|
|
426
|
+
overrideQuarantine: false,
|
|
427
|
+
reject,
|
|
428
|
+
settle: (response) => {
|
|
429
|
+
if (!response.ok) {
|
|
430
|
+
reject(wrunModuleLoadError(response, metadata));
|
|
431
|
+
}
|
|
432
|
+
else if (!('kind' in response)) {
|
|
433
|
+
reject(new Error(`WRUN ${metadata.id}: module load ${request.id} was answered as a run`));
|
|
434
|
+
}
|
|
435
|
+
else {
|
|
436
|
+
const handle = wrunModuleHandleOf(response, abi);
|
|
437
|
+
wrunModuleBytes.set(handle, bytes);
|
|
438
|
+
resolve(handle);
|
|
439
|
+
}
|
|
440
|
+
},
|
|
441
|
+
});
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
/** Evict loaded entries, least recently used first, until the cache is back at its capacity (a load in flight
|
|
445
|
+
* stays: its callers share it). */
|
|
314
446
|
trimModuleCache() {
|
|
315
|
-
for (const [
|
|
447
|
+
for (const [key, entry] of this.moduleCache) {
|
|
316
448
|
if (this.moduleCache.size <= WRUN_MODULE_CACHE_CAPACITY)
|
|
317
449
|
return;
|
|
318
|
-
if (entry.
|
|
319
|
-
this.moduleCache.delete(
|
|
450
|
+
if (entry.loaded)
|
|
451
|
+
this.moduleCache.delete(key);
|
|
320
452
|
}
|
|
321
453
|
}
|
|
322
454
|
async runSeries(input) {
|
|
323
455
|
const lane = this.admit(input.metadata, input.overrideQuarantine);
|
|
324
|
-
|
|
325
|
-
if (sourceCheck)
|
|
326
|
-
await sourceCheck;
|
|
456
|
+
assertWrunBrowserSources(input.metadata);
|
|
327
457
|
// The packer and the strategy resolver read the shared host (loaded once, off the boot graph;
|
|
328
458
|
// a loaded host adds no await, so a warm run dispatches in the same task as before).
|
|
329
459
|
const hostLoading = ensureWrunChartHostSharedHost();
|
|
@@ -333,11 +463,10 @@ export class WrunBrowserHost {
|
|
|
333
463
|
const id = ++this.requestSeq;
|
|
334
464
|
const request = {
|
|
335
465
|
id,
|
|
336
|
-
module: input.module,
|
|
337
466
|
mode: input.mode ?? 'historical',
|
|
338
467
|
...packed,
|
|
339
468
|
};
|
|
340
|
-
const response = await this.
|
|
469
|
+
const response = await this.dispatchWithModule(request, transfer, lane, input.module);
|
|
341
470
|
return this.unpack(response, input.metadata, input.rows);
|
|
342
471
|
}
|
|
343
472
|
/** Full run that also opens (or replaces) a live forming-bar session under
|
|
@@ -348,9 +477,7 @@ export class WrunBrowserHost {
|
|
|
348
477
|
* key (see the worker's ownership rule). */
|
|
349
478
|
async startLiveSeries(input) {
|
|
350
479
|
const lane = this.admit(input.metadata, input.overrideQuarantine);
|
|
351
|
-
|
|
352
|
-
if (sourceCheck)
|
|
353
|
-
await sourceCheck;
|
|
480
|
+
assertWrunBrowserSources(input.metadata);
|
|
354
481
|
// The packer and the strategy resolver read the shared host (loaded once, off the boot graph;
|
|
355
482
|
// a loaded host adds no await, so a warm run dispatches in the same task as before).
|
|
356
483
|
const hostLoading = ensureWrunChartHostSharedHost();
|
|
@@ -366,15 +493,14 @@ export class WrunBrowserHost {
|
|
|
366
493
|
kind: 'live-start',
|
|
367
494
|
sessionKey: input.sessionKey,
|
|
368
495
|
id,
|
|
369
|
-
module: input.module,
|
|
370
496
|
mode: 'live',
|
|
371
497
|
...(input.epoch !== undefined ? { epoch: input.epoch } : {}),
|
|
372
498
|
...packed,
|
|
373
499
|
};
|
|
374
|
-
const response = await this.
|
|
500
|
+
const response = await this.dispatchWithModule(request, transfer, {
|
|
375
501
|
...lane,
|
|
376
502
|
session: { key: input.sessionKey, opens: true },
|
|
377
|
-
});
|
|
503
|
+
}, input.module);
|
|
378
504
|
return this.unpack(response, input.metadata, input.rows);
|
|
379
505
|
}
|
|
380
506
|
/** One live tick: evaluate only `rows` (the new/revised forming-bar rows)
|
|
@@ -386,9 +512,7 @@ export class WrunBrowserHost {
|
|
|
386
512
|
* broker config rides the session, only the rows' broker feed ships). */
|
|
387
513
|
async runLiveRows(input) {
|
|
388
514
|
const lane = this.admit(input.metadata, input.overrideQuarantine);
|
|
389
|
-
|
|
390
|
-
if (sourceCheck)
|
|
391
|
-
await sourceCheck;
|
|
515
|
+
assertWrunBrowserSources(input.metadata);
|
|
392
516
|
// The packer and the strategy resolver read the shared host (loaded once, off the boot graph;
|
|
393
517
|
// a loaded host adds no await, so a warm run dispatches in the same task as before).
|
|
394
518
|
const hostLoading = ensureWrunChartHostSharedHost();
|
|
@@ -483,18 +607,105 @@ export class WrunBrowserHost {
|
|
|
483
607
|
}
|
|
484
608
|
return { quarantineKey, overrideQuarantine: overrideQuarantine === true };
|
|
485
609
|
}
|
|
610
|
+
/** A full run or a live-start: the module rides the request, its key (the worker reuses the module it holds under
|
|
611
|
+
* it) and the host's private copy of its bytes (copied by structured clone, never transferred: a worker that does
|
|
612
|
+
* not hold the key, a respawned one or one that let it go, compiles them). The packed request's own `abi` stays
|
|
613
|
+
* the run's sheet's. */
|
|
614
|
+
dispatchWithModule(request, transfer, lane, module) {
|
|
615
|
+
return this.dispatch({ ...request, moduleKey: module.key, moduleBytes: wrunModuleBytes.get(module) ?? NO_MODULE_BYTES }, transfer, lane);
|
|
616
|
+
}
|
|
486
617
|
dispatch(request, transfer, lane) {
|
|
618
|
+
return new Promise((resolve, reject) => {
|
|
619
|
+
this.post(request, transfer, {
|
|
620
|
+
...lane,
|
|
621
|
+
kind: 'run',
|
|
622
|
+
reject,
|
|
623
|
+
settle: (response, entry) => this.settleRun(entry, response, resolve, reject),
|
|
624
|
+
});
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
/**
|
|
628
|
+
* Post one request under a pending entry: the respawn hold first (a held death refuses it, nothing posted), then
|
|
629
|
+
* the worker, the entry in its `start` phase with the start ceiling armed (the wait behind other requests, the
|
|
630
|
+
* module's compile and its instantiation, bounded as the dispatch clock bounded the hold before 0.6.0), then the
|
|
631
|
+
* post. A request the structured clone refuses fails alone (its entry and clock go with it).
|
|
632
|
+
*/
|
|
633
|
+
post(request, transfer, init) {
|
|
487
634
|
const held = this.respawnHold();
|
|
488
|
-
if (held)
|
|
489
|
-
|
|
635
|
+
if (held) {
|
|
636
|
+
init.reject(held);
|
|
637
|
+
return;
|
|
638
|
+
}
|
|
490
639
|
const worker = this.ensureWorker();
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
const timer = setTimeout(() => this.poisonWorker('timeout', 'WRUN worker terminated after a timed-out run', request.id), WRUN_RUN_TIMEOUT_MS);
|
|
495
|
-
this.pending.set(request.id, { ...lane, resolve, reject, timer });
|
|
640
|
+
const entry = { ...init, phase: 'start', runOrder: 0, timer: this.armClock(request.id) };
|
|
641
|
+
this.pending.set(request.id, entry);
|
|
642
|
+
try {
|
|
496
643
|
worker.postMessage(request, transfer);
|
|
497
|
-
}
|
|
644
|
+
}
|
|
645
|
+
catch (error) {
|
|
646
|
+
if (this.pending.get(request.id) === entry) {
|
|
647
|
+
this.pending.delete(request.id);
|
|
648
|
+
clearTimeout(entry.timer);
|
|
649
|
+
}
|
|
650
|
+
entry.reject(error instanceof Error ? error : new Error(String(error)));
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
/** A phase's clock: a request that outlives it means the worker is stuck (a guest spinning, a compile that never
|
|
654
|
+
* ends), so the worker is poisoned and replaced and later requests start clean. */
|
|
655
|
+
armClock(id) {
|
|
656
|
+
return setTimeout(() => this.poisonWorker('timeout', 'WRUN worker terminated after a timed-out run', id), WRUN_RUN_TIMEOUT_MS);
|
|
657
|
+
}
|
|
658
|
+
/** One answer from the worker. `running` moves its request to the `run` phase on a fresh clock (a second one, one
|
|
659
|
+
* for a load or for no pending request changes nothing); a terminal answer settles its request in either phase;
|
|
660
|
+
* an answer for no pending request (an untracked live-end's, a retired incarnation's) is dropped. */
|
|
661
|
+
receive(message) {
|
|
662
|
+
const entry = this.pending.get(message.id);
|
|
663
|
+
if (entry === undefined)
|
|
664
|
+
return;
|
|
665
|
+
if ('running' in message) {
|
|
666
|
+
if (entry.kind !== 'run' || entry.phase === 'run')
|
|
667
|
+
return;
|
|
668
|
+
// The module is instantiated and the run executes: its own deadline replaces the start ceiling, whatever the
|
|
669
|
+
// queue, the compile and the instantiation took.
|
|
670
|
+
clearTimeout(entry.timer);
|
|
671
|
+
entry.phase = 'run';
|
|
672
|
+
entry.runOrder = ++this.runSeq;
|
|
673
|
+
entry.timer = this.armClock(message.id);
|
|
674
|
+
return;
|
|
675
|
+
}
|
|
676
|
+
this.pending.delete(message.id);
|
|
677
|
+
clearTimeout(entry.timer);
|
|
678
|
+
entry.settle(message, entry);
|
|
679
|
+
}
|
|
680
|
+
/** A run's terminal answer: the package answered (an overridden quarantine is cleared by the success, and an
|
|
681
|
+
* opened session is one the worker now holds), a lost session, a live-order refusal, or a failure. */
|
|
682
|
+
settleRun(entry, response, resolve, reject) {
|
|
683
|
+
const { session } = entry;
|
|
684
|
+
if (response.ok) {
|
|
685
|
+
if ('kind' in response) {
|
|
686
|
+
reject(new Error(`WRUN request ${response.id} was answered as a module load`));
|
|
687
|
+
return;
|
|
688
|
+
}
|
|
689
|
+
if (entry.overrideQuarantine)
|
|
690
|
+
this.quarantines.delete(entry.quarantineKey);
|
|
691
|
+
if (session?.opens)
|
|
692
|
+
this.liveSessionKeys.add(session.key);
|
|
693
|
+
resolve(response);
|
|
694
|
+
}
|
|
695
|
+
else if (response.sessionLost) {
|
|
696
|
+
if (session)
|
|
697
|
+
this.liveSessionKeys.delete(session.key);
|
|
698
|
+
reject(new WrunLiveSessionLostError(response.error));
|
|
699
|
+
}
|
|
700
|
+
else if (response.code === 'live-order')
|
|
701
|
+
reject(new WrunLiveOrderError(response.error));
|
|
702
|
+
else {
|
|
703
|
+
// A failed tick drops its session in the worker; a failed start
|
|
704
|
+
// never replaces the session already open under its key.
|
|
705
|
+
if (session && !session.opens)
|
|
706
|
+
this.liveSessionKeys.delete(session.key);
|
|
707
|
+
reject(new Error(response.error));
|
|
708
|
+
}
|
|
498
709
|
}
|
|
499
710
|
ensureWorker() {
|
|
500
711
|
if (this.worker)
|
|
@@ -506,41 +717,10 @@ export class WrunBrowserHost {
|
|
|
506
717
|
// The host's engine port spawns the sandbox worker (web: the worker factory, with the worker script's URL and
|
|
507
718
|
// the factory's failure source, both the app's), synchronously, as the host's own call did.
|
|
508
719
|
this.worker = wrunChartHostEnv().engine.spawnWorker({
|
|
509
|
-
// The sandbox worker posts no handshake: any message proves it up. No ready deadline: the
|
|
510
|
-
//
|
|
720
|
+
// The sandbox worker posts no handshake: any message proves it up. No ready deadline: the requests' clocks,
|
|
721
|
+
// the quarantine and the respawn window own this worker's liveness.
|
|
511
722
|
readiness: { mode: 'first-message' },
|
|
512
|
-
onMessage: (event) =>
|
|
513
|
-
const response = event.data;
|
|
514
|
-
const pending = this.pending.get(response.id);
|
|
515
|
-
if (!pending)
|
|
516
|
-
return;
|
|
517
|
-
this.pending.delete(response.id);
|
|
518
|
-
clearTimeout(pending.timer);
|
|
519
|
-
const { session } = pending;
|
|
520
|
-
if (response.ok) {
|
|
521
|
-
// The package answered: an overridden quarantine is cleared by the
|
|
522
|
-
// success, and an opened session is one the worker now holds.
|
|
523
|
-
if (pending.overrideQuarantine)
|
|
524
|
-
this.quarantines.delete(pending.quarantineKey);
|
|
525
|
-
if (session?.opens)
|
|
526
|
-
this.liveSessionKeys.add(session.key);
|
|
527
|
-
pending.resolve(response);
|
|
528
|
-
}
|
|
529
|
-
else if (response.sessionLost) {
|
|
530
|
-
if (session)
|
|
531
|
-
this.liveSessionKeys.delete(session.key);
|
|
532
|
-
pending.reject(new WrunLiveSessionLostError(response.error));
|
|
533
|
-
}
|
|
534
|
-
else if (response.code === 'live-order')
|
|
535
|
-
pending.reject(new WrunLiveOrderError(response.error));
|
|
536
|
-
else {
|
|
537
|
-
// A failed tick drops its session in the worker; a failed start
|
|
538
|
-
// never replaces the session already open under its key.
|
|
539
|
-
if (session && !session.opens)
|
|
540
|
-
this.liveSessionKeys.delete(session.key);
|
|
541
|
-
pending.reject(new Error(response.error));
|
|
542
|
-
}
|
|
543
|
-
},
|
|
723
|
+
onMessage: (event) => this.receive(event.data),
|
|
544
724
|
// A worker-level error (a script or module chunk that would not load, an
|
|
545
725
|
// uncaught throw in the worker script) leaves the Worker unable to answer
|
|
546
726
|
// anything: keep it installed and every later run would post into a void
|
|
@@ -556,20 +736,20 @@ export class WrunBrowserHost {
|
|
|
556
736
|
}
|
|
557
737
|
/**
|
|
558
738
|
* The shared worker died: quarantine the package that was running (the
|
|
559
|
-
*
|
|
560
|
-
*
|
|
561
|
-
*
|
|
562
|
-
*
|
|
563
|
-
* flight means nothing to quarantine (a worker that dies idle is only a
|
|
739
|
+
* culprit, `culpritOf`), terminate the worker, remember the death for the
|
|
740
|
+
* respawn window, and reject every pending request (loads included) with
|
|
741
|
+
* the poison error naming the live sessions lost with the worker. Nothing
|
|
742
|
+
* in flight means nothing to quarantine (a worker that dies idle is only a
|
|
564
743
|
* death for the window). `message` is what every casualty reads; the
|
|
565
|
-
*
|
|
566
|
-
* (cause, the culprit's package and
|
|
567
|
-
* is recorded here once, frozen, and rides every
|
|
568
|
-
* the respawn hold's included, so a casualty can
|
|
744
|
+
* culprit of a timeout keeps its own deadline sentence. The death's
|
|
745
|
+
* context (cause, the culprit's package, session and phase, the budget a
|
|
746
|
+
* timeout used up) is recorded here once, frozen, and rides every
|
|
747
|
+
* rejection of this death, the respawn hold's included, so a casualty can
|
|
748
|
+
* name who held the sandbox.
|
|
569
749
|
*/
|
|
570
|
-
poisonWorker(reason, message,
|
|
750
|
+
poisonWorker(reason, message, expiredId) {
|
|
571
751
|
const now = Date.now();
|
|
572
|
-
const culprit =
|
|
752
|
+
const culprit = this.culpritOf(expiredId);
|
|
573
753
|
if (culprit !== undefined)
|
|
574
754
|
this.quarantine(culprit.quarantineKey, reason, now);
|
|
575
755
|
const casualties = new Set(this.liveSessionKeys);
|
|
@@ -585,7 +765,9 @@ export class WrunBrowserHost {
|
|
|
585
765
|
kind,
|
|
586
766
|
...(culprit !== undefined ? { quarantineKey: culprit.quarantineKey } : {}),
|
|
587
767
|
...(culprit?.session !== undefined ? { sessionKey: culprit.session.key } : {}),
|
|
588
|
-
|
|
768
|
+
...(culprit !== undefined ? { phase: culprit.phase } : {}),
|
|
769
|
+
// A phase's clock (from the post for `start`, from `running` for `run`): the budget bounds the hold, not the
|
|
770
|
+
// execution.
|
|
589
771
|
...(kind === 'timeout' ? { heldMs: WRUN_RUN_TIMEOUT_MS } : {}),
|
|
590
772
|
at: now,
|
|
591
773
|
});
|
|
@@ -596,11 +778,30 @@ export class WrunBrowserHost {
|
|
|
596
778
|
}
|
|
597
779
|
for (const [id, entry] of this.pending) {
|
|
598
780
|
clearTimeout(entry.timer);
|
|
599
|
-
const own = reason === 'timeout' &&
|
|
781
|
+
const own = reason === 'timeout' && entry === culprit
|
|
782
|
+
? `WRUN ${entry.kind === 'load' ? 'module load' : 'run'} ${id} timed out after ${WRUN_RUN_TIMEOUT_MS}ms`
|
|
783
|
+
: message;
|
|
600
784
|
entry.reject(wrunWorkerPoisonError(death, own));
|
|
601
785
|
}
|
|
602
786
|
this.pending.clear();
|
|
603
787
|
}
|
|
788
|
+
/** Who held the worker when it died: the request in the `run` phase, the hog holding the worker's synchronous turn
|
|
789
|
+
* (the expired request itself when it was running, else the first that started running), else the expired
|
|
790
|
+
* request (nothing ran: its own wait and compile used its ceiling up), else, for an error event, the oldest
|
|
791
|
+
* request in flight. */
|
|
792
|
+
culpritOf(expiredId) {
|
|
793
|
+
const expired = expiredId !== undefined ? this.pending.get(expiredId) : undefined;
|
|
794
|
+
if (expired?.phase === 'run')
|
|
795
|
+
return expired;
|
|
796
|
+
let hog;
|
|
797
|
+
for (const entry of this.pending.values()) {
|
|
798
|
+
if (entry.phase === 'run' && (hog === undefined || entry.runOrder < hog.runOrder))
|
|
799
|
+
hog = entry;
|
|
800
|
+
}
|
|
801
|
+
if (hog !== undefined)
|
|
802
|
+
return hog;
|
|
803
|
+
return expiredId !== undefined ? expired : this.pending.values().next().value;
|
|
804
|
+
}
|
|
604
805
|
/** Put (or keep) a package on the ladder: a death while its previous hold
|
|
605
806
|
* runs, or inside the reset grace after it, is a repeat and doubles the
|
|
606
807
|
* hold (capped); a later death starts over at the base hold. Records past
|
|
@@ -649,7 +850,3 @@ catch {
|
|
|
649
850
|
export function __resetWrunBrowserHostGuardsForTests() {
|
|
650
851
|
sharedHost?.resetGuardsForTests();
|
|
651
852
|
}
|
|
652
|
-
async function sha256Hex(bytes) {
|
|
653
|
-
const digest = await crypto.subtle.digest('SHA-256', bytes);
|
|
654
|
-
return [...new Uint8Array(digest)].map((byte) => byte.toString(16).padStart(2, '0')).join('');
|
|
655
|
-
}
|