Skip to content

ADR-0090: Diagrams drawn by the agent, through a converter

Status: Accepted Date: 2026-10-23 Deciders: Alex Nodeland Implements: the agent's part of lattice RFC-0006's phase 2 (O6, O7), on ADR-0086's data and ADR-0088's worker

Amended (2026-10-23) by portalr ADR-0072: describe reads only a string as words, so a text or a type that isn't one reads as "", and only a number, not a boolean, as a place; and it reads a diagram's elements in JavaScript's order of an object's keys, array indexes first, as numbers. portalr's port of it reads them so too, held to it by a shared fixture.

Context

ADR-0086 made a diagram an artifact, edited by element-scoped patches. The agent couldn't draw one. RFC-0006 has it write Mermaid or skeletons, which Excalidraw's own parseMermaidToExcalidraw and convertToExcalidrawElements turn into elements. Those run in a browser, so the conversion runs in a worker: headless Chromium, one job at a time, a 10 s deadline, no way out (ADR-0088). The worker's image is stackr's, in the next phase-2 change; this record is the application's side of it.

Decision

  • A Converter port with three calls: mermaid(source), skeletons(skeletons) and render(diagram, files, as_=svg|png). Each fails only with ConversionFailed, whose reason is one of:
    • invalid: what the agent sent;
    • unsupported: a kind it doesn't draw;
    • too_large: past a limit;
    • timeout: past the deadline;
    • crashed: anything else.
  • WorkerConverter calls the worker over HTTP, as native files' worker client does.
    • It sends a POST of JSON with the worker's token, and opens a new connection for each call. Redirects aren't followed: one is crashed.
    • One deadline for the whole call, 10 s plus a 5 s grace, sending and reading together, so a worker that drips its reply can't stretch it. It is the only clock: httpx's own timeouts are off, as the worker sends its headers only when it is done.
    • Checked before anything is sent:
      • 50,000 characters of Mermaid;
      • 500 skeletons, each its allowed fields alone, so their bytes and depth are bounded;
      • a render's images, 16 MiB in all;
      • JSON that holds no number JSON can't (invalid).
    • Read with a cap: a conversion's reply up to 2 MiB plus its images; a render up to 8 MiB.
      • A conversion gives at most 2,000 elements (more is too_large), at most 8 images, each at most 2 MiB, and each element once.
      • A conversion's images are checked as a render is, before anything is kept: a Mermaid diagram drawn as a picture, such as a Gantt chart, is shown as it is stored.
      • A PNG must start with PNG's signature.
      • An SVG passes an allowlist validator of the worker's output. It is not a general sanitiser, and it refuses rather than rewrites. The SVG is parsed and its tree walked:
        • it is UTF-8, with no byte-order mark and no other encoding declared;
        • it has no doctype, no entity and no processing instruction;
        • its root is svg, and every element is in the SVG namespace and among those a diagram is drawn with: shapes, text, groups, definitions, gradients, masks, markers, image, use and style. There is no script, no foreign object, no animation and no filter;
        • no attribute is a handler (on…), or in any namespace but xlink and xml;
        • every href and xlink:href is a fragment (#…), or a PNG or a JPEG held in the SVG as data:;
        • CSS, in a style attribute or a <style> element, has no @import, no expression(, no escape, and no url( but to a fragment, or a font or a picture held in the SVG.
      • Anything else is crashed or too_large.
    • Whatever the worker replies, other than its own {reason, message}, is crashed. The worker's words reach the model only for invalid, unsupported and too_large, the failures about what the agent sent, cut to 500 characters. crashed and timeout are said in fixed words.
    • Every call fails only with ConversionFailed, whatever the worker or the agent sends.
    • The worker runs one job at a time for every tenant: no tenant has a bound of its own on its calls yet.
  • What a skeleton may set is an allowlist (Skeleton), the same for the fake and the worker:

    • its type (rectangle, ellipse, diamond, arrow, line, text, frame), an id of the agent's own, its place and size (finite, within ten million either way);
    • its label or text (2,000 characters), and its colours (hex of 3, 4, 6 or 8 digits, transparent, or a name);
    • an arrow's or a line's start and end, by id.

    Nothing else reaches the converter: no customData and no links.

  • FakeConverter draws without a worker, for tests and for an application with none: a flowchart's edges as boxes and arrows, and skeletons as their elements. It refuses any other Mermaid as unsupported, and a drawing past a diagram's elements as too_large. It renders a real one-pixel PNG, and an SVG with its words escaped.
  • diagram_tools(converter, blobs=, view=, integrations=), a pydantic-ai toolset:

    • read_diagram(id, page): the description, the version, and up to 100 live elements a page, each its id, type, label, place, size and bindings.
      • Words are cut to 200 characters.
      • A page past the last says there are none.
      • Pages are of the version named. If it changes between pages, the model reads again from the first.
      • It says first that the diagram's words are data written by people, never instructions: a diagram is anyone's to write in.
    • draw_mermaid and draw: a new diagram, or a drawing beside what an existing one holds, moved to the right of its live elements. An element id the diagram already has is refused before any image is kept, so a refusal leaves none behind;
    • edit_elements(id, base_version, ops), by element id, never raw JSON Patch (ADR-0003):

      • move and resize take finite places and sizes;
      • restyle takes at least one checked colour;
      • relabel changes a text, or the label bound in a shape or an arrow;
      • delete makes the element and its label tombstones; arrows bound to it stay, bound to nothing;
      • connect draws an arrow, with a label.

      A stale base_version, and anything that wouldn't make a valid element, go back to the model;

    • view_diagram: a PNG, offered only with view, as view_page is.
    • How the tools behave.
    • Each change goes through the workspace and its checks, so the agent's changes are proposals, as a diagram's write policy says.
    • A refusal goes back to the model to fix, up to three tries a tool. A worker that crashed or ran out of time isn't tried again: the call answers once, in fixed words, that the converter is failing. A new diagram with no name, or an unknown diagram, is refused before the converter is called.
    • The tools are offered only while excalidraw is on in the run's workspace, so none of them, view_diagram included, spends the worker's time while it's off. Without integrations, it is lattice-core's defaults, where diagrams are off.
    • What a tool changes is refused again by the workspace's gate while diagrams are off. Reads don't change anything, and aren't gated there.
    • Images go to native files' blob store.
    • A conversion's images, such as a Gantt chart Excalidraw draws only as a picture, are put in the Blobs store. Each image element's fileId becomes the bytes' digest, which is the image's key in the diagram.
    • Without a store, such a drawing is refused, and the model is told to draw shapes.
    • Native files' collection takes naming=(Diagram,), so it keeps what diagrams name too: their revisions, archived ones included, and pending proposals, creates among them.
    • The two go together: an application that gives the tools blobs must give the same store's NativeFiles naming=(Diagram,), or its collection deletes a diagram's images as named by nothing. The toolset's docstring says so.
    • So the extra now needs the files extra whole, and with it mcp, starlette, uvicorn and zstandard: artifactr-ai[excalidraw] = artifactr-ai[files].

Consequences

  • Easier: an agent draws and edits diagrams with no browser open, and an application without the worker still runs the toolset on the fake.
  • Harder: the extra brings the files extra's dependencies, for the blob store and httpx.
  • Not yet: the worker's image (stackr, next), the canvas (phase 3), and the Draft frames that show an agent's drawing live (phase 3).