# Live engine protocol This is a reference for people working on rtex itself. Hosts never speak this protocol; they use the session API ([embedding.md](embedding.md)), and the session talks to the engine. The live engine is one `lualatex` process. It has loaded the project's preamble and runs a loop inside `\begin{document}`. Requests go in on one channel and framed responses come back on another. stdout cannot be used, because LuaTeX prints its banner there even in batch mode. ## Starting the engine The host writes a driver file, `/rtex-serve-g.tex`: 1. `\input` of `rtex-preamble.tex`: the project's preamble, i.e. everything before `\begin{document}`, with preamble `\input` files inlined and body setup statements appended. 2. A few allocations (`\rtexbox`, a loop counter, a catcode table with `@` as a letter), then `\begin{document}`. Every `AtBeginDocument` hook (fontspec, microtype, hyperref) has run by now. 3. `\input{rtex-serve-patches}`: the server-only shortcuts (see below). 4. `\directlua` loading `rtex-serve.lua` and calling `rtex_serve.init(...)`, which opens the channel and sends the `ready` frame. 5. The serve loop: `\loop\rtexstep\ifnum\rtexcontinue>0 \repeat`, then `\end{document}`. It runs it as: ``` lualatex -interaction=batchmode --output-directory= /rtex-serve-g.tex ``` with the project directory as the working directory and this environment: | Variable | Purpose | |---|---| | `RTEX_RESP` | where the server writes responses: a FIFO on Unix, a named pipe `\\.\pipe\rtex---resp` on Windows | | `RTEX_REQ` | Windows only: the request pipe `\\.\pipe\rtex---req` | | `TEXINPUTS`, `LUAINPUTS` | prepend rtex's `tex/latex//` and `tex//` | | `openout_any=a` | allow writing to the response channel by absolute path | | `max_print_line=100000` | keep log lines unwrapped for the diagnostics parser | | `RTEX_MATH_OWNER_ATTRIBUTE` | optional configured attribute number; enables math ownership observation in live and capture processes | | `RTEX_TRACE` | optional: a file that gets one flushed line per request stage | Before `\begin{document}` the driver copies the latest layout's `.aux` and `.bbl` next to itself as `rtex-serve-g.aux`/`.bbl`. Packages that decide their mode from the aux (natbib, hyperref) and biblatex's citations then start in the document's state. ### Channels On **Unix**, requests go to the server's stdin and responses come back on a FIFO. The host opens the FIFO's read end once, non-blocking, before the server gets there. It polls until the first frame arrives, then switches to blocking reads. Closing and reopening the FIFO would lose frames. On **Windows**, the host creates two named pipes before spawning the server. Each has one instance, accepts local clients only, and is not inheritable. The server opens the request pipe in binary mode (`io.open(path, "rb")`) and then the response pipe. Stdin is not used because it is in text mode on Windows: CR LF in a source would become LF, which breaks the byte counts, and Ctrl-Z would end the input. On macOS and Windows, a reader thread on the host drains the response channel with blocking reads and passes whole frames to the engine thread over a queue. Nothing polls. On macOS, `poll` did not report a frame that arrived while it waited, and a FIFO there holds less than a large display list. The server blocked on its write while the host waited out the watchdog. Draining continuously means the server can always finish writing. On Linux, `poll` does report a FIFO that becomes readable while it waits, so the thread waiting for a reply reads the FIFO itself, without blocking, and polls when nothing has arrived. Every frame is the reply to a request, and the host reads whenever it waits for one. The host also sets the FIFO's size to 1 MB, the limit for unprivileged processes, so the server finishes writing a result even while the host is busy elsewhere; only a larger one waits for the host's next read. This saves the hand-off to a reader thread: a thread wake-up on every result. ## Requests A **compile** is a raw frame: ``` C \n C \n ``` `` names a context sent earlier. The optional JSON lists picture cache entries for picture environments in the source, each with the 1-based source `line` its `\begin` is on. A picture with an entry is placed from the cache, and one without is drawn. Everything else is one JSON object per line: | `op` | Fields | Reply | |---|---|---| | `context` | `id`, `ctx` (below) | `{"op":"ok","id":…}` | | `labels` | `labels`: `[name, body]` pairs from the last pass's aux (`r@