@ai-ecoverse/slicc-kernel 1.183.0 → 1.184.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 (60) hide show
  1. package/README.md +96 -0
  2. package/dist/async-process.js +14060 -0
  3. package/dist/async-process.js.map +7 -0
  4. package/dist/cloudflare-sources.js +2 -0
  5. package/dist/cloudflare.js +42562 -0
  6. package/dist/cloudflare.js.map +7 -0
  7. package/dist/driver-worker.js.map +2 -2
  8. package/dist/index.js.map +2 -2
  9. package/dist/kernel-worker.js +134 -39
  10. package/dist/kernel-worker.js.map +4 -4
  11. package/dist/node-driver-worker.js.map +2 -2
  12. package/dist/node-process-worker.cjs +3985 -574
  13. package/dist/node-process-worker.cjs.map +4 -4
  14. package/dist/node-process-worker.js +3985 -574
  15. package/dist/node-process-worker.js.map +4 -4
  16. package/dist/node.js +1478 -193
  17. package/dist/node.js.map +4 -4
  18. package/dist/process-worker.js +3901 -490
  19. package/dist/process-worker.js.map +4 -4
  20. package/dist/types/async/header-hook.d.ts +3 -0
  21. package/dist/types/async/link.d.ts +14 -0
  22. package/dist/types/async/message-link.d.ts +15 -0
  23. package/dist/types/async/node-host.d.ts +16 -0
  24. package/dist/types/async/remote-worker.d.ts +12 -0
  25. package/dist/types/async/snapshots.d.ts +13 -0
  26. package/dist/types/async/transport.d.ts +28 -0
  27. package/dist/types/async/wire.d.ts +113 -0
  28. package/dist/types/cdp/port.d.ts +2 -1
  29. package/dist/types/cloudflare/clock-skew.d.ts +2 -0
  30. package/dist/types/cloudflare/computer-driver.d.ts +65 -0
  31. package/dist/types/cloudflare/computer.d.ts +105 -0
  32. package/dist/types/cloudflare/fork-tokens.d.ts +17 -0
  33. package/dist/types/cloudflare/images.d.ts +20 -0
  34. package/dist/types/cloudflare/isolates.d.ts +17 -0
  35. package/dist/types/cloudflare/kernel-end.d.ts +45 -0
  36. package/dist/types/cloudflare/loader.d.ts +54 -0
  37. package/dist/types/cloudflare/options.d.ts +81 -0
  38. package/dist/types/cloudflare/placement.d.ts +61 -0
  39. package/dist/types/cloudflare/rpc-link.d.ts +49 -0
  40. package/dist/types/cloudflare/rpc-pack.d.ts +7 -0
  41. package/dist/types/cloudflare/sqlite-meta.d.ts +4 -0
  42. package/dist/types/cloudflare/sqlite-root.d.ts +5 -0
  43. package/dist/types/cloudflare/sqlite-store.d.ts +73 -0
  44. package/dist/types/cloudflare.d.ts +16 -0
  45. package/dist/types/facade/port.d.ts +2 -1
  46. package/dist/types/kernel/host.d.ts +1 -0
  47. package/dist/types/kernel/process.d.ts +2 -0
  48. package/dist/types/kernel/protocol.d.ts +1 -0
  49. package/dist/types/launcher.d.ts +16 -0
  50. package/dist/types/mount/hostfs.d.ts +2 -1
  51. package/dist/types/mount/in-process.d.ts +15 -0
  52. package/dist/types/node.d.ts +14 -0
  53. package/dist/types/ports.d.ts +7 -0
  54. package/dist/types/process/wasi/wasi-files.d.ts +1 -0
  55. package/dist/types/process/wasi/wasi-host.d.ts +3 -2
  56. package/dist/types/process/wasi/wasi-module.d.ts +2 -0
  57. package/dist/types/process/wasi/wasi-sigmask.d.ts +2 -1
  58. package/dist/types/process/wasi/wasm-reentry.d.ts +1 -0
  59. package/dist/types/realm/sync-sab-bridge.d.ts +1 -0
  60. package/package.json +10 -4
package/README.md CHANGED
@@ -262,6 +262,101 @@ By default `nodeTransport()` refuses destinations on the host itself, because No
262
262
  - **The opt-in:** `nodeTransport({ hostLoopback: true })` allows the host's loopback except ports 9222–9229. `nodeTransport({ hostLoopback: [8080, 9222] })` allows exactly the listed ports, so DevTools ports are reachable only when named. Link-local stays refused either way.
263
263
  - **Connect-time enforcement:** the kernel also checks the address each connection actually reaches, before anything is sent, so a name whose DNS answer changes after the check is still refused. A keep-alive connection opened outside this kernel's policy (by the host application, or by another kernel in the same process whose `hostLoopback` allows it) to the same origin could be reused without a new check. When a proxy is in use (`HTTP_PROXY`/`HTTPS_PROXY` with `--use-env-proxy` or `NODE_USE_ENV_PROXY=1`), a connection opened to the proxy's own host and port is allowed even on the host's loopback, and so is any request later tunnelled over that same live TCP connection (matched by its local and remote address and port, and forgotten when it closes), so only the pre-check applies to the target; a direct connection, a `NO_PROXY` host's included, is checked as usual, and a proxy that resolves names itself closes the rebinding window on its side.
264
264
 
265
+ ### In a Cloudflare Durable Object
266
+
267
+ `@ai-ecoverse/slicc-kernel/cloudflare` runs the kernel inside a Durable Object, with its processes in Dynamic Workers. It is experimental: the API can change, and the limits below are real.
268
+
269
+ ```js
270
+ import { DurableObject } from 'cloudflare:workers';
271
+ import { createCloudflareKernel } from '@ai-ecoverse/slicc-kernel/cloudflare';
272
+
273
+ export class Kernel extends DurableObject {
274
+ now() {
275
+ return Date.now();
276
+ }
277
+
278
+ kernel() {
279
+ const { storage } = this.ctx;
280
+ this.booting ??= createCloudflareKernel({
281
+ loader: this.env.LOADER,
282
+ sql: storage.sql,
283
+ sqlite: { transaction: (fn) => storage.transactionSync(fn) },
284
+ clock: () => this.env.KERNEL.get(this.ctx.id).now(),
285
+ });
286
+ return this.booting;
287
+ }
288
+
289
+ async fetch() {
290
+ const kernel = await this.kernel();
291
+ return Response.json(await kernel.run(['bash', '-c', 'ls / | head -3'], { cwd: '/root' }));
292
+ }
293
+ }
294
+ ```
295
+
296
+ The Worker needs a Worker Loader binding (`worker_loaders`) and a SQLite-backed Durable Object class. Packages are installed as on Node, with `kernel.writeFile` under `/node_modules`; [`examples/cloudflare`](examples/cloudflare) bundles bash and coreutils with the Worker and installs them on first boot, and [`examples/cloudflare-computer`](examples/cloudflare-computer) adds a [Cloudflare Computer](https://github.com/cloudflare/computer) Workspace.
297
+
298
+ `createCloudflareKernel(options)` returns a kernel with `run`, `openTerminal`, `writeFile`, `readFile`, `chmod`, `connect`, `users`, `groups`, `setRoutes`, `mount`, `umount`, `mounts` and `terminate`, as `createNodeKernel` does. Its own options:
299
+
300
+ | option | default | |
301
+ | --- | --- | --- |
302
+ | `loader` | required | the Worker Loader binding |
303
+ | `sql`, `sqlite` | none | the Durable Object's `ctx.storage.sql`, and `{ transaction, writeBehind }`: the root file system and its POSIX metadata live in tables prefixed `slicc_` |
304
+ | `root`, `meta`, `ranged` | `memoryRoot()` | a root of your own in place of `sql` |
305
+ | `clock` | none | called before every timed wait; pass a call into the Durable Object's own namespace, as above. Without it, `sleep` and timeouts can end early |
306
+ | `isolates` | `{ max: 8, waitMs: 10000, memory: 64 MiB, perInstance: 8 MiB }` | how many Dynamic Workers run at once, how long a spawn waits for one, and the memory budget per Dynamic Worker |
307
+
308
+ **How it works.** Workers has no `Worker`, refuses `Atomics.wait`, and does not share memory between isolates, so a process cannot block the way it does in a page. Here a process is a wasm instance in a Dynamic Worker, parked with JSPI (`WebAssembly.Suspending`) while its system call is answered by the Durable Object over Workers RPC.
309
+ - **The kernel side is the same code as in the page.** A proxy in the Durable Object speaks the kernel's shared-memory protocol on behalf of each remote process (`src/async/remote-worker.ts`).
310
+ - **The process side is derived at build time.** `tools/derive-async` turns the synchronous process runtime (`src/process/wasi`, `src/process/emscripten`) into an async one, `dist/async-process.js`: functions that can reach a kernel call become `async`, and wasm is entered through `WebAssembly.promising`. The build fails where it cannot (a kernel call in a constructor, a getter, or a callback passed to a synchronous built-in).
311
+ - **Emscripten programs run without their glue.** Workers refuses `eval`, and Emscripten's JavaScript file system layer cannot be suspended by JSPI. So the kernel implements the Emscripten import ABI itself (`src/process/emscripten`). `createNodeKernel({ emscripten: 'kernel' })` selects the same on Node; the default there and in the page stays the package's glue.
312
+ - **Programs are Worker modules.** Workers also refuses `WebAssembly.compile` at run time, so each program's wasm is handed to the Worker Loader, one Dynamic Worker per program, with many processes in it. A fork's memory snapshot stays inside that Dynamic Worker.
313
+
314
+ `createNodeKernel({ blocking: 'async', emscripten: 'kernel' })` runs the same async runtime on Node, for tests.
315
+
316
+ **Limits.**
317
+ - **Not supported:** threads (`pthread_create` fails with `EAGAIN`), `dlopen` and anything else that compiles wasm at run time, JS programs (`"abi": "js"`), and packages with an `imports` module. Each fails with an errno or a message, never a hang.
318
+ - **Memory:** a Dynamic Worker has 128 MB. A program whose wasm memory starts above that cannot run (`python.wasm` declares 160 MiB). Processes of one program share a Dynamic Worker up to the budget and then spill into another; at most 8 Dynamic Workers run at once, and a spawn beyond that waits, then fails with `EAGAIN`, which bash retries. A Dynamic Worker that outgrows its limit is ended by Cloudflare: its processes exit with status 70 and the kernel carries on.
319
+ - **CPU and signals:** a process that computes for 30 s without a system call is ended by Cloudflare. Nothing can interrupt it sooner: a signal, `SIGKILL` included, is seen at the process's next system call.
320
+ - **Clocks:** inside a Workers request the clock stands still while code computes. `time` and CPU accounting are wrong, and a timed wait that follows a long computation in the same process can end early by that computation's length.
321
+ - **Durability:** the SQLite root writes behind. Every `run()`, `writeFile()`, `chmod()`, terminal exit and client reply, and every `fsync` or `fdatasync`, makes all earlier writes durable. A program that never syncs can lose, on an unclean stop of the Durable Object, what it wrote since the last of those points: at most the last second, or 50 ms after its last write if it then went quiet. What survives is always a consistent tree. `sync` without a file does nothing, because Emscripten's libc compiles it away. `sqlite: { writeBehind: false }` commits every change at once, at about 25 ms per change.
322
+ - **Hard links** are refused on the root, as on every root.
323
+ - **Speed:** a system call costs about 0.3 ms and starting a process about 50–90 ms.
324
+
325
+ Measured on a deployed Worker, from a client about 40 ms away, next to local `wrangler dev`:
326
+
327
+ | | `wrangler dev` | deployed |
328
+ | --- | --- | --- |
329
+ | cold boot (SQLite root; bash, coreutils and ripgrep installed) | 67 ms | 736 ms |
330
+ | first `bash -c 'echo hi'` | 16 ms | 305 ms |
331
+ | warm `echo hi` | 9 ms | 100 ms |
332
+ | `echo a \| cat; x=$(uname); ls / \| head -3; for i in 1 2 3; do echo $i; done \| wc -l` | 100 ms | 865 ms |
333
+ | 1,000 `echo` lines | 87 ms | 506 ms |
334
+ | 20 × `$(echo x)` | 100 ms | 1,036 ms |
335
+ | 16-stage `cat` pipeline | 243 ms | 3,677 ms |
336
+ | 100 redirections into new files | 244 ms | 1,226 ms |
337
+ | `rg -l` over 200 files | 203 ms | 1,255 ms |
338
+
339
+ #### As a Cloudflare Computer backend
340
+
341
+ `sliccBackend` makes the kernel a `WorkspaceBackend` of `@cloudflare/computer`: `workspace.runtime.exec(source, { backend: 'slicc' })` runs `bash -c` with GNU bash and the installed packages, on the Workspace's own files, mounted at `/workspace`.
342
+
343
+ ```js
344
+ import { Workspace } from '@cloudflare/computer';
345
+ import { createCloudflareKernel, sliccBackend } from '@ai-ecoverse/slicc-kernel/cloudflare';
346
+
347
+ const workspace = new Workspace({
348
+ storage: ctx.storage,
349
+ sessionId: ctx.id.toString(),
350
+ backends: [sliccBackend({ kernel: () => kernel })],
351
+ });
352
+ const run = await workspace.runtime.exec('ls -la /workspace | wc -l', { backend: 'slicc' });
353
+ ```
354
+
355
+ Output is streamed, `killExec` signals the whole process group, and a timeout sends `SIGTERM` and then `SIGKILL`. The kernel's own root (`/root`, `/etc`, `/tmp`, `/node_modules`) lives in the same SQLite database as the Workspace, in its own tables.
356
+ - **What the Workspace store does not have:** owners (files show as uid 0 and `chown` fails), `utimes` (`touch -d` does nothing), hard links, and ranged writes (a write that seeks backwards rewrites the file).
357
+ - **Write-behind:** a file is committed to the Workspace up to 1 s after it is closed, and always before the run's exit event. `workspace.fs` can see a file that late during a run, and an error in a deferred commit is not reported to the program that wrote the file. `sliccBackend({ writeBehind: false })` commits at close.
358
+ - **Deletes are slow:** each removed file is one durable commit, about 30 ms on a deployed Worker.
359
+
265
360
  ## Commands
266
361
 
267
362
  Commands come from installed packages in npm's `node_modules` layout: every `<modules>/<name>/package.json` and `<modules>/@scope/<name>/package.json` with a `slicc.commands` block. An Emscripten program names its glue and module, for example bash's:
@@ -1499,6 +1594,7 @@ The kernel is ported from SLICC's `packages/webapp/src/kernel/` with the browser
1499
1594
  - `src/cdp/`: the CDP facade on `127.0.0.1:9222`, its WebSocket framing, the registry of hosts and the `MessagePort` bridge to them.
1500
1595
  - `src/sudo.ts`, `src/sudoers.ts`, `src/sudo-command.ts`: the kernel side of `sudo` and `visudo -c`, the policy parser (both line forms and their diagnostics), and the two commands.
1501
1596
  - `src/time-zone.ts`: the system time zone (`timeZone`, or the browser's) and UTC offsets of a zone, which cron's schedules and `TZ` both come from.
1597
+ - `src/async/`, `src/process/emscripten/`, `src/cloudflare.ts`, `src/cloudflare/` and `tools/derive-async/`: the kernel [in a Cloudflare Durable Object](#in-a-cloudflare-durable-object). `src/async/` is the link between the kernel and a process that shares no memory with it; `src/process/emscripten/` the kernel's own Emscripten imports; `src/cloudflare/` the Durable Object host, the SQLite root and the Cloudflare Computer backend; `tools/derive-async/` the build step that derives the async process runtime. Their tests are in `test/integration/cf/` and run in Node, with `node --test --test-concurrency=2 'test/integration/cf/*.test.mjs'`; those named `cloudflare-*` and `computer-cloudflare-*` start `wrangler dev`, and run against a deployed Worker when given its URL and key. `npm test` and CI run them with the rest. On a pull request, CI's `deploy` job runs `tools/cf-staging.mjs`, which deploys `examples/cloudflare-computer` as the Worker `slicc-kernel-do-pr-<number>` with a fresh random key, and hands its address to the test shards and the reviewers as `STAGING_URL` (`https://…workers.dev/?key=…`); `computer-cloudflare-staging.test.mjs` then runs the Computer example's tests against it. That address, key included, shows in the run's log and on the pull request, so the Worker stops answering 24 hours after its deploy, and its programs have no network. The job needs the secret `SLICC_KERNEL_CLOUDFLARE_API_TOKEN` (a Cloudflare API token that may edit Workers scripts; `SLICC_KERNEL_CLOUDFLARE_ACCOUNT_ID` too when the token sees several accounts). Without it, on a fork, in the merge queue, or when the deploy fails, there is no staging Worker and that test file skips. Each deploy deletes the staging Workers of other pull requests that no deploy has touched for a week.
1502
1598
  - `src/cron/`: the cron service (`service.ts`), the schedule and table parsers, the clock, and the `crontab` command with the kernel side of `ctx.crontab`. An embedder that uses `Launcher` directly passes `cron: { lock }` to share the firing lock among the kernels on one root; the default, `slicc-kernel-cron`, is one lock for the whole origin.
1503
1599
 
1504
1600