ADR-0049: The diagrams worker: Excalidraw in headless Chromium, behind a relay¶
Status: Accepted Date: 2026-10-23 Deciders: Alex Nodeland
Records stackr's half of lattice RFC-0006's phase 2: the worker that artifactr's converter calls (artifactr ADR-0090). It is the worker artifactr ADR-0088 measured, and it is built and isolated as native files' workers are (ADR-0043, ADR-0046).
Context¶
- artifactr draws through a port,
Converter, with three calls: Mermaid to elements, skeletons to elements, and a diagram rendered as SVG or PNG.WorkerConvertercalls them over HTTP, with a token, and holds every reply to limits and checks (ADR-0090). Nothing answered it. - Only Excalidraw's own code draws as Excalidraw does.
parseMermaidToExcalidraw,convertToExcalidrawElementsandexportToSvgare browser code. ADR-0088 measured them in headless Chromium and in jsdom with resvg, and chose Chromium: its PNGs are right, and it is faster at the 2,000-element limit. - Font subsetting needs
'unsafe-eval'. Without it, an export embeds whole faces: an SVG of 299 kB against 15 kB. The portal's policy never allows it (ADR-0088), so a small export is the worker's. - Xiaolai, Excalidraw's CJK face, stays out of the worker's image until its author answers (lattice#679): it is OFL 1.1, but its README reserves the names Xiaolai and Kose, and Excalidraw's modified copy keeps the name. Without a CJK face, Chromium draws tofu.
Decision¶
The worker¶
- A Node service,
packages/stackr/workers/diagrams, in lattice's Bun workspace, sobun.lockpins everything it runs and ships.- Its server drives one warm headless Chromium through Playwright's driver,
playwright-core. - Chromium loads its page: Excalidraw 0.18.1 and
@excalidraw/mermaid-to-excalidraw2.2.2, the version Excalidraw itself depends on, built by Vite.
- Its server drives one warm headless Chromium through Playwright's driver,
- It answers
WorkerConverter's contract and nothing else:POST /mermaid,POST /skeletonsandPOST /render, each with the worker's token;GET /healthz, which says only Excalidraw's version, for Compose's health check;- a failure as a 4xx or a 5xx with
{reason, message}.
- One job at a time, within its deadline, at most 10 s whatever a call asks for.
- A job that waits past its deadline isn't started.
- A job that runs past it is answered
timeout. A job the page didn't answer, because the page or the browser failed, is answeredcrashed, in fixed words. After either, the page is thrown away, and a new one is made at once, in the background, so the next job finds it warm unless it comes within the few seconds a cold page takes. A browser that died is started again. invalidis the page's own refusal alone: Mermaid that doesn't parse, or skeletons Excalidraw can't convert, which the page answers with, in their words. Such a job, and atoo_largeone, leaves the page as it was.- The page is made at most once at a time: a job that gives up waiting for a cold page leaves no second one being made behind it.
- Its replies are what artifactr checks for (ADR-0090). Running the real worker's replies through artifactr's own
Convertedandchecked()found four things, each fixed in the worker; artifactr needed no change:- Mermaid names its nodes as it likes,
中文among them, and Excalidraw keeps the names as element ids. A Mermaid diagram's ids are made new, as the canvas's are. The agent's skeletons keep the ids it gave them, which artifactr holds to[A-Za-z0-9_-]. - A Mermaid
clickgives an element a link, which an export draws as<a href>. Converted elements keep no link. - Mermaid's pictures are SVGs, such as a Gantt chart's, with
foreignObjectand styles of their own. Each is drawn once, on a canvas in the page, and kept as a PNG. - An export names esm.sh for a face it couldn't embed, Xiaolai while the worker has none. Such a
@font-faceis dropped, and the viewer's own face draws those words. An SVG names no address outside itself.
- Mermaid names its nodes as it likes,
- The PNG is the SVG, drawn. Excalidraw's own PNG export draws with the faces the document loaded, which the page doesn't, so it fell back to a serif. Drawing the exported SVG gives the PNG the same embedded faces.
- A diagram is restored before it renders.
restoreElementsfills what an element leaves out with Excalidraw's defaults, as the canvas does.
The page, and 'unsafe-eval'¶
- The page's origin is
http://diagrams.invalid, which no network has. The server answers every request to it from the page's own files, through Playwright's routing, and refuses any other request, logging each origin once.- Playwright's routing sees the page's HTTP requests, not a WebSocket's or WebRTC's. Those the policy refuses (
connect-srcnames no other origin), and the container has no route out: each wall stands without the others.
- Playwright's routing sees the page's HTTP requests, not a WebSocket's or WebRTC's. Those the policy refuses (
-
Its policy is the only one in lattice with
'unsafe-eval':
'unsafe-eval'is there for font subsetting alone. The page runs Excalidraw's and Mermaid's code, and nothing the model wrote. What the model sends is data: Mermaid's source, skeletons and diagrams. Mermaid parses its source; it never runs it as code.- The worker's container has no route out either (below): the policy and the routing are a second wall, not the only one.
The image¶
diagrams-worker, a target of the template's Dockerfile, in two stages.diagrams-buildis the Bun lattice uses, pinned by digest.scripts/build-diagrams-worker.shclones lattice at the commituv.lockpins artifactr at, so the worker and the converter that calls it come from one commit. It runsbun install --frozen-lockfileand builds the page and the server.diagrams-workeris Playwright's image,mcr.microsoft.com/playwright:v1.64.0-noble, pinned by digest. Its Chromium is the one Playwright 1.64.0 drives, and the worker'splaywright-coreis 1.64.0.
- It runs as user 10005, with
HOMEin its tmpfs. - Noto CJK draws CJK. The image installs Debian's
fonts-noto-cjk(SIL OFL 1.1), and removes the image's other CJK faces, WenQuanYi Zen Hei and IPAGothic, so Noto is the one it draws with. Xiaolai is left out of the page's faces unless the build argumentXIAOLAIis 1, which.env'sDIAGRAMS_XIAOLAI=1sets, and which waits for its author's answer (lattice#679). - The build checks its Playwright. The worker's
playwright-corecomes from the lattice commituv.lockpins; the image's Chromium, from the template. The Dockerfile'sARG PLAYWRIGHTnames the image's version, and the build fails, saying so, if the worker it built drives another: an application whose libraries and template were updated apart can't run a Chromium its driver doesn't know. - The commit must hold the worker. An application whose
uv.lockpins artifactr at a commit before this record's has nopackages/stackr/workers/diagramsto build, and the build says to update the libraries. Generated from the template once this record is in lattice, an application's lock pins a commit that has it. - Its notices are beside its page:
THIRD-PARTY-NOTICES.txt, which portalr's notices plugin writes from the page's bundle, under a policy of the worker's own (policy.json);FONTS.txt, with the faces' licences: the OFL's text and Noto CJK's copyright, as Debian records them.
- About 1 GB: Playwright's image, which carries Firefox and WebKit too, which the worker never runs.
In the template¶
- A question,
diagrams, default yes, asked with native files: a conversion's images are kept in native files' store. - The worker runs only with
DIAGRAMS=on, in.envormake up DIAGRAMS=on, in a Compose profile of its own,diagrams.make envgenerates its token,DIAGRAMS_TOKEN.make uptells it the ID of its image, asWORKER_BUILD. - ADR-0046's relay pattern.
- The worker,
diagrams-worker, is alone on its network,diagrams. That network is internal, with no address for the host (inhibit_ipv4). - Its relay,
diagrams, is socat. It is ondiagramsand onfiles, where the application reaches it.
- The worker,
- The container is a native file's worker's, with its own limits:
- read-only, no capabilities, no new privileges, under an init;
- 1 GiB of memory and no swap (
DIAGRAMS_MEMORY); - one CPU (
DIAGRAMS_CPUS); - 256 processes, for Chromium's;
- a 256 MiB tmpfs that runs nothing.
- The application's side.
FilesregistersDiagram, its checks and its integration key,excalidraw, whether the worker runs or not, so diagrams already made stay readable.- With
DIAGRAMS=on, it gives the agentdiagram_tools, over aWorkerConverterathttp://diagrams:8080, with native files' store asblobs. - Native files' collection is told about diagrams (
naming=(Diagram,)), so it keeps their images (ADR-0090). - The tools are offered only where
excalidrawis on, and it is off in every workspace until switched on (lattice-core'sKNOWN).
- Diagrams without forms. The template's
diagramsdoesn't need itsui: the application gates every artifact by its integration either way (integration_gate(integrations, files.key_of)), soexcalidrawis a switch in both.
One Excalidraw¶
versions.envnames it:EXCALIDRAW_VERSION=0.18.1.scripts/check-templateholds these to one another:- the worker's
@excalidraw/excalidrawto that version; - its
@excalidraw/mermaid-to-excalidrawto the version Excalidraw depends on, read frombun.lock; - the Dockerfile's Playwright image to the worker's
playwright-core.
- the worker's
- portalr's half of the check comes with the canvas (phase 3). Any of portalr's UIs that depends on
@excalidraw/excalidrawis already held to the same version.
Options considered¶
For the image¶
| Playwright's image, by digest (chosen) | A slim Node image, and playwright install --with-deps chromium |
Chromium from Debian | |
|---|---|---|---|
| Size | About 1 GB, with two browsers it never runs | About 600 MB | Smaller still |
| What pins Chromium | The image's digest | Playwright's version, then a download from Playwright's CDN at build time, with no digest | Debian's version, not the one Playwright drives |
| Its system libraries | Playwright's own list, tested | The same list, installed at build | Debian's |
A download with no digest would be the only part of the image not pinned by one. Chromium from Debian would differ from the one Playwright 1.64.0 is tested against.
For where the worker's code lives¶
- In stackr, in lattice's Bun workspace (chosen): one lock, scanned by osv-scanner with the rest; Biome, TypeScript and knip check it in CI's Check job.
- In the template, as files copied into each application: each application would carry Excalidraw's tree and a lock of its own, which no lattice check would see.
- In a repository of its own: a second release to keep in step with artifactr's converter, which a commit pinned in
uv.lockkeeps in step for free.
For where 'unsafe-eval' lives¶
- The worker's page alone (chosen): the page runs no code the model wrote, and has no route out.
- The portal's policy: every page of the application would allow it, for an export that the worker makes smaller anyway (ADR-0088).
Trade-off analysis¶
The worker costs an image of about 1 GB and 1 GiB while it runs, for exports that are Excalidraw's own, faces and all, and PNGs the agent can see. ADR-0046's relay costs one small container, and buys what it bought native files: a worker that reaches nothing of the application. Building the worker from the commit artifactr is pinned at costs a clone of lattice at build time, as portalr's UIs already do. It keeps the worker and its client one revision.
What is still not isolated¶
- Chromium's own sandbox is off, as Playwright launches it by default: the container is the boundary. The page runs only Excalidraw's and Mermaid's code, on data.
- One page serves every tenant's jobs, one at a time, and is thrown away only after one fails or runs out of time. A job leaves no diagram in the page's document. Excalidraw's caches, of faces and of the shapes it has drawn, outlive a job.
- With diagrams on and no native file's app running, no
NativeFilesexists, so nothing collects the store: the images of a drawing that was never accepted stay in it. /healthzanswers without the token, with Excalidraw's version and nothing else, on the worker's own network.- The relay takes at most 16 connections at once, shared by every tenant, as native files' do. The worker runs one job at a time, so a burst queues, each job within its own deadline. No tenant has a share of its own yet (ADR-0088).
- What floats. The images are pinned by digest. Noto CJK, and the packages it pulls, float with Ubuntu's archive, as the template's other apt packages do.
- Licences (amended 2026-10-24, lattice#679): each face's is read from its pinned upstream, and
FONTS.txtcarries the notices and pins. Liberation Sans is Liberation 2.1.5 (SIL OFL 1.1), copied over Excalidraw's 1.05, whose name table refers to Ascender's licence agreement. Not verified:- Excalifont, which has no standalone licence file upstream, only its name table's text in Excalidraw's source;
- whether Excalidraw's modified Xiaolai may keep the name its author reserves, which keeps it out;
- the WebAssembly modules subsetting runs: harfbuzz and woff2.
Measured¶
On the scoped stack, with the image from this record on Docker Desktop's Linux VM, Apple silicon, through the relay:
| An agent's two drawings from Mermaid, a flowchart with CJK words and a Gantt chart, proposed | 4.2 s, the Gantt chart one PNG of about 8 kB |
| 2,000 elements, SVG / PNG | 1.28 s / 1.32 s |
| A 2,000-element PNG with a 0.3 s deadline | timeout after 0.72 s; the next job, on a new page, 5.7 s |
| Memory after those jobs | 586 MiB of 1 GiB; not killed, not restarted |
Consequences¶
- Easier: the agent draws in Excalidraw's own shapes, and sees its drawing as a PNG. An export is small, and names nothing outside itself. One version of Excalidraw draws in the worker and, with phase 3, in the canvas.
- Harder: a running application with diagrams is two more containers, and an image of about 1 GB, whose first build clones lattice. Upgrading Excalidraw means re-running ADR-0088's method, with
versions.env, the worker and the canvas moved together. - Harder: Renovate bumps
@excalidraw/*andplaywright-corein the worker'spackage.jsonalone, andscripts/check-templatethen fails untilversions.env, and the Dockerfile's Playwright tag and digest, move with them, as.prototoolsand the portal's Bun do. - To revisit:
- a per-tenant share of the worker;
- a slimmer image, once Playwright publishes one with Chromium alone.
Action items¶
- The worker, its page and its server, in lattice's Bun workspace.
- The template's
diagramsquestion, the image's targets, the relay and the profile. -
scripts/check-template's pins, andscripts/check-files' posture, egress and probe of the worker. - Xiaolai, behind
XIAOLAI=1, once its author answers lattice#679. - portalr's canvas, held to
EXCALIDRAW_VERSION(phase 3). - The tombstone sweep, run on a schedule by the application (artifactr ADR-0091).