@nimbus-sh/worker 0.2.3 → 0.4.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.
Files changed (146) hide show
  1. package/dist/_shared/session-router.d.ts +8 -0
  2. package/dist/_shared/session-router.d.ts.map +1 -1
  3. package/dist/_shared/session-router.js +8 -0
  4. package/dist/esbuild-wasm-bundle.generated.d.ts +1 -1
  5. package/dist/esbuild-wasm-bundle.generated.js +1 -1
  6. package/dist/facets/cirrus-real.js +1 -1
  7. package/dist/facets/manager.d.ts +43 -129
  8. package/dist/facets/manager.d.ts.map +1 -1
  9. package/dist/facets/manager.js +107 -318
  10. package/dist/facets/opencode-staging.d.ts +1 -1
  11. package/dist/facets/opencode-staging.d.ts.map +1 -1
  12. package/dist/facets/opencode-staging.js +3 -0
  13. package/dist/facets/real-vite-hmr.js +1 -1
  14. package/dist/facets/vite-dev-server.d.ts +4 -4
  15. package/dist/facets/vite-dev-server.js +5 -5
  16. package/dist/git/commands.d.ts +8 -0
  17. package/dist/git/commands.d.ts.map +1 -1
  18. package/dist/git/commands.js +41 -9
  19. package/dist/git/network-facet.d.ts +1 -1
  20. package/dist/git/network-facet.d.ts.map +1 -1
  21. package/dist/git/network-facet.js +39 -15
  22. package/dist/git-bundle.generated.d.ts +2 -2
  23. package/dist/git-bundle.generated.d.ts.map +1 -1
  24. package/dist/git-bundle.generated.js +3 -3
  25. package/dist/index.d.ts.map +1 -1
  26. package/dist/index.js +9 -1
  27. package/dist/loaders/child-process/spawn-facet.js +1 -1
  28. package/dist/loaders/child-process/spawn-pool.d.ts.map +1 -1
  29. package/dist/loaders/child-process/spawn-pool.js +2 -2
  30. package/dist/loaders/generated-workers.d.ts +2 -2
  31. package/dist/loaders/generated-workers.d.ts.map +1 -1
  32. package/dist/loaders/generated-workers.js +3 -3
  33. package/dist/loaders/npm-resolve-preamble.d.ts +3 -3
  34. package/dist/loaders/npm-resolve-preamble.js +3 -3
  35. package/dist/loaders/pre-bundle-preamble.d.ts +5 -5
  36. package/dist/loaders/pre-bundle-preamble.js +6 -6
  37. package/dist/loaders/process-host.d.ts +6 -107
  38. package/dist/loaders/process-host.d.ts.map +1 -1
  39. package/dist/loaders/process-host.js +6 -405
  40. package/dist/npm/install-batch-facet.d.ts +1 -1
  41. package/dist/npm/install-batch-facet.js +1 -1
  42. package/dist/npm/installer.d.ts +6 -6
  43. package/dist/npm/installer.d.ts.map +1 -1
  44. package/dist/npm/installer.js +28 -27
  45. package/dist/npm/pre-bundle-facet.d.ts +2 -2
  46. package/dist/npm/pre-bundle-facet.js +5 -5
  47. package/dist/npm/r2-cache.d.ts +1 -1
  48. package/dist/npm/resolve-facet.d.ts +1 -1
  49. package/dist/npm/resolve-facet.js +1 -1
  50. package/dist/npm/resolve-one-facet.d.ts +5 -5
  51. package/dist/npm/resolve-one-facet.js +5 -5
  52. package/dist/router/index.js +2 -2
  53. package/dist/router/remote-api.js +9 -1
  54. package/dist/runtime/bun-repl.d.ts +1 -1
  55. package/dist/runtime/bun-repl.js +3 -3
  56. package/dist/runtime/esbuild-wasm-bytes.js +1 -1
  57. package/dist/runtime/facet-loader-host.d.ts +5 -8
  58. package/dist/runtime/facet-loader-host.d.ts.map +1 -1
  59. package/dist/runtime/facet-loader-host.js +7 -11
  60. package/dist/runtime/node-repl.js +2 -2
  61. package/dist/runtime/node-shims-artifact.js +1 -1
  62. package/dist/runtime/node-shims.js +2 -1
  63. package/dist/runtime/opencode-artifact.js +1 -1
  64. package/dist/runtime/opentui-wasm-bytes.js +1 -1
  65. package/dist/runtime/python-repl.js +3 -3
  66. package/dist/runtime/ruby-repl.js +2 -2
  67. package/dist/runtime/ruby-resident.js +1 -1
  68. package/dist/runtime/sqlite-wasm-bytes.js +1 -1
  69. package/dist/session/diag.js +1 -1
  70. package/dist/session/hibernation.d.ts +20 -53
  71. package/dist/session/hibernation.d.ts.map +1 -1
  72. package/dist/session/hibernation.js +57 -194
  73. package/dist/session/init-phases.d.ts +2 -3
  74. package/dist/session/init-phases.d.ts.map +1 -1
  75. package/dist/session/init-phases.js +3 -2
  76. package/dist/session/init.d.ts.map +1 -1
  77. package/dist/session/init.js +8 -4
  78. package/dist/session/keys.d.ts +14 -37
  79. package/dist/session/keys.d.ts.map +1 -1
  80. package/dist/session/keys.js +18 -37
  81. package/dist/session/nimbus-session.d.ts +10 -15
  82. package/dist/session/nimbus-session.d.ts.map +1 -1
  83. package/dist/session/nimbus-session.js +28 -31
  84. package/dist/session/port-capability.d.ts +43 -0
  85. package/dist/session/port-capability.d.ts.map +1 -0
  86. package/dist/session/port-capability.js +49 -0
  87. package/dist/session/programmatic.d.ts +39 -5
  88. package/dist/session/programmatic.d.ts.map +1 -1
  89. package/dist/session/programmatic.js +150 -19
  90. package/dist/session/routes.d.ts +2 -0
  91. package/dist/session/routes.d.ts.map +1 -1
  92. package/dist/session/routes.js +96 -20
  93. package/dist/session/rpc.d.ts +36 -9
  94. package/dist/session/rpc.d.ts.map +1 -1
  95. package/dist/session/rpc.js +84 -24
  96. package/dist/session/start-real-vite.d.ts.map +1 -1
  97. package/dist/session/start-real-vite.js +3 -1
  98. package/dist/session/state-store.d.ts +3 -2
  99. package/dist/session/state-store.d.ts.map +1 -1
  100. package/dist/session/state-store.js +3 -2
  101. package/dist/session/supervisor-rpc.d.ts.map +1 -1
  102. package/dist/session/supervisor-rpc.js +7 -4
  103. package/dist/session/ws.d.ts +1 -3
  104. package/dist/session/ws.d.ts.map +1 -1
  105. package/dist/session/ws.js +4 -3
  106. package/dist/wrangler/nimbus-wrangler.js +1 -1
  107. package/package.json +4 -2
  108. package/scripts/bundle-esbuild-wasm.mjs +2 -2
  109. package/scripts/bundle-facet-workers.mjs +5 -7
  110. package/scripts/bundle-sqlite-wasm.mjs +1 -1
  111. package/dist/facets/inner-do-registry.d.ts +0 -41
  112. package/dist/facets/inner-do-registry.d.ts.map +0 -1
  113. package/dist/facets/inner-do-registry.js +0 -51
  114. package/dist/facets/launch-pacer.d.ts +0 -111
  115. package/dist/facets/launch-pacer.d.ts.map +0 -1
  116. package/dist/facets/launch-pacer.js +0 -130
  117. package/dist/loaders/fanout-pool.d.ts +0 -207
  118. package/dist/loaders/fanout-pool.d.ts.map +0 -1
  119. package/dist/loaders/fanout-pool.js +0 -363
  120. package/dist/loaders/loader-pool.d.ts +0 -294
  121. package/dist/loaders/loader-pool.d.ts.map +0 -1
  122. package/dist/loaders/loader-pool.js +0 -642
  123. package/dist/loaders/process-fabric.d.ts +0 -509
  124. package/dist/loaders/process-fabric.d.ts.map +0 -1
  125. package/dist/loaders/process-fabric.js +0 -360
  126. package/dist/loaders/vendor/errors.d.ts +0 -24
  127. package/dist/loaders/vendor/errors.d.ts.map +0 -1
  128. package/dist/loaders/vendor/errors.js +0 -46
  129. package/dist/loaders/vendor/serialize.d.ts +0 -3
  130. package/dist/loaders/vendor/serialize.d.ts.map +0 -1
  131. package/dist/loaders/vendor/serialize.js +0 -25
  132. package/dist/loaders/vendor/types.d.ts +0 -69
  133. package/dist/loaders/vendor/types.d.ts.map +0 -1
  134. package/dist/loaders/vendor/types.js +0 -4
  135. package/dist/loaders/workerd-facet-host.d.ts +0 -141
  136. package/dist/loaders/workerd-facet-host.d.ts.map +0 -1
  137. package/dist/loaders/workerd-facet-host.js +0 -284
  138. package/dist/session/bindings.d.ts +0 -209
  139. package/dist/session/bindings.d.ts.map +0 -1
  140. package/dist/session/bindings.js +0 -680
  141. package/dist/session/ctx-exports.d.ts +0 -16
  142. package/dist/session/ctx-exports.d.ts.map +0 -1
  143. package/dist/session/ctx-exports.js +0 -22
  144. package/dist/session/ws-hibernation-config.d.ts +0 -64
  145. package/dist/session/ws-hibernation-config.d.ts.map +0 -1
  146. package/dist/session/ws-hibernation-config.js +0 -89
@@ -1,642 +0,0 @@
1
- /**
2
- * loader-pool.ts — Nimbus loader-isolate pool based on cloudflare-parallel.
3
- *
4
- * Adds Nimbus-specific behavior to the upstream pool design:
5
- * 1. **Stable-slot isolate reuse**. Upstream's #counter++ gives every
6
- * dispatch a fresh isolate — fine for one-off AI calls, terrible for
7
- * running 67 npm tarball extractions (cold-start dominates). We pin
8
- * each job to `slot = cursor % concurrency` and use stable loader
9
- * IDs `nfp:${fnHash}:slot-${i}:g${generation}`, so a pool of
10
- * concurrency=4 keeps at most 4 warm isolates rather than N fresh ones.
11
- * 2. **Nimbus defaults**: compatibilityDate = CF_COMPAT_DATE (matches
12
- * the supervisor worker), compatibilityFlags = ['nodejs_compat'],
13
- * globalOutbound = undefined (inherit parent network so the facet can
14
- * reach https://registry.npmjs.org without a proxy binding).
15
- * 3. **SupervisorRPC autoinjection**. The pool grabs a SupervisorRPC
16
- * stub from `getCtxExports()` and forwards it as `env.SUPERVISOR` to
17
- * every facet, same pattern as git-network-facet.ts. Callers can add
18
- * more bindings via `extraBindings`.
19
- * 4. **Fail-loud defaults**: timeout 60s, retries 0, onError 'throw'.
20
- * Caller opts in to leniency.
21
- *
22
- * The vendored directory contains only the upstream serialization, error,
23
- * and binding types used by this implementation.
24
- */
25
- import { CF_COMPAT_DATE } from '@nimbus-sh/core/constants.js';
26
- import { getCtxExports } from '../session/ctx-exports.js';
27
- import { disposeRpcResource } from '@nimbus-sh/core/_shared/rpc-dispose.js';
28
- import { serializeFunction, hashSource } from './vendor/serialize.js';
29
- import { recordFailure, setLastFacetId, getLastRpcFrame } from '@nimbus-sh/core/observability/oom-discriminator.js';
30
- import { classifyError } from '@nimbus-sh/core/observability/oom-classify.js';
31
- import { BindingError, ExecutionError, RetryExhaustedError, TimeoutError, } from './vendor/errors.js';
32
- /**
33
- * esbuild runtime helpers re-declared at the top of every generated facet
34
- * module. esbuild emits `__name(fn, "fn")` wrappers around every named
35
- * function or arrow-with-binding-name; `fn.toString()` yields a body that
36
- * references `__name` by bare identifier. The supervisor bundle declares
37
- * `__name` at its own top level, but the binding does NOT cross isolate
38
- * boundaries — the facet's worker.js must re-declare it.
39
- *
40
- * The shim is bytes-stable so it doesn't perturb the loader-cache key;
41
- * if esbuild ever emits a new helper we'll see a "<name> is not defined"
42
- * error in the facet, add it here, and every slot rebuilds.
43
- */
44
- const ESBUILD_RUNTIME_SHIM = [
45
- 'const __defProp = Object.defineProperty;',
46
- 'const __name = (target, value) => __defProp(target, "name", { value, configurable: true });',
47
- 'const __nimbusDisposeRpcResult = (value) => {',
48
- ' if ((typeof value !== "object" && typeof value !== "function") || value === null) return;',
49
- ' const dispose = value[Symbol.dispose];',
50
- ' if (typeof dispose === "function") { try { dispose.call(value); } catch {} }',
51
- '};',
52
- 'const __nimbusUseRpcResult = async (promise, use) => {',
53
- ' const value = await promise;',
54
- ' try { return await use(value); }',
55
- ' finally { __nimbusDisposeRpcResult(value); }',
56
- '};',
57
- ].join('\n');
58
- /** Assemble the exact JavaScript module parsed by a dynamic loader worker. */
59
- export function assembleLoaderWorkerModuleSource(options) {
60
- const lines = [
61
- 'import { WorkerEntrypoint } from "cloudflare:workers";',
62
- ];
63
- const wasmEntries = options.wasmEntries ?? [];
64
- if (wasmEntries.length > 0) {
65
- lines.push('');
66
- lines.push('// ── Pool-injected WebAssembly modules ─────────────────────');
67
- for (const entry of wasmEntries) {
68
- lines.push(`import __NIMBUS_WASM_${entry.id} from './${entry.name}';`);
69
- }
70
- lines.push('globalThis.__NIMBUS_WASM = globalThis.__NIMBUS_WASM || {};');
71
- for (const entry of wasmEntries) {
72
- lines.push(`globalThis.__NIMBUS_WASM[${JSON.stringify(entry.name)}] = __NIMBUS_WASM_${entry.id};`);
73
- }
74
- lines.push('// ── End pool-injected WebAssembly modules ─────────────────');
75
- }
76
- lines.push('', '// ── esbuild runtime shim ──────────────────────────────────', '// When Nimbus is bundled by wrangler/esbuild, our facet function', '// is transformed into `__name(async function …, "…")` at emit', '// time. `fn.toString()` then yields the wrapped function body,', '// but `__name` and its helpers are module-local in the SUPERVISOR', '// bundle and do NOT cross into the facet isolate. Redeclare them', '// here so facet bodies survive the toString() round-trip.', ESBUILD_RUNTIME_SHIM, '// ── End esbuild runtime shim ──────────────────────────────', '');
77
- if (options.preamble) {
78
- lines.push('// ── Preamble (pool-level helpers) ─────────────────────────', options.preamble, '// ── End preamble ──────────────────────────────────────────', '');
79
- }
80
- lines.push(`const __fn__ = ${options.fnSource};`);
81
- lines.push('');
82
- const callExpr = options.hasBindings
83
- ? '__fn__(...args, this.env)'
84
- : '__fn__(...args)';
85
- lines.push('export default class extends WorkerEntrypoint {', ' execute(...args) {', ` const result = ${callExpr};`, ' if (result instanceof Promise) return result;', ' return result;', ' }', '}');
86
- return lines.join('\n');
87
- }
88
- /**
89
- * Nimbus-scoped parallel dispatch over `env.LOADER`. Tasks are pure
90
- * functions whose last argument is an `env` object containing the
91
- * forwarded bindings (default: `{ SUPERVISOR }`).
92
- *
93
- * Typical use:
94
- *
95
- * const pool = new NimbusLoaderPool(env, ctx, {
96
- * concurrency: 4,
97
- * tag: 'npm-install',
98
- * });
99
- * const results = await pool.map(
100
- * async (pkg, env) => env.SUPERVISOR.writeBatch(buildPayload(pkg)),
101
- * toFetch,
102
- * );
103
- */
104
- export class NimbusLoaderPool {
105
- loader;
106
- concurrency;
107
- defaultTimeoutMs;
108
- defaultRetries;
109
- tag;
110
- slotGenerations = new Map();
111
- bindings;
112
- preamble;
113
- preambleHash;
114
- /**
115
- * WASM modules to ship in the LOADER `modules` map. See
116
- * NimbusLoaderPoolOptions.wasmModules for the rationale. Stored in
117
- * insertion order so the per-import preamble we generate matches
118
- * across pool dispatches (cache-key stability).
119
- */
120
- wasmModules;
121
- /** Hash of (name + byte length + first/last bytes) of every wasm
122
- * module, folded into the loader cache key so changes invalidate
123
- * warm slots. Hashing the FULL bytes would be O(20+ MiB) per dispatch
124
- * and is unnecessary — wasm bytes are pinned at deploy time, the
125
- * length+endpoints are a strong-enough fingerprint. */
126
- wasmHash;
127
- /**
128
- * Short prefix of the owning DO's id, baked into the loader.get()
129
- * cache key so warm isolates are scoped to ONE session. Without this,
130
- * session A's pool and session B's pool (same `tag` + `fnHash`) share
131
- * an isolate — which means B's writeBatch RPCs routed through A's
132
- * env.SUPERVISOR binding (minted with A's doId at construction
133
- * time). B's install reports success but the writes land in A's VFS,
134
- * leaving B with only the git-clone seed files (~119 instead of ~1491).
135
- * 12 chars is enough entropy for DO ids to collide-free per process.
136
- */
137
- doIdShort;
138
- constructor(env, ctx, opts) {
139
- const loader = env?.LOADER;
140
- if (!loader || typeof loader.get !== 'function') {
141
- throw new BindingError('NimbusLoaderPool: env.LOADER binding missing or invalid. ' +
142
- 'Add a [[worker_loaders]] entry to wrangler.jsonc.');
143
- }
144
- this.loader = loader;
145
- this.concurrency = Math.max(1, opts?.concurrency ?? 4);
146
- this.defaultTimeoutMs = opts?.timeoutMs ?? 60_000;
147
- this.defaultRetries = Math.max(0, opts?.retries ?? 0);
148
- this.tag = opts?.tag ?? 'facet';
149
- this.preamble = opts?.preamble;
150
- // Include preamble in the cache-bucket key so changes to bundled helpers
151
- // invalidate warm slots. Empty preamble → '0' suffix (stable).
152
- this.preambleHash = this.preamble ? hashSource(this.preamble) : '0';
153
- this.doIdShort = opts?.cacheScope === 'global'
154
- ? 'global'
155
- : ctx.id.toString().slice(0, 12);
156
- // Materialise the wasm-modules table. Sanitise each name into a
157
- // valid JS identifier for the static import binding; key collisions
158
- // (e.g. 'esbuild.wasm' and 'esbuild_wasm' both sanitise to
159
- // 'esbuild_wasm') are rejected loudly because the generated worker
160
- // would otherwise have duplicate imports. Order is preserved.
161
- const wasmEntries = [];
162
- const seenIds = new Set();
163
- if (opts?.wasmModules) {
164
- for (const [name, bytes] of Object.entries(opts.wasmModules)) {
165
- if (!(bytes instanceof ArrayBuffer)) {
166
- throw new BindingError(`NimbusLoaderPool: wasmModules['${name}'] must be ArrayBuffer ` +
167
- `(got ${bytes?.constructor?.name || typeof bytes}).`);
168
- }
169
- const id = name.replace(/[^A-Za-z0-9_]/g, '_').replace(/^[^A-Za-z_]/, '_');
170
- if (seenIds.has(id)) {
171
- throw new BindingError(`NimbusLoaderPool: wasmModules key '${name}' collides with another after ` +
172
- `identifier-sanitisation (id='${id}'). Pick distinct module names.`);
173
- }
174
- seenIds.add(id);
175
- wasmEntries.push({ name, id, bytes });
176
- }
177
- }
178
- this.wasmModules = wasmEntries;
179
- // Fingerprint: name + length + first/last byte of each module.
180
- // Hashing 20+ MiB of wasm per dispatch would be wasteful; this
181
- // fingerprint is bytes-stable for a given deployed bundle and only
182
- // changes when the wasm itself changes (deploy-time event).
183
- if (wasmEntries.length === 0) {
184
- this.wasmHash = '0';
185
- }
186
- else {
187
- const fp = wasmEntries
188
- .map((w) => {
189
- const u = new Uint8Array(w.bytes);
190
- const len = u.byteLength;
191
- const first = len > 0 ? u[0] : 0;
192
- const last = len > 0 ? u[len - 1] : 0;
193
- return `${w.name}:${len}:${first}:${last}`;
194
- })
195
- .join('|');
196
- this.wasmHash = hashSource(fp);
197
- }
198
- const bindings = { ...(opts?.extraBindings ?? {}) };
199
- if (!opts?.omitSupervisor) {
200
- const ctxExports = getCtxExports();
201
- if (ctxExports?.SupervisorRPC) {
202
- // INSTALL-HONESTY: peer-DO branch supplies coordinator's doId
203
- // via supervisorDoIdOverride so SUPERVISOR.* RPCs route back
204
- // to the user's session DO, not the peer DO. Default to the
205
- // local ctx.id (single-DO callers and the in-DO in-DO fanout path).
206
- const supDoId = opts?.supervisorDoIdOverride ?? ctx.id.toString();
207
- bindings.SUPERVISOR = ctxExports.SupervisorRPC({
208
- props: { doId: supDoId, pid: opts?.supervisorPid ?? 0 },
209
- });
210
- }
211
- else {
212
- // SupervisorRPC unavailable — running without ctx.exports
213
- // (e.g. unit-test harness, or LOADER.load contexts where the
214
- // bindings.SUPERVISOR auto-wire isn't set up). We still construct
215
- // the pool but the facet will get env.SUPERVISOR === undefined.
216
- // Callers that need SUPERVISOR should check availability before
217
- // dispatch. (A facet that tries to call env.SUPERVISOR.writeBatch
218
- // will throw a plain TypeError; that's the clearest failure mode.)
219
- }
220
- }
221
- this.bindings = Object.keys(bindings).length > 0 ? bindings : undefined;
222
- }
223
- /** Effective concurrency used when no per-call override is supplied. */
224
- get defaultConcurrency() {
225
- return this.concurrency;
226
- }
227
- #resolve(opts) {
228
- return {
229
- timeoutMs: Math.max(0, opts?.timeoutMs ?? this.defaultTimeoutMs),
230
- retries: Math.max(0, opts?.retries ?? this.defaultRetries),
231
- };
232
- }
233
- /**
234
- * Materialise per-call wasm bytes into the {name,id,bytes} shape
235
- * the import-build path expects. Mirrors the constructor's logic
236
- * (identifier sanitisation + collision check) but ALSO rejects
237
- * collisions with constructor-time entries.
238
- *
239
- * Returns [] when there are no per-call entries — that's the hot
240
- * path (every existing pool dispatch).
241
- */
242
- #materialisePerCallWasm(perCall) {
243
- if (!perCall)
244
- return [];
245
- const out = [];
246
- const ctorIds = new Set(this.wasmModules.map((w) => w.id));
247
- const seen = new Set();
248
- for (const [name, bytes] of Object.entries(perCall)) {
249
- if (!(bytes instanceof ArrayBuffer)) {
250
- throw new BindingError(`NimbusLoaderPool: per-call wasmModules['${name}'] must be ` +
251
- `ArrayBuffer (got ${bytes?.constructor?.name || typeof bytes}).`);
252
- }
253
- const id = name.replace(/[^A-Za-z0-9_]/g, '_').replace(/^[^A-Za-z_]/, '_');
254
- if (ctorIds.has(id)) {
255
- throw new BindingError(`NimbusLoaderPool: per-call wasmModules key '${name}' (sanitised ` +
256
- `id='${id}') collides with a constructor-time wasm module. ` +
257
- `Per-call modules cannot shadow pool-defaults. Pick a distinct name.`);
258
- }
259
- if (seen.has(id)) {
260
- throw new BindingError(`NimbusLoaderPool: per-call wasmModules key '${name}' (sanitised ` +
261
- `id='${id}') collides with another per-call key. Pick distinct names.`);
262
- }
263
- seen.add(id);
264
- out.push({ name, id, bytes });
265
- }
266
- return out;
267
- }
268
- /**
269
- * Compute a stable fingerprint of a list of {name,bytes} entries.
270
- * Returns '0' for the empty list (cache-key bytes-stable for the
271
- * common no-per-call-wasm case). Same fingerprinting strategy as
272
- * the constructor's `wasmHash`: name + length + first/last byte
273
- * per module. Hashing all bytes would be O(20+ MiB) per dispatch
274
- * for nothing — the length+endpoints fingerprint is a strong-
275
- * enough discriminator and identical bytes produce identical
276
- * fingerprints (warm reuse).
277
- *
278
- * Per-call wasm fingerprinting MUST be content-sensitive: when a user
279
- * compiles two .c files (e.g. `clang a.c -o a` then `clang b.c -o b`)
280
- * the resulting .wasm binaries may differ by only a few bytes deep
281
- * inside the code section. The old fingerprint
282
- * `name + len + first + last` collided for such cases, returning the
283
- * same cache key and forcing a warm-isolate reuse that served the
284
- * FIRST binary's WebAssembly.Module on the second dispatch (verified
285
- * in prod: ./a and ./b were both 7572 bytes with identical first/last
286
- * bytes, differing only at offset 651 — clang-state-fix wave repro).
287
- *
288
- * Fix: hash the ACTUAL bytes via djb2 over the full content. Per-call
289
- * wasm is typically the user's compiled binary (KBs to a few MiB);
290
- * djb2 of a few MiB takes microseconds on the supervisor side and
291
- * runs once per dispatch (not per request — warm-reuse-on-match still
292
- * works for the legitimate "same bytes" case). The savings of NOT
293
- * hashing the wasm were marginal; the correctness cost was severe.
294
- */
295
- #fingerprintWasm(entries) {
296
- if (entries.length === 0)
297
- return '0';
298
- const parts = [];
299
- for (const w of entries) {
300
- const u = new Uint8Array(w.bytes);
301
- const len = u.byteLength;
302
- // djb2 over the bytes. Faster than crypto.subtle.digest at small
303
- // sizes, deterministic, and good enough for cache-key
304
- // disambiguation (NOT cryptographic — the loader cache doesn't
305
- // protect against malicious inputs).
306
- let h = 5381;
307
- for (let i = 0; i < len; i++) {
308
- h = ((h << 5) + h + u[i]) | 0;
309
- }
310
- parts.push(`${w.name}:${len}:${(h >>> 0).toString(36)}`);
311
- }
312
- return hashSource(parts.join('|'));
313
- }
314
- /**
315
- * Build the WorkerCode blob that the loader callback will return.
316
- * Same bytes every time for a given function and slot, allowing workerd
317
- * to reuse the isolate.
318
- *
319
- * Always prepends the ESBUILD_RUNTIME_SHIM so stringified functions that
320
- * reference esbuild-emitted helpers (__name, __defProp, etc.) don't
321
- * crash the facet with "__name is not defined". User preambles are
322
- * appended below the shim.
323
- */
324
- #buildCode(fnSource, perCallWasmEntries) {
325
- const workerOpts = {
326
- compatibilityDate: CF_COMPAT_DATE,
327
- compatibilityFlags: ['nodejs_compat'],
328
- // Inherit parent network so the facet can reach registry.npmjs.org.
329
- globalOutbound: undefined,
330
- env: this.bindings,
331
- };
332
- // ── WASM module imports ───────────────────────────────────────────
333
- // Each entry in `wasmModules` (constructor-time + per-call) is
334
- // registered in the LOADER's modules map (below) as
335
- // `{ wasm: ArrayBuffer }`. workerd compiles each during the
336
- // worker's module-load phase (eval permitted there) and the
337
- // standard ESM import binding receives the resulting
338
- // WebAssembly.Module. We expose them on `globalThis.__NIMBUS_WASM`
339
- // so the user fn can read them at request time without having to
340
- // re-import (the user fn is serialized via fn.toString and doesn't
341
- // carry import statements).
342
- //
343
- // Per-call entries (passed via NimbusLoaderCallOptions.wasmModules
344
- // — used by the wasm-runner shell command) are appended to the same
345
- // table. Naming collision with constructor entries is rejected
346
- // upstream in #materialisePerCallWasm so the import block here
347
- // doesn't have to deduplicate.
348
- const allWasmEntries = [
349
- ...this.wasmModules,
350
- ...(perCallWasmEntries ?? []),
351
- ];
352
- const moduleSource = assembleLoaderWorkerModuleSource({
353
- fnSource,
354
- preamble: this.preamble,
355
- wasmEntries: allWasmEntries,
356
- hasBindings: this.bindings !== undefined,
357
- });
358
- // Modules map: the entry worker.js source plus any wasm modules the
359
- // pool was constructed with. Workerd parses the modules map at
360
- // worker-load time and resolves the static `import` statements
361
- // we generated above against this map. The wasm-shape entry
362
- // (`{ wasm: ArrayBuffer }`) tells workerd to compile during the
363
- // module-load phase — the only phase where wasm code generation
364
- // is permitted in this deploy.
365
- //
366
- // Per-call entries are appended after constructor entries so the
367
- // map order matches the import order in the generated worker.js
368
- // (matters only for human-readable diffs; workerd doesn't care).
369
- const modules = { 'worker.js': moduleSource };
370
- for (const w of allWasmEntries) {
371
- modules[w.name] = { wasm: w.bytes };
372
- }
373
- return {
374
- compatibilityDate: workerOpts.compatibilityDate,
375
- compatibilityFlags: workerOpts.compatibilityFlags,
376
- mainModule: 'worker.js',
377
- modules,
378
- env: workerOpts.env,
379
- // globalOutbound: undefined = inherit parent network; omitting the key
380
- // from the returned object has the same effect (codegen treats
381
- // absence as inherit when the key is explicitly stated; here we keep
382
- // it absent to match the cloudflare-parallel semantics).
383
- };
384
- }
385
- /**
386
- * Dispatch a single task to the slot isolate. `slotIndex` picks which
387
- * warm isolate services the call; callers round-robin slots themselves.
388
- */
389
- async #dispatchSlot(fnSource, fnHash, slotIndex, args, resilience, perCallWasm) {
390
- // Per-call wasm fingerprint. Mixed into the cache key so two calls
391
- // with different bytes hit different slots (no cache poisoning).
392
- // For the common case (no per-call wasm) the fingerprint is '0',
393
- // which is bytes-stable so warm reuse is unaffected.
394
- const perCallWasmEntries = this.#materialisePerCallWasm(perCallWasm);
395
- const perCallWasmHash = this.#fingerprintWasm(perCallWasmEntries);
396
- // Cache key includes the short DO id so warm isolates are scoped to
397
- // ONE session. See the doIdShort field comment for why — without it,
398
- // a later session's pool reuses the warm worker from a previous
399
- // session (which still carries the old session's env.SUPERVISOR
400
- // binding), and writeBatch RPCs land in the wrong DO's VFS.
401
- const buildId = (generation) => `nfp:${this.tag}:${this.doIdShort}:${fnHash}:${this.preambleHash}:${this.wasmHash}:${perCallWasmHash}:slot-${slotIndex}:g${generation}`;
402
- let id = buildId(this.slotGenerations.get(slotIndex) ?? 0);
403
- const code = this.#buildCode(fnSource, perCallWasmEntries);
404
- // W5 Lever 5: record the dispatch so /api/_diag/memory shows the
405
- // last-facet-id even on a hang or silent kill. Bounded — single
406
- // slot updated on every dispatch.
407
- try {
408
- setLastFacetId(id, slotIndex);
409
- }
410
- catch { /* best-effort */ }
411
- const runOnce = async () => {
412
- // loader.get() is synchronous from the caller's POV; the callback
413
- // is only invoked on cache miss. We wrap the callback tightly so a
414
- // retry doesn't rebuild workerCode — that's already stable here.
415
- //
416
- // The returned `stub` is the cached worker reference. We deliberately
417
- // do NOT dispose it: loader.get() is designed for warm-slot reuse
418
- // across dispatches (same `id` returns the same cached worker),
419
- // and disposing would invalidate that cache.
420
- //
421
- // We USED to also dispose the per-dispatch `entrypoint` stub in a
422
- // finally block here (added in 3c47b44 to prevent QueueState::ACTIVE
423
- // during cold-start install). Empirically that broke dispatch on
424
- // every slot after the first: once the finally ran
425
- // `entrypoint[Symbol.dispose]()`, subsequent dispatches on the
426
- // same cached slot hung — SupervisorRPC.writeBatch logged
427
- // 'canceled', the DO-side _rpcWriteBatch completed OK, but the
428
- // pool never saw the result and every task stalled to the 60s
429
- // per-task timeout at 0/13 packages. Removing the per-dispatch
430
- // dispose restored 13/13 in ~2.3s in dev.
431
- //
432
- // The pool-level dispose() method below (also from 3c47b44) is
433
- // fine and stays — it only tears down the long-lived SUPERVISOR
434
- // binding stub once the whole pool is done, which does NOT
435
- // invalidate any in-flight slot's entrypoint reference.
436
- const stub = this.loader.get(id, async () => code);
437
- const entrypoint = stub.getEntrypoint();
438
- try {
439
- const out = await entrypoint.execute(...args);
440
- return out;
441
- }
442
- catch (err) {
443
- if (err instanceof Error) {
444
- throw new ExecutionError(err.message, err.stack);
445
- }
446
- throw new ExecutionError(String(err));
447
- }
448
- };
449
- const maxAttempts = 1 + resilience.retries;
450
- let lastError;
451
- let retriedCloneRefusal = false;
452
- let attempt = 0;
453
- while (attempt < maxAttempts) {
454
- try {
455
- if (resilience.timeoutMs > 0) {
456
- // Race runOnce() against a settable timer. CRITICAL: clear
457
- // the timer in a finally so the timer's reject closure (which
458
- // transitively roots `args` — i.e. the per-task payload sent
459
- // to the slot, including 28 MiB pre-bundle slices) doesn't
460
- // hold its references for the full timeoutMs after the race
461
- // settles.
462
- //
463
- // Before this fix: a facet OOM at t=0 left the slice rooted
464
- // for the remaining timeoutMs (default 60s for pre-bundle).
465
- // With concurrency=2, two consecutive OOMs could pin
466
- // ~56 MiB of slice memory in the supervisor heap for a full
467
- // minute — alongside an in-flight cirrus-real boot, that's
468
- // enough to push a shared isolate over the 128 MiB cap.
469
- // See plan in close-plan-2026-04-28.
470
- let timerId;
471
- try {
472
- return await Promise.race([
473
- runOnce(),
474
- new Promise((_, reject) => {
475
- timerId = setTimeout(() => reject(new TimeoutError(resilience.timeoutMs)), resilience.timeoutMs);
476
- }),
477
- ]);
478
- }
479
- finally {
480
- if (timerId !== undefined)
481
- clearTimeout(timerId);
482
- }
483
- }
484
- return await runOnce();
485
- }
486
- catch (err) {
487
- lastError = err instanceof Error ? err : new Error(String(err));
488
- const cause = classifyError(lastError);
489
- // W5 Lever 5: classify + record. We push on EVERY failed
490
- // attempt (not just the final retry-exhausted throw) so the
491
- // ring captures transient SQLITE_NOMEM / clone-refused
492
- // patterns that still ultimately succeed. Ring is bounded
493
- // (50 entries) so noise is self-limiting.
494
- try {
495
- recordFailure({
496
- at: Date.now(),
497
- phase: 'rpc',
498
- cause,
499
- rssEstimateBytes: 0, heapUsedBytes: 0,
500
- lruBytes: 0, inFlightBytes: 0,
501
- lastRpcFrame: getLastRpcFrame(),
502
- lastFacetId: { codeId: id, slotIndex, atMs: Date.now() },
503
- message: lastError.message,
504
- });
505
- }
506
- catch { /* fail-soft */ }
507
- if (cause === 'clone_refused' && !retriedCloneRefusal) {
508
- retriedCloneRefusal = true;
509
- const generation = (this.slotGenerations.get(slotIndex) ?? 0) + 1;
510
- this.slotGenerations.set(slotIndex, generation);
511
- id = buildId(generation);
512
- try {
513
- setLastFacetId(id, slotIndex);
514
- }
515
- catch { /* best-effort */ }
516
- // If the supervisor DO is stale, a newer loader still cannot
517
- // deserialize back into it; only recycling that DO heals the
518
- // reverse direction. This refresh targets the stale-loader case.
519
- continue;
520
- }
521
- if (attempt < maxAttempts - 1) {
522
- // 100 * 2^attempt, capped at 2s so retries don't compound waiting.
523
- const delay = Math.min(2000, 100 * Math.pow(2, attempt));
524
- await new Promise((r) => setTimeout(r, delay));
525
- }
526
- attempt++;
527
- }
528
- }
529
- if (maxAttempts > 1) {
530
- throw new RetryExhaustedError(maxAttempts, lastError);
531
- }
532
- throw lastError;
533
- }
534
- #prepare(fn) {
535
- const fnSource = serializeFunction(fn);
536
- const fnHash = hashSource(fnSource);
537
- return { fnSource, fnHash };
538
- }
539
- /**
540
- * Run `fn` once with `arg` on a slot isolate. Returns the result or
541
- * throws TimeoutError / RetryExhaustedError / ExecutionError.
542
- */
543
- async submit(fn, arg, opts) {
544
- const { fnSource, fnHash } = this.#prepare(fn);
545
- const resilience = this.#resolve(opts);
546
- return (await this.#dispatchSlot(fnSource, fnHash, 0, [arg], resilience, opts?.wasmModules));
547
- }
548
- /**
549
- * Run `fn` on every item in `items`, at most `concurrency` at a time,
550
- * pinned to stable slots so warm isolates are reused.
551
- *
552
- * Results are returned in input order. Failure handling per `onError`.
553
- */
554
- async map(fn, items, opts) {
555
- if (items.length === 0)
556
- return [];
557
- const { fnSource, fnHash } = this.#prepare(fn);
558
- return this.#mapInternal(fnSource, fnHash, items, opts);
559
- }
560
- /**
561
- * Same shape as `map`, but accepts a pre-serialized function source
562
- * string instead of a live function reference. Used by
563
- * `NimbusFanoutPool`'s peer-DO leg, where the function was already
564
- * serialized on the coordinator side and forwarded over RPC.
565
- *
566
- * The fnSource MUST be the output of `serializeFunction(fn)`
567
- * (typically forwarded directly from a coordinator RPC). Bytes-
568
- * stable invariants:
569
- * - `fnHash = hashSource(fnSource)` must be deterministic so
570
- * warm slots are correctly keyed.
571
- * - `fnSource` must NOT reference `this` — same rule as
572
- * `serializeFunction`.
573
- *
574
- * No fn-validation runs here (it already ran on the coordinator);
575
- * the peer trusts the caller to forward a valid serialization.
576
- */
577
- async mapSource(fnSource, items, opts) {
578
- if (items.length === 0)
579
- return [];
580
- const fnHash = hashSource(fnSource);
581
- return this.#mapInternal(fnSource, fnHash, items, opts);
582
- }
583
- async #mapInternal(fnSource, fnHash, items, opts) {
584
- const resilience = this.#resolve(opts);
585
- const concurrency = Math.max(1, Math.min(opts?.concurrency ?? this.concurrency, items.length));
586
- const onError = opts?.onError ?? 'throw';
587
- const settled = new Array(items.length);
588
- let cursor = 0;
589
- const runSlot = async (slotIndex) => {
590
- while (true) {
591
- const idx = cursor++;
592
- if (idx >= items.length)
593
- return;
594
- try {
595
- const value = (await this.#dispatchSlot(fnSource, fnHash, slotIndex, [items[idx]], resilience, opts?.wasmModules));
596
- settled[idx] = { ok: true, value };
597
- }
598
- catch (err) {
599
- const error = err instanceof Error ? err : new Error(String(err));
600
- if (onError === 'throw')
601
- throw error;
602
- settled[idx] = { ok: false, error };
603
- }
604
- }
605
- };
606
- await Promise.all(Array.from({ length: concurrency }, (_, slotIndex) => runSlot(slotIndex)));
607
- if (onError === 'null') {
608
- return settled.map((s) => (s.ok ? s.value : null));
609
- }
610
- if (onError === 'skip') {
611
- return settled
612
- .filter((s) => s.ok)
613
- .map((s) => s.value);
614
- }
615
- // onError === 'throw' — all slots succeeded.
616
- return settled.map((s) => s.value);
617
- }
618
- /**
619
- * Release any RPC stubs held by the pool. Call this once the caller
620
- * is done with the pool (post-`map`/`submit`) so the underlying
621
- * stubs don't linger in workerd's deferred-destruction queue.
622
- *
623
- * Primary target: the SUPERVISOR binding stub we minted at
624
- * construction time (via `ctxExports.SupervisorRPC({props})`). It's
625
- * a cross-isolate RPC stub — without explicit disposal it stays
626
- * referenced until the parent isolate's event-handler context
627
- * finishes, which during npm install means "until the whole install
628
- * completes" — long enough to accumulate alongside other leaked
629
- * stubs and trip the QueueState::ACTIVE fatal.
630
- *
631
- * Safe to call more than once; idempotent.
632
- */
633
- dispose() {
634
- if (!this.bindings)
635
- return;
636
- for (const key of Object.keys(this.bindings)) {
637
- disposeRpcResource(this.bindings[key]);
638
- }
639
- // Prevent double-dispose from re-running the loop.
640
- this.bindings = undefined;
641
- }
642
- }