Skip to main content

Module worker

Module worker 

Source
Expand description

The code-mode worker process (a host binary started with its worker flag). The code-mode worker: one restricted JavaScript execution in a process of its own, on Boa. A host binary becomes this process when it is started with the hidden worker flag (BREP_mcp_core cad::code::worker_entry).

The host that starts this process owns it: it watches the process’s CPU time, memory and age, and KILLS it — on an agent’s halt or a limit — which stops any running script, however it loops. Nothing in the host’s address space is reachable from the script, and a killed worker leaves nothing behind but the CAD operations it had already handed to the service.

A program is a plain script body with top-level await and return; the returned value is its result. The worker blanks one leading import … from "@brep/cad" line (strip_sdk_import) and runs the body as export default async function main() { BODY } (module_source). sdk is a global built from the names the host granted: each sdk.<namespace>.<action>(body) is one host call, and a name that is not there throws saying which names are close.

Protocol, one JSON object per line:

  • stdin, first line: {"names", "limits", "source"} — the granted <namespace>.<action> names, the execution’s limits (the HOST enforces them: Boa has no CPU or heap quota of its own) and the program;
  • stdin, then: {"call": id, "ok": true, "value": json} or {"call": id, "ok": false, "error": record} — a host call’s answer;
  • stdout: {"call": id, "method": "<namespace>.<action>", "body": json} — a host call, answered by a Promise that resolves with the value or rejects with an ApiFailure error carrying the record as .error; {"log": line}; and last {"done": {"ok": json}} or {"done": {"error": {"code", "message"}}}.

Nothing is importable; there is no filesystem, network, process, timer or environment binding.

The same protocol drives the browser’s code worker (a Web Worker running this crate in the app’s wasm, messages in place of lines): Realm is the event-driven core both share — Realm::begin runs the program until it waits on the host or ends, Realm::answer delivers one host reply and runs on. main is that core over stdin and stdout.

Structs§

Realm
One execution, driven by its host’s messages.

Constants§

MAIN_OPEN
What a program body is wrapped in. The opening shares the body’s first line, so the rows a parser names are the program’s own rows.

Functions§

main
Run one execution reading stdin and writing stdout; the process exits after the done line.
module_source
The ES module a program body runs as: its top-level await and return are those of an async main. The closing brace has a line of its own, so a body that ends in a // comment keeps it.
strip_sdk_import
Blank one leading import … from "@brep/cad" statement (the first line that is not empty or a // comment), for clients that write modules: sdk is a global either way. The statement becomes spaces, so every row and column after it is unchanged. Any other program is returned as it is.
syntax_help
A parse error made actionable: the parser’s message, the source lines it points at with a caret, and the fix for the mistakes that produce it when a program travels inside JSON (a \n escape decoded to a real line break inside a quoted string; a whole program escaped twice; a module written where a script body is expected). source is the program body and first_row_shift how far the parser’s columns on its first row are from the body’s (MAIN_OPEN shares that row).